Web服務之LNMMP架構及動靜分離實現

一、LNMMP  

  LNMMP環境是Linux + Nginx + Memcached + MySQL + PhP,即LNMP + memcached。

   Memcached 是一個高性能的分佈式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它通過在內存中緩存數據和對象來減少讀取數據庫的次數,從而提供動態、數據庫驅動網站的速度。Memcached基於一個存儲鍵/值對的hashmap。其守護進程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,並通過memcached協議與守護進程通信。

   

二、工程拓撲

wKiom1N4iVbBfTQjAAC0cRxVX3w956.jpg


三、安裝nginx服務器

1)部署開發環境
# yum -y install "Development tools" "Server Platform Development"
2)解決依賴 pcre-devel  openssl-devel
# yum -y install pcre-devel openssl-devel
3) 設置用戶
# 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
   內容如下
# 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)爲服務腳本賦予執行權限
# chmod +x /etc/rc.d/init.d/nginx
8)添加到系統服務並開機啓動
# chkconfig --add nginx
# chkconfig nginx on
# chkconfig --list nigx
9) 設置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 內容如下
 au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
10)啓動服務
# service nginx start
# ss -tnalp | grep nginx


四、安裝MySQL服務器

1.安裝

1
2
# 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 創建軟連接,易於操作

2.爲數據庫創建數據目錄

#mkdri -pv /mydata/data

3.新建用戶以安全方式運行進程

1
2
3
4
5
#groupadd -r mysql      //創建系統組mysql
#useradd -r -s /sbin/nologin -g mysql mysql -M -D /mydata/data mysql
//創建系統用戶mysql
#chown -R mysql:mysql /mydata/data
//設置目錄屬主屬組

4.初始化mysql

1
2
3
4
5
# cd /usr/local/mysql
# scripts/mysql_install_db --datadir=/mydata/data --user=mysql
//初始化數據庫
# chown -R root .
//設置當前目錄所有文件屬主爲root

5.提供腳本

1
2
3
4
5
6
7
8
#cd /usr/local/mysql
#cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
//設置腳本mysqld
#chmod +x /etc/rc.d/init.d/mysqld
//給腳本執行權限
# chkconfig --add mysqld
//添加開機啓動
# chkconfig  mysqld on

6.提供配文件

1
2
3
4
5
6
7
#cd /usr/local/mysql
#cp support-files/my-large.cnf  /etc/my.cnf
#vim /etc/my.cnf
thread_concurrency = 2
//修改,併發線程數,bithread_concurrency的值爲CPU個數乘以2
datadir = /mydata/data
#添加,mysql數據文件的存放路徑:

7.其他配置

1
2
3
4
5
6
7
8
9
10
# 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的頭文件至系統頭文件路徑/usr/include
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
//輸出mysql的庫文件給系統庫
#ldconfig   //重載系統庫:

8.啓動服務

# service mysqld start
# ss  -tnl | grep 3306

9.用戶初始化

1
2
3
4
5
6
7
8
#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用戶設置密碼
mysql> FLUSH PRIVILEGES;




五、安裝php服務器

1.解決開發環境和依賴關係

1
2
3
# 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.提供配置文件

1
# cp php.ini-production /etc/php.ini

4.爲php-fpm提供腳本,並將其添加到服務列表

# 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

1
2
3
4
5
6
7
8
9
10
# 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    

1
2
3
4
# 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,實現動靜分離

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  www.hoo.com;
        add_header X-via $server_addr;    #讓客戶端能夠看到代理服務器的IP
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location ~* \.(jpg|jpeg|png|gif|js|css)$ {  #匹配靜態內容
            root   html;    #默認目錄在/usr/local/nginx/html
        }
        location ~ \.php$ {  #匹配動態php文件
            root                html;
            fastcgi_pass        172.16.7.11:9000;    #代理到的服務器
            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服務器

1.memcached特性    

Memcached是一款開發工具,它既不是一個代碼加速器,也不是數據庫中間件。其設計哲學思想主要反映在如下方面:

(1)簡單key/value存儲:服務器不關心數據本身的意義及結構,只要是可序列化數據即可。存儲項由“鍵、過期時間、可選的標誌及數據”四個部分組成;

(2)功能的實現一半依賴於客戶端,一半基於服務器端:客戶負責發送存儲項至服務器端、從服務端獲取數據以及無法連接至服務器時採用相應的動作;服務端負責接收、存儲數據,並負責數據項的超時過期;

(3)各服務器間彼此無視:不在服務器間進行數據同步;

(4)O(1)的執行效率

(5)清理超期數據:默認情況下,Memcached是一個LRU緩存,同時,它按事先預訂的時長清理超期數據;但事實上,memcached不會刪除任何已緩存數據,只是在其過期之後不再爲客戶所見;而且,memcached也不會真正按期限清理緩存,而僅是當get命令到達時檢查其時長;


2.安裝memcached

   a).部署開發環境,解決依賴關係

# yum groupinstall "Development Tools" "Server Platform Deveopment" -y
# yum install -y libevent-devel

   b).編譯安裝memcached

# 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至服務列表

# chmod +x /etc/init.d/memcached
# chkconfig --add memcached
# service memcached start

   e).檢查是否運行

# 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功能進行測試,在網站目錄中建立測試頁面test.php

//php服務器
# 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狀態信息

1.解壓即可使用

//解壓至php服務器/usr/local/nginx/html/
# unzip memadmin-master.zip

2.測試訪問

   www.hoo.com/memadmin-master




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章