LNMMP架構實現Web動靜分離
北城書生 的BLOG
http://scholar.blog.51cto.com/9985645/1661861
前言
前 面的文章中說過LAMP架構包括:Linux作業系統,Apache網站伺服器,MySQL資料庫,Perl、PHP或者Python程式語言,而今天要 說的LNMMP和LAMP類似,只是作為Web伺服器的不再是Apache而是高效能的Nginx,同時引進Memcached加速快取效率,用於加快 訪問速度。
Memcached是一款開源、高效能、分散式記憶體物件快取系統,可應用各種需要快取的場景,其主要目的是通過降低對資料庫的訪問來加速Web應用程式。它是一個基於記憶體的“鍵值對”儲存,用於儲存資料庫呼叫、API呼叫或頁面引用結果的直接資料,如字串、物件等。
實現過程
實驗拓撲
實驗環境
系統環境:CentOS6.6
web伺服器:172.16.10.123 nginx-1.6.3
PHP伺服器:172.16.10.110 php-5.4.26
資料庫伺服器:172.16.10.211MariaDB-5.5.36
Memcached伺服器:172.16.10.212memcached-1.4.24
工作原理
利用nginx的高效能特點做前端反向代理伺服器,分發使用者請求,靜態請求直接返回結果,動態請求交給後端php處理,php查詢資料庫返回處理結果,並將結果快取至Memcached,當接收新請求時,php首先在Memcached查詢,Memcached有結果直接返還給nginx,沒結果再查詢資料庫,依次類推。
安裝配置nginx
12345678910111213141516171819202122232425262728 | #解決依賴關係 [[email protected]~] #yumgroupinstall"DevelopmentTools""ServerPlatformDeveopment"-y [[email protected]~] #yuminstallopenssl-develpcre-devel-y [[email protected]~] #groupadd-rnginx [[email protected]~] #useradd-r-gnginxnginx [[email protected]~] #tarxfnginx-1.6.3.tar.gz [[email protected]~] #cdnginx-1.6.3 [[email protected]nginx-1.6.3] #./configure\ >--prefix= /usr/local/nginx \ >--sbin-path= /usr/sbin/nginx \ >--conf-path= /etc/nginx/nginx .conf\ >--error-log-path= /var/log/nginx/error .log\ >--http-log-path= /var/log/nginx/access .log\ >--pid-path= /var/run/nginx/nginx .pid\ >--lock-path= /var/lock/nginx .lock\ >--user=nginx\ >--group=nginx\ >--with-http_ssl_module\ >--with-http_flv_module\ >--with-http_stub_status_module\ >--with-http_gzip_static_module\ >--http-client-body-temp-path= /usr/local/nginx/client/ \ >--http-proxy-temp-path= /usr/local/nginx/proxy/ \ >--http-fastcgi-temp-path= /usr/local/nginx/fcgi/ \ >--http-uwsgi-temp-path= /usr/local/nginx/uwsgi \ >--http-scgi-temp-path= /usr/local/nginx/scgi \ >--with-pcre [[email protected]nginx-1.6.3] #make&&makeinstall |
為nginx提供SysV init指令碼
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | [[email protected]~] #vim/etc/rc.d/init.d/nginx #新建檔案/etc/rc.d/init.d/nginx,內容如下: #!/bin/sh # #nginx-thisscriptstartsandstopsthenginxdaemon # #chkconfig:-8515 #description:NginxisanHTTP(S)server,HTTP(S)reverse\ #proxyandIMAP/POP3proxyserver #processname:nginx #config:/etc/nginx/nginx.conf #config:/etc/sysconfig/nginx #pidfile:/var/run/nginx.pid
#Sourcefunctionlibrary. . /etc/rc .d /init .d /functions
#Sourcenetworkingconfiguration. . /etc/sysconfig/network
#Checkthatnetworkingisup. [ "$NETWORKING" = "no" ]&& exit 0
nginx= "/usr/sbin/nginx" prog=$( basename $nginx)
NGINX_CONF_FILE= "/etc/nginx/nginx.conf"
[-f /etc/sysconfig/nginx ]&&. /etc/sysconfig/nginx
lockfile= /var/lock/subsys/nginx
make_dirs(){
#makerequireddirectories
user=`nginx-V2>&1| grep "configurearguments:" | sed 's/[^*]*--user=\([^]*\).*/\1/g' -`
options=`$nginx-V2>&1| grep 'configurearguments:' `
for opt in $options; do
if [` echo $opt| grep '.*-temp-path' `]; then
value=` echo $opt| cut -d "=" -f2`
if [!-d "$value" ]; then
#echo"creating"$value
mkdir -p$value&& chown -R$user$value
fi
fi
done }
start(){
[-x$nginx]|| exit 5
[-f$NGINX_CONF_FILE]|| exit 6
make_dirs
echo -n$ "Starting$prog:"
daemon$nginx-c$NGINX_CONF_FILE
retval=$?
echo
[$retval- eq 0]&& touch $lockfile
return $retval }
stop(){
echo -n$ "Stopping$prog:"
killproc$prog-QUIT
retval=$?
echo
[$retval- eq 0]&& rm -f$lockfile
return $retval }
restart(){
configtest|| return $?
stop
sleep 1
start }
reload(){
configtest|| return $?
echo -n$ "Reloading$prog:"
killproc$nginx-HUP
RETVAL=$?
echo }
force_reload(){
restart }
configtest(){
$nginx-t-c$NGINX_CONF_FILE }
rh_status(){
status$prog }
rh_status_q(){
rh_status> /dev/null 2>&1 }
case "$1" in
start)
rh_status_q&& exit 0
$1
;;
stop)
rh_status_q|| exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q|| exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q|| exit 0
;;
*)
echo $"Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force- reload|configtest}"
exit 2 esac |
為指令碼賦予執行許可權
1 | [[email protected]~] #chmod+x/etc/rc.d/init.d/nginx |
新增至服務管理列表,並讓其開機自動啟動
12 | [[email protected]~] #chkconfig--addnginx [[email protected]~] #chkconfignginxon |
配置nginx
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 | [[email protected]~] #vim/etc/nginx/nginx.conf worker_processes2; #worker程序的個數 error_log /var/log/nginx/error .lognotice; #錯誤日誌路徑及級別 events{
worker_connections1024; #每個worker能夠併發響應的最大請求數 } http{
includemime.types; #支援多媒體型別
default_typeapplication /octet-stream ;
sendfileon; #由核心直接轉發
#keepalive_timeout0;
keepalive_timeout5; #持久連線5s
gzip on; #開啟壓縮功能
server{
listen80;
server_namebbs.scholar.com;
add_headerX-via$server_addr; #讓客戶端能夠看到代理伺服器的IP
location/{
root /www/bbs ;
indexindex.phpindex.htmlindex.htm;
}
location~*\.(jpg|jpeg|png|gif|js|css)${ #匹配靜態內容
root /www/bbs ;
}
location~\.php${ #匹配動態內容
root /www/bbs ;
fastcgi_pass172.16.10.110:9000; #代理到的伺服器
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAMEscripts$fastcgi_script_name;
includefastcgi_params;
}
}
}
[[email protected]~] #vim/etc/nginx/fastcgi_params fastcgi_paramGATEWAY_INTERFACECGI /1 .1; fastcgi_paramSERVER_SOFTWAREnginx; fastcgi_paramQUERY_STRING$query_string; fastcgi_paramREQUEST_METHOD$request_method; fastcgi_paramCONTENT_TYPE$content_type; fastcgi_paramCONTENT_LENGTH$content_length; fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; fastcgi_paramSCRIPT_NAME$fastcgi_script_name; fastcgi_paramREQUEST_URI$request_uri; fastcgi_paramDOCUMENT_URI$document_uri; fastcgi_paramDOCUMENT_ROOT$document_root; fastcgi_paramSERVER_PROTOCOL$server_protocol; fastcgi_paramREMOTE_ADDR$remote_addr; fastcgi_paramREMOTE_PORT$remote_port; fastcgi_paramSERVER_ADDR$server_addr; fastcgi_paramSERVER_PORT$server_port; fastcgi_paramSERVER_NAME$server_name; [[email protected]~] #servicenginxstart |
安裝配置memcached
123456789101112131415 | #解決依賴關係 [[email protected]~] #yumgroupinstall"DevelopmentTools""ServerPlatformDeveopment"-y #安裝libevent #memcached依賴於libeventAPI,因此要事先安裝之 [[email protected]~] #tarxflibevent-2.0.22-stable.tar.gz [[email protected]~] #cdlibevent-2.0.22-stable [[email protected]libevent-2.0.22-stable] #./configure--prefix=/usr/local/libevent [[email protected]libevent-2.0.22-stable] #make&&makeinstall [[email protected]~] #echo"/usr/local/libevent/lib">/etc/ld.so.conf.d/libevent.conf [[email protected]~] #ldconfig #安裝配置memcached [[email protected]~] #tarxfmemcached-1.4.24.tar.tar [[email protected]~] #cdmemcached-1.4.24 [[email protected]memcached-1.4.24] #./configure--prefix=/usr/local/memcached--with-libevent=/usr/local/libevent [[email protected]memcached-1.4.24] #make&&makeinstall |
提供指令碼
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 | [[email protected]~] #vim/etc/init.d/memcached #!/bin/bash # #Initfileformemcached # #chkconfig:-8614 #description:Distributedmemorycachingdaemon # #processname:memcached #config:/etc/sysconfig/memcached . /etc/rc .d /init .d /functions ##Defaultvariables PORT= "11211" USER= "nobody" MAXCONN= "1024" CACHESIZE= "64" OPTIONS= "" RETVAL=0 prog= "/usr/local/memcached/bin/memcached" desc= "Distributedmemorycaching" lockfile= "/var/lock/subsys/memcached" start(){
echo -n$ "Starting$desc(memcached):"
daemon$prog-d-p$PORT-u$USER-c$MAXCONN-m$CACHESIZE-o "$OPTIONS"
RETVAL=$?
[$RETVAL- eq 0]&&success&& touch $lockfile||failure
echo
return $RETVAL } stop(){
echo -n$ "Shuttingdown$desc(memcached):"
killproc$prog
RETVAL=$?
[$RETVAL- eq 0]&&success&& rm -f$lockfile||failure
echo
return $RETVAL } restart(){
stop
start } reload(){
echo -n$ "Reloading$desc($prog):"
killproc$prog-HUP
RETVAL=$?
[$RETVAL- eq 0]&&success||failure
echo
return $RETVAL } case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[-e$lockfile]&&restart
RETVAL=$?
;;
reload)
reload
;;
status)
status$prog
RETVAL=$?
;;
*)
echo $ "Usage:$0{start|stop|restart|condrestart|status}"
RETVAL=1 esac exit $RETVAL |
授權並啟動服務
1234 | [[email protected]~] #vim/etc/init.d/memcached [[email protected]~] #chmod+x/etc/init.d/memcached [[email protected]~] #chkconfig--addmemcached [[email protected]~] #servicememcachedstart |
安裝配置php
123456789101112 | #解決依賴關係 [[email protected]~] #yumgroupinstall"Developmenttools""ServerPlatformDevelopment"-y [[email protected]~] #yum-ygroupinstall"DesktopPlatformDevelopment" [[email protected]~] #yum-yinstallbzip2-devellibmcrypt-devel [[email protected]~] #tarxfphp-5.4.26.tar.bz2 [[email protected]~] #cdphp-5.4.26 [[email protected]php-5.4.26] #./configure--prefix=/usr/local/php--with-mysql=mysqlnd --with-pdo-mysql=mysqlnd--with-mysqli=mysqlnd-- enable -mbstring--with-freetype- dir --with-jpeg- dir --with-png- dir --with-zlib--with-libxml- dir = /usr -- enable -xml -- enable -sockets-- enable -fpm--with-mcrypt--with-config- file -path= /etc --with-config- file -scan- dir = /etc/php .d--with-bz2--with-openssl [[email protected]php-5.4.26] #make&&makeinstall |
提供配置檔案
1234567891011121314151617181920212223 | #為php提供配置檔案 [[email protected]php-5.4.26] #cpphp.ini-production/etc/php.ini #為php-fpm提供指令碼 [[email protected]php-5.4.26] #cpsapi/fpm/init.d.php-fpm/etc/rc.d/init.d/php-fpm [[email protected]php-5.4.26] #chmod+x/etc/rc.d/init.d/php-fpm [[email protected]php-5.4.26] #chkconfig--addphp-fpm [[email protected]php-5.4.26] #chkconfigphp-fpmon #為php-fpm提供配置檔案 [[email protected]php-5.4.26] #cd/usr/local/php [[email protected]php] #cpetc/php-fpm.conf.defaultetc/php-fpm.conf [[email protected]php] #vimetc/php-fpm.conf pid= /usr/local/php/var/run/php-fpm .pid listen=172.16.10.110:9000 pm.max_children=25 #最大子程序數 pm.start_servers=5 #開機預啟動子程序數 pm.min_spare_servers=2 #最小空閒子程序數 pm.max_spare_servers=6 #最大空閒子程序數 |
php安裝xcache拓展
12345678910111213 | [[email protected]~] #tarxfxcache-3.1.0.tar.bz2 [[email protected]~] #cdxcache-3.1.0 [[email protected]xcache-3.1.0] #/usr/local/php/bin/phpize [[email protected]xcache-3.1.0] #./configure--enable-xcache--with-php-config=/usr/local/php /bin/php-config [[email protected]xcache-3.1.0] #make&&makeinstall [[email protected]xcache-3.1.0] #mkdir/etc/php.d [[email protected]xcache-3.1.0] #cpxcache.ini/etc/php.d/ [[email protected]xcache-3.1.0] #vim/etc/php.d/xcache.ini [xcache-common] ;;non-Windowsexample: extension= /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache .so |
php安裝memcache拓展
12345678910 | [[email protected]~] #tarxfmemcache-2.2.7.tgz [[email protected]~] #cdmemcache-2.2.7 [[email protected]memcache-2.2.7] #/usr/local/php/bin/phpize [[email protected]memcache-2.2.7] #./configure--with-php-config=/usr/local/php/bin/php-config--enable-memcache [[email protected]memcache-2.2.7] #make&&makeinstall [[email protected]memcache-2.2.7] #vim/etc/php.ini extension= /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache .so [[email protected]~] #servicephp-fpmstart |
安裝配置mariadb
12345678 | [[email protected]~] #mkdir/mydata/data-pv [[email protected]~] #groupadd-rmysql [[email protected]~] #useradd-gmysql-rmysql [r[email protected]~] #chown-Rmysql.mysql/mydata/data [[email protected]~] #tarxfmariadb-5.5.36-linux-x86_64.tar.gz-C/usr/local [[email protected]~] #cd/usr/local [[email protected] local ] #ln-svmariadb-5.5.36-linux-x86_64mysql [[email protected] local ] #chown-Rroot.mysqlmysql |
提供配置及指令碼檔案
1234567891011 | [[email protected] local ] #mkdir/etc/mysql [[email protected] local ] #cdmysql [[email protected]mysql] #cp/support-files/my-large.cnf/etc/mysql/my.cnf [[email protected]mysql] #vim/etc/mysql/my.cnf datadir= /mydata/data [[email protected]mysql] #cpsupport-files/mysql.server/etc/rc.d/init.d/mysqld [[email protected]mysql] #chmod+x/etc/rc.d/init.d/mysqld [[email protected]mysql] #chkconfig--addmysqld [[email protected]mysql] #chkconfigmysqldon |
初始化資料庫
12 | [[email protected]mysql] #scripts/mysql_install_db--user=mysql--datadir=/mydata/data [[email protected]~] #servicemysqldstart |
部署站點
12345678910111213141516171819202122232425 | [[email protected]~] #mkdir/www/bbs-pv [[email protected]~] #unzipwordpress-3.2.1-zh_CN.zip [[email protected]~] #cdwordpress [[email protected]wordpress] #mv*/www/bbs/ #在web和php上分別準備站點檔案 #php節點 [[email protected]~] #cd/www/bbs [[email protected]bbs] #cpwp-config-sample.phpwp-config.php [[email protected]bbs] #vimwp-config.php /**WordPress資料庫的名稱*/ define( 'DB_NAME' , 'wpdb' ); /**MySQL資料庫使用者名稱*/ define( 'DB_USER' , 'wpuser' ); /**MySQL資料庫密碼*/ define( 'DB_PASSWORD' , 'wppass' ); /**MySQL主機*/ define( 'DB_HOST' , '172.16.10.211' ); /**建立資料表時預設的文字編碼*/ define( 'DB_CHARSET' , 'utf8' ); |
建立資料庫並授權
12345678 | MariaDB[(none)]>createdatabasewpdb; QueryOK,1rowaffected(0.05sec) MariaDB[(none)]>grantallonwpdb.*to[email protected] '172.16.%.%' identifiedby 'wppass' ; QueryOK,0rowsaffected(0.06sec) MariaDB[(none)]>flushprivileges; QueryOK,0rowsaffected(0.03sec) |
安裝memadmin
MemAdmin是一款視覺化的Memcached管理與監控工具,使用PHP開發,體積小,操作簡單。
主要功能:
1234567 | 伺服器引數監控:STATS、SETTINGS、ITEMS、SLABS、SIZES實時重新整理 伺服器效能監控:GET、DELETE、INCR、DECR、CAS等常用操作命中率實時監控 支援資料遍歷,方便對儲存內容進行監視 支援條件查詢,篩選出滿足條件的KEY或VALUE 陣列、JSON等序列化字元反序列顯示 相容memcache協議的其他服務,如TokyoTyrant(遍歷功能除外) 支援伺服器連線池,多伺服器管理切換方便簡潔 |
123 | [[email protected]~] #tarxfmemadmin-1.0.12.tar.gz-C/www/bbs/ #web和php端都需執行此操作 |
登陸後新增伺服器
開始管理伺服器
更多細節有興趣可自行探索
Ten end
LNMMP 架構實現Web動靜分離實驗就說到這裡了,整個部署過程跟LAMP類似,朋友們部署過程遇到問題可留言交流,nginx在反向代理時還可將快取快取至 memcached伺服器,從而提高快取效能,這裡稍微一提,就不做詳解了。以上僅為個人學習整理,如有錯漏,大神勿噴~~~
本文出自 “北城書生” 部落格,請務必保留此出處http://scholar.blog.51cto.com/9985645/1661861
轉載於:https://blog.51cto.com/941012521/1695677