前言
前面的文章中說過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.211 MariaDB-5.5.36
Memcached服務器:172.16.10.212 memcached-1.4.24
工作原理
利用nginx的高性能特點做前端反向代理服務器,分發用戶請求,靜態請求直接返回結果,動態請求交給后端php處理,php查詢數據庫返回處理結果,并將結果緩存至Memcached,當接收新請求時,php首先在Memcached查詢,Memcached有結果直接返還給nginx,沒結果再查詢數據庫,依次類推。
安裝配置nginx
#解決依賴關系 [root@node1 ~]# yum groupinstall "Development Tools" "Server Platform Deveopment" -y [root@node1 ~]# yum install openssl-devel pcre-devel -y [root@node1 ~]# groupadd -r nginx [root@node1 ~]# useradd -r -g nginx nginx [root@node1 ~]# tar xf nginx-1.6.3.tar.gz [root@node1 ~]# cd nginx-1.6.3 [root@node1 ~]# [root@node1 ~]# cd nginx-1.6.3 [root@node1 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 [root@node1 nginx-1.6.3]# make && make install
為nginx提供SysV init腳本
[root@node1 ~]# vim /etc/rc.d/init.d/nginx #新建文件/etc/rc.d/init.d/nginx,內容如下: #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$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() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` 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
為腳本賦予執行權限
[root@node1 ~]# chmod +x /etc/rc.d/init.d/nginx
添加至服務管理列表,并讓其開機自動啟動
[root@node1 ~]# chkconfig --add nginx [root@node1 ~]# chkconfig nginx on
配置nginx
[root@node1 ~]# vim /etc/nginx/nginx.conf worker_processes 2; #worker進程的個數 error_log /var/log/nginx/error.log notice; #錯誤日志路徑及級別 events { worker_connections 1024; #每個worker能夠并發響應的最大請求數 } http { include mime.types; #支持多媒體類型 default_type application/octet-stream; sendfile on; #由內核直接轉發 #keepalive_timeout 0; keepalive_timeout 5; #持久連接5s gzip on; #開啟壓縮功能 server { listen 80; server_name bbs.scholar.com; add_header X-via $server_addr; #讓客戶端能夠看到代理服務器的IP location / { root /www/bbs; index index.php index.html index.htm; } location ~* \.(jpg|jpeg|png|gif|js|css)$ { #匹配靜態內容 root /www/bbs; } location ~ \.php$ { #匹配動態內容 root /www/bbs; fastcgi_pass 172.16.10.110:9000; #代理到的服務器 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME scripts$fastcgi_script_name; include fastcgi_params; } } } [root@node1 ~]# vim /etc/nginx/fastcgi_params fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; [root@node1 ~]# service nginx start
安裝配置memcached
#解決依賴關系 [root@scholar ~]# yum groupinstall "Development Tools" "Server Platform Deveopment" -y #安裝libevent memcached依賴于libevent API,因此要事先安裝之,項目主頁:http://libevent.org/,讀者可自行選擇需要的版本下載。本文采用的是目前最新版本的源碼包libevent-2.0.22-stable.tar.gz。安裝過程: [root@scholar ~]# tar xf libevent-2.0.22-stable.tar.gz [root@scholar ~]# cd libevent-2.0.22-stable [root@scholar libevent-2.0.22-stable]# ./configure --prefix=/usr/local/libevent [root@scholar libevent-2.0.22-stable]# make && make install [root@scholar ~]# echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf [root@scholar ~]# ldconfig #安裝配置memcached [root@scholar ~]# tar xf memcached-1.4.24.tar.tar [root@scholar ~]# cd memcached-1.4.24 [root@scholar memcached-1.4.24]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent [root@scholar memcached-1.4.24]# make && make install
提供腳本
[root@scholar ~]# vim /etc/init.d/memcached #!/bin/bash # # Init file for memcached # # chkconfig: - 86 14 # description: Distributed memory caching daemon # # processname: memcached # config: /etc/sysconfig/memcached . /etc/rc.d/init.d/functions ## Default variables PORT="11211" USER="nobody" MAXCONN="1024" CACHESIZE="64" OPTIONS="" RETVAL=0 prog="/usr/local/memcached/bin/memcached" desc="Distributed memory caching" 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 $"Shutting down $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
授權并啟動服務
[root@scholar ~]# vim /etc/init.d/memcached [root@scholar ~]# chmod +x /etc/init.d/memcached [root@scholar ~]# chkconfig --add memcached [root@scholar ~]# service memcached start
安裝配置php
#解決依賴關系 [root@scholar ~]# yum groupinstall "Development tools" "Server Platform Development" -y [root@scholar ~]# yum -y groupinstall "Desktop Platform Development" [root@scholar ~]# yum -y install bzip2-devel libmcrypt-devel [root@scholar ~]# tar xf php-5.4.26.tar.bz2 [root@scholar ~]# cd php-5.4.26 [root@scholar 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 [root@scholar php-5.4.26]# make && make install
提供配置文件
#為php提供配置文件 [root@scholar php-5.4.26]# cp php.ini-production /etc/php.ini #為php-fpm提供腳本 [root@scholar php-5.4.26]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@scholar php-5.4.26]# chmod +x /etc/rc.d/init.d/php-fpm [root@scholar php-5.4.26]# chkconfig --add php-fpm [root@scholar php-5.4.26]# chkconfig php-fpm on #為php-fpm提供配置文件 [root@scholar php-5.4.26]# cd /usr/local/php [root@scholar php]# cp etc/php-fpm.conf.default etc/php-fpm.conf [root@scholar php]# vim etc/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拓展
[root@scholar ~]# tar xf xcache-3.1.0.tar.bz2 [root@scholar ~]# cd xcache-3.1.0 [root@scholar xcache-3.1.0]# /usr/local/php/bin/phpize [root@scholar xcache-3.1.0]# ./configure --enable-xcache --with-php-config=/usr/local/php /bin/php-config [root@scholar xcache-3.1.0]# make && make install [root@scholar xcache-3.1.0]# mkdir /etc/php.d [root@scholar xcache-3.1.0]# cp xcache.ini /etc/php.d/ [root@scholar xcache-3.1.0]# vim /etc/php.d/xcache.ini [xcache-common] ;; non-Windows example: extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so
php安裝memcache拓展
[root@scholar ~]# tar xf memcache-2.2.7.tgz [root@scholar ~]# cd memcache-2.2.7 [root@scholar memcache-2.2.7]# /usr/local/php/bin/phpize [root@scholar memcache-2.2.7]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache [root@scholar memcache-2.2.7]# make && make install [root@scholar memcache-2.2.7]# vim /etc/php.ini extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so [root@scholar ~]# service php-fpm start
安裝配置mariadb
[root@MariaDB ~]# mkdir /mydata/data -pv [root@MariaDB ~]# groupadd -r mysql [root@MariaDB ~]# useradd -g mysql -r mysql [root@MariaDB ~]# chown -R mysql.mysql /mydata/data [root@MariaDB ~]# tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local [root@MariaDB ~]# cd /usr/local [root@MariaDB local]# ln -sv mariadb-5.5.36-linux-x86_64 mysql [root@MariaDB local]# chown -R root.mysql mysql
提供配置及腳本文件
[root@MariaDB local]# mkdir /etc/mysql [root@MariaDB local]# cd mysql [root@MariaDB mysql]# cp /support-files/my-large.cnf /etc/mysql/my.cnf [root@MariaDB mysql]# vim /etc/mysql/my.cnf datadir = /mydata/data [root@MariaDB mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@MariaDB mysql]# chmod +x /etc/rc.d/init.d/mysqld [root@MariaDB mysql]# chkconfig --add mysqld [root@MariaDB mysql]# chkconfig mysqld on
初始化數據庫
[root@MariaDB mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data [root@MariaDB ~]# service mysqld start
部署站點
[root@node1 ~]# mkdir /www/bbs -pv [root@node1 ~]# unzip wordpress-3.2.1-zh_CN.zip [root@node1 ~]# cd wordpress [root@node1 wordpress]# mv * /www/bbs/ #在web和php上分別準備站點文件 #php節點 [root@scholar ~]# cd /www/bbs [root@scholar bbs]# cp wp-config-sample.php wp-config.php [root@scholar bbs]# vim wp-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');
創建數據庫并授權
MariaDB [(none)]> create database wpdb; Query OK, 1 row affected (0.05 sec) MariaDB [(none)]> grant all on wpdb.* to wpuser@'172.16.%.%' identified by 'wppass'; Query OK, 0 rows affected (0.06 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.03 sec)
安裝memadmin
MemAdmin是一款可視化的Memcached管理與監控工具,使用PHP開發,體積小,操作簡單。
主要功能:
服務器參數監控:STATS、SETTINGS、ITEMS、SLABS、SIZES實時刷新 服務器性能監控:GET、DELETE、INCR、DECR、CAS等常用操作命中率實時監控 支持數據遍歷,方便對存儲內容進行監視 支持條件查詢,篩選出滿足條件的KEY或VALUE 數組、JSON等序列化字符反序列顯示 兼容memcache協議的其他服務,如Tokyo Tyrant (遍歷功能除外) 支持服務器連接池,多服務器管理切換方便簡潔
[root@node1 ~]# tar xf memadmin-1.0.12.tar.gz -C /www/bbs/ #web和php端都需執行此操作
登陸后添加服務器
開始管理服務器
更多細節有興趣可自行探索
Ten end
LNMMP架構實現Web動靜分離實驗就說到這里了,整個部署過程跟LAMP類似,朋友們部署過程遇到問題可留言交流,nginx在反向代理時還可將緩存緩存至memcached服務器,從而提高緩存性能,這里稍微一提,就不做詳解了。以上僅為個人學習整理,如有錯漏,大神勿噴~~~
原創文章,作者:書生,如若轉載,請注明出處:http://www.www58058.com/5347