Web服務(wù)之LNMMP架構(gòu)及動靜分離實現(xiàn)
一、LNMMP
LNMMP環(huán)境是Linux + Nginx + Memcached + MySQL + PhP,即LNMP + memcached。
Memcached 是一個高性能的分布式內(nèi)存對象緩存系統(tǒng),用于動態(tài)Web應(yīng)用以減輕數(shù)據(jù)庫負載。它通過在內(nèi)存中緩存數(shù)據(jù)和對象來減少讀取數(shù)據(jù)庫的次數(shù),從而提供動態(tài)、數(shù)據(jù)庫驅(qū)動網(wǎng)站的速度。Memcached基于一個存儲鍵/值對的hashmap。其守護進程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,并通過memcached協(xié)議與守護進程通信。
二、工程拓撲
三、安裝nginx服務(wù)器
- 1)部署開發(fā)環(huán)境
- # yum -y install "Development tools" "Server Platform Development"
- 2)解決依賴 pcre-devel openssl-devel
- # yum -y install pcre-devel openssl-devel
- 3) 設(shè)置用戶
- # groupadd -r nginx
- # useradd -r -g nginx nginx
- 4)編譯安裝nginx-1.4.7
- # tar xf nginx-1.4.7.tar.gz
- # cd nginx-1.4.7
- # ./configure --prefix=/usr --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=\ /var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
- --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=\
- /var/tmp/nginx/scgi --with-pcre
- # make && make install
- 5)檢測配置文件語法
- # /usr/sbin/nginx -t
- 6) 提供啟動腳本
- # vim /etc/rc.d/init.d/nginx
- 內(nèi)容如下
- # 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
- 7)為服務(wù)腳本賦予執(zhí)行權(quán)限
- # chmod +x /etc/rc.d/init.d/nginx
- 8)添加到系統(tǒng)服務(wù)并開機啟動
- # chkconfig --add nginx
- # chkconfig nginx on
- # chkconfig --list nigx
- 9) 設(shè)置nginx配置文件的語法高亮
- # mkdir ./vim/syntax -pv
- # cd .vim/syntax
- # wget http://www.vim.org/scripts/download_script.php?src_id=19394
- # cd .vim
- # vim filetype.vim 內(nèi)容如下
- au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
- 10)啟動服務(wù)
- # service nginx start
- # ss -tnalp | grep nginx
四、安裝MySQL服務(wù)器
1.安裝
- # tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local
- # ln -sv /usr/local/mysql-5.5.33-linux2.6-x86_64 mysql 創(chuàng)建軟連接,易于操作
2.為數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)目錄
- #mkdri -pv /mydata/data
3.新建用戶以安全方式運行進程
- #groupadd -r mysql //創(chuàng)建系統(tǒng)組mysql
- #useradd -r -s /sbin/nologin -g mysql mysql -M -D /mydata/data mysql
- //創(chuàng)建系統(tǒng)用戶mysql
- #chown -R mysql:mysql /mydata/data
- //設(shè)置目錄屬主屬組
4.初始化mysql
- # cd /usr/local/mysql
- # scripts/mysql_install_db --datadir=/mydata/data --user=mysql
- //初始化數(shù)據(jù)庫
- # chown -R root .
- //設(shè)置當前目錄所有文件屬主為root
5.提供腳本
- #cd /usr/local/mysql
- #cp support-files/mysql.server /etc/rc.d/init.d/mysqld
- //設(shè)置腳本mysqld
- #chmod +x /etc/rc.d/init.d/mysqld
- //給腳本執(zhí)行權(quán)限
- # chkconfig --add mysqld
- //添加開機啟動
- # chkconfig mysqld on
6.提供配文件
- #cd /usr/local/mysql
- #cp support-files/my-large.cnf /etc/my.cnf
- #vim /etc/my.cnf
- thread_concurrency = 2
- //修改,并發(fā)線程數(shù),bithread_concurrency的值為CPU個數(shù)乘以2
- datadir = /mydata/data
- #添加,mysql數(shù)據(jù)文件的存放路徑:
7.其他配置
- # vim /etc/profile.d/mysqld.sh
- exportPATH=/usr/local/mysql/bin:$PATH
- # source /etc/profile.d/mysqld.sh
- #vim /etc/man.config
- MANPATH /usr/local/mysql/man//添加此行
- # ln -sv /usr/local/mysql/include /usr/include/mysql
- //輸出mysql的頭文件至系統(tǒng)頭文件路徑/usr/include
- # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
- //輸出mysql的庫文件給系統(tǒng)庫
- #ldconfig //重載系統(tǒng)庫:
8.啟動服務(wù)
- # service mysqld start
- # ss -tnl | grep 3306
9.用戶初始化
- #mysql
- mysql> use mysql
- mysql> selecthost,user,password from user;
- mysql> DELETE FROM user WHERE user = ''; //刪除空用戶
- mysql> DELETE FROM user WHERE user = '::1'; //刪除ipv6用戶
- mysql> UPDATE user SET password = PASSWORD('Hoolee') WHERE password = '';
- //為root用戶設(shè)置密碼
- mysql> FLUSH PRIVILEGES;
#p#
五、安裝php服務(wù)器
1.解決開發(fā)環(huán)境和依賴關(guān)系
- # yum -y install bzip2-devel
- # yum -y install libmcrypt-devel
- # yum -y groupinstall "Desktop Platform Development"
2.安裝php
- # tar xf php-5.4.26.tar.bz2
- # cd /usr/src/php-5.4.26/
- # ./configure--prefix=/usr/local/php --with-openssl
- --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
- --enable-mbstring --with-freetype-dir --with-jpeg-dir
- --with-png-dir --with-zlib--with-libxml-dir=/usr --enable-xml
- --enable-sockets --with-apxs2=/usr/local/apache2/bin/apxs
- --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d
- --with-bz2 --enable-maintainer-zts
- # make && make install
3.提供配置文件
- # cp php.ini-production /etc/php.ini
4.為php-fpm提供腳本,并將其添加到服務(wù)列表
- # cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
- # chmod +x /etc/rc.d/init.d/php-fpm
- # chkconfig --add php-fpm
- # chkconfig php-fpm on
5.為php-fpm提供配置文件
- # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
6.編輯php-fpm配置文件
- # vim /usr/local/php/etc/php-fpm.conf
- pm.max_children = 150
- pm.start_servers = 8
- pm.min_spare_servers = 5
- pm.max_spare_servers = 10
- pid = /usr/local/php/var/run/php-fpm.pid
- listen = 172.16.1.11:9000
7.啟動php-fpm
- # service php-fpm start
- # ps -aux | grep php-fpm
六、安裝php加速器xcache
1.安裝xcache
- # tar xf xcache-3.0.3.tar.gz
- # cd xcache-3.0.3
- # /usr/local/php/bin/phpize
- //phpize是用來安裝php擴展模塊的,通過phpize可以建立php的
- 外掛模塊,若你想在原來編譯好的php中加入memcached或者
- ImageMagick等擴展模塊,就需要使用phpize
- # # ./configure --enable=xcache --with-php-config=/usr/local/php/bin/
- php-config
- # make && make install
- //顯示xcache模塊路徑:/usr/local/php/lib/php/extensions/no-debug-zts-20100525/
2.編輯配置文件,整合php + xcache
- # vim xcache.ini
- //添加模塊路徑
- extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
- # cp xcache.ini /etc/php.d/
3.重啟php-fpm
- # service php-fpm restart
七、配置nginx
1.編輯/etc/nginx/nginx.conf,實現(xiàn)動靜分離
- worker_processes 2; #worker進程的個數(shù)
- error_log /var/log/nginx/error.log notice; #錯誤日志路徑及級別
- events {
- worker_connections 1024; #每個worker能夠并發(fā)響應(yīng)的最大請求數(shù)
- }
- http {
- include mime.types; #支持多媒體類型
- default_type application/octet-stream;
- sendfile on; #由內(nèi)核直接轉(zhuǎn)發(fā)
- #keepalive_timeout 0;
- keepalive_timeout 5; #持久連接5s
- gzip on; #開啟壓縮功能
- server {
- listen 80;
- server_name www.hoo.com;
- add_header X-via $server_addr; #讓客戶端能夠看到代理服務(wù)器的IP
- location / {
- root html;
- index index.php index.html index.htm;
- }
- location ~* \.(jpg|jpeg|png|gif|js|css)$ { #匹配靜態(tài)內(nèi)容
- root html; #默認目錄在/usr/local/nginx/html
- }
- location ~ \.php$ { #匹配動態(tài)php文件
- root html;
- fastcgi_pass 172.16.7.11:9000; #代理到的服務(wù)器
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME scripts$fastcgi_script_name;
- include fastcgi_params;
- }
- }
- }
2.編輯/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;
3.重載nginx
- # service nginx reload
八、安裝Memcached服務(wù)器
1.memcached特性
Memcached是一款開發(fā)工具,它既不是一個代碼加速器,也不是數(shù)據(jù)庫中間件。其設(shè)計哲學思想主要反映在如下方面:
(1)簡單key/value存儲:服務(wù)器不關(guān)心數(shù)據(jù)本身的意義及結(jié)構(gòu),只要是可序列化數(shù)據(jù)即可。存儲項由“鍵、過期時間、可選的標志及數(shù)據(jù)”四個部分組成;
(2)功能的實現(xiàn)一半依賴于客戶端,一半基于服務(wù)器端:客戶負責發(fā)送存儲項至服務(wù)器端、從服務(wù)端獲取數(shù)據(jù)以及無法連接至服務(wù)器時采用相應(yīng)的動作;服務(wù)端負責接收、存儲數(shù)據(jù),并負責數(shù)據(jù)項的超時過期;
(3)各服務(wù)器間彼此無視:不在服務(wù)器間進行數(shù)據(jù)同步;
(4)O(1)的執(zhí)行效率
(5)清理超期數(shù)據(jù):默認情況下,Memcached是一個LRU緩存,同時,它按事先預(yù)訂的時長清理超期數(shù)據(jù);但事實上,memcached不會刪除任何已緩存數(shù)據(jù),只是在其過期之后不再為客戶所見;而且,memcached也不會真正按期限清理緩存,而僅是當get命令到達時檢查其時長;
2.安裝memcached
a).部署開發(fā)環(huán)境,解決依賴關(guān)系
- # yum groupinstall "Development Tools" "Server Platform Deveopment" -y
- # yum install -y libevent-devel
- # tar xf memcached-1.4.15.tar.gz
- # cd memcached-1.4.15
- # ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
- # make && make install
c).為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
d).添加memcached至服務(wù)列表
- # chmod +x /etc/init.d/memcached
- # chkconfig --add memcached
- # service memcached start
- # ss -tnlp | grep 11211
九、安裝php的memcached擴展
1.安裝memcached擴展
- # tar xf memcache-2.2.7.tgz
- # cd memcache-2.2.7
- # /usr/local/php/bin/phpize
- # ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
- # make && make install
2.編輯配置文件,整合php + memcached
- # vim xcache.ini
- //添加模塊路徑
- extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/memcache.so
3.重啟php-fpm
- # service php-fpm restart
4.對memcached功能進行測試,在網(wǎng)站目錄中建立測試頁面test.php
- //php服務(wù)器
- # vim test.php
- <?php
- $mem = new Memcache;
- $mem->connect("172.16.1.13", 11211) or die("Could not connect");
- $version = $mem->getVersion();
- echo "Server's version: ".$version."<br/>\n";
- $mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");
- echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";
- $get_result = $mem->get('hellokey');
- echo "$get_result is from memcached server.";
- ?>
- ~
測試訪問即可。
5.安裝wordpress測試訪問即可
十、安裝memadmin-master查看memcached狀態(tài)信息
1.解壓即可使用
- //解壓至php服務(wù)器/usr/local/nginx/html/
- # unzip memadmin-master.zip
2.測試訪問