Nginx(四):LNMMP架構實現Web動靜分離

Nginx(四):LNMMP架構實現Web動靜分離

簡介:

  Memcached是一款開源、高性能、分佈式內存對象緩存系統,可應用各類須要緩存的場景,其主要目的是經過下降對Database的訪問來加速web應用程序。它是一個基於內存的「鍵值對」存儲,用於存儲數據庫調用、API調用或頁面引用結果的直接數據,如字符串、對象等。php

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

一、簡單key/value存儲:服務器不關心數據自己的意義及結構,只要是可序列化數據便可。存儲項由「鍵、過時時間、可選的標誌及數據」四個部分組成;html

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

三、各服務器間彼此無視:不在服務器間進行數據同步;node

四、O(1)的執行效率;mysql

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

實驗拓撲:

輸入圖片說明

工做原理:

  利用nginx的高性能特色作前端反向代理服務器,分發用戶請求,靜態請求直接返回結果,動態請求交給後端php處理,php查詢數據庫返回處理結果,並將結果緩存至Memcached,當接收新請求時,php首先在Memcached查詢,Memcached有結果直接返還給nginx,沒結果再查詢數據庫,依次類推。nginx

實驗環境:

OS:CentOS-6.7-x86_64
Nginx:192.168.1.4
PHP-fpm:192.168.1.5
Mysql:192.168.1.6
Memcached:192.168.1.7

實驗過程:

編譯安裝Nginx-1.8:

安裝開發環境,並解決軟件依賴關係:web

[root@node0 ~]# yum groupinstall "Development Tools" "Server Platform Deveopment"
[root@node0 ~]# yum install pcre-devel zlib-devel

建立nginx用戶和nginx組:sql

[root@node0 ~]# groupadd -r nginx
[root@node0 ~]# useradd -g nginx -r nginx

編譯安裝:

[root@node0 ~]# tar xf nginx-1.8.0.tar.gz
[root@node0 ~]# cd nginx-1.8.0/
[root@node0 nginx-1.8.0]# ./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.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@node0 nginx-1.8.0]# make && make install

提供服務啓動腳本:

[root@node0 ~]# cat /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.lock

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   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

nginx主配置文件支持vim高亮:

下載nginx.vim:http://www.vim.org/scripts/script.php?script_id=1886

[root@node0 ~]# mkdir -p ./.vim/syntax
[root@node0 ~]# cp nginx.vim ./.vim/syntax/
[root@node0 ~]# vim ./.vim/filetype.vim
au BufRead,BufNewFile /etc/nginx/* if &ft == '' | setfiletype nginx | endif

編輯配置文件:

[root@node0 ~]# vim /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    server {
        listen 80;
        server_name www.chencer.org;
        add_header X-via $server_addr;

        location / {
            root /web/www;
            index index.php index.html index.hml;
        }
        location ~* \.(jpg|jpeg|png|gif|js|css)$ {
            root /web/www;
        }
        location ~ \.php$ {
            root /web/www;
            fastcgi_pass 192.168.1.5:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}
[root@node0 ~]# 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@node0 ~]# chmod +x /etc/rc.d/init.d/nginx
[root@node0 ~]# chkconfig nginx --add
[root@node0 ~]# chkconfig nginx on
[root@node0 ~]# service nginx start
編譯安裝PHP-5.4;

安裝編譯環境,並解決軟件依賴關係:

[root@node1 ~]# yum groupinstall "Server Platform Development" "Development tools" "Desktop Platform Development"
[root@node1 ~]# yum install bzip2-devel libmcrypt-devel mhash-devel
# 注意:libmcrypt-devel和mhash-devel來自於epel源

編譯安裝php:

[root@node1 ~]# tar xf php-5.4.45.tar.bz2 
[root@node1 ~]# cd php-5.4.45/
[root@node1 php-5.4.45]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-libxml-dir=/usr --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
[root@node1 php-5.4.45]# make && make install

導出頭文件、庫文件:

[root@node1 ~]# ln -sv /usr/local/php/include /usr/include/php
"/usr/include/php" -> "/usr/local/php/include"
[root@node1 ~]# vim /etc/ld.so.conf.d/php.conf
/usr/local/php/lib
[root@node1 ~]# ldconfig

添加並重讀環境變量:

[root@node1 ~]# vim /etc/profile.d/php.sh
export PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH
[root@node1 ~]# source /etc/profile.d/php.sh

提供php和php-fpm配置文件:

[root@node1 ~]# cp ./php-5.4.45/php.ini-production /etc/php.ini
[root@node1 ~]# cd /usr/local/php/etc/
[root@node1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@node1 etc]# vim php-fpm.conf
listen = 192.168.0.5:9000
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php/var/run/php-fpm.pid

編譯安裝xcache:

[root@node1 ~]# tar xf xcache-3.2.0.tar.bz2 
[root@node1 ~]# cd xcache-3.2.0/
[root@node1 xcache-3.2.0]# phpize
[root@node1 xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
[root@node1 xcache-3.2.0]# make && make install

編譯安裝php-memecache擴展:

[root@node1 ~]# tar xf memcache-2.2.7.tar.gz 
[root@node1 ~]# cd memcache-2.2.7
[root@node1 memcache-2.2.7]# phpize
[root@node1 memcache-2.2.7]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
[root@node1 memcache-2.2.7]# make && make install

配置添加xcache和memechace擴展:

[root@node1 ~]# mkdir /etc/php.d
[root@node1 ~]# cp ./xcache-3.2.0/xcache.ini /etc/php.d/
[root@node1 ~]# vim /etc/php.d/xcache.ini
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so
[root@node1 ~]# vim /etc/php.ini
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

爲php-fpm提供服務啓動腳本:

[root@node1 ~]# cp ./php-5.4.45/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@node1 ~]# chmod +x /etc/rc.d/init.d/php-fpm

添加並啓動服務:

[root@node1 ~]# chkconfig php-fpm --add
[root@node1 ~]# chkconfig php-fpm on
[root@node1 ~]# service php-fpm start
二進制格式安裝Mysql-5.5:

建立數據目錄和mysql用戶:

[root@node2 ~]# mkdir -p /data/mydata
[root@node2 ~]# groupadd -r mysql
[root@node2 ~]# useradd -g mysql -r mysql
[root@node2 ~]# chown -R mysql.mysql /data/

解決軟件依賴關係:

[root@node2 ~]# yum install libaio

二進制安裝設置:

[root@node2 ~]# tar xf mysql-5.5.47-linux2.6-x86_64.tar.gz -C /usr/local/
[root@node2 ~]# cd /usr/local/
[root@node2 local]# ln -sv mysql-5.5.47-linux2.6-x86_64 mysql
"mysql" -> "mysql-5.5.47-linux2.6-x86_64"
[root@node2 local]# cd mysql/
[root@node2 mysql]# chown -R root.mysql ./*
[root@node2 mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mydata/

導出頭文件、庫文件、man手冊:

[root@node2 mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql
"/usr/include/mysql" -> "/usr/local/mysql/include"
[root@node2 mysql]# vim /etc/ld.so.conf.d/mysql-5.5-x86_64.conf
/usr/local/mysql/lib
[root@node2 mysql]# ldconfig
[root@node2 mysql]# vim /etc/man.config
MANPATH /usr/local/mysql/man

添加並重讀環境變量:

[root@node2 mysql]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@node2 mysql]# source /etc/profile.d/mysql.sh

提供配置文件和服務啓動腳本:

[root@node2 mysql]# mkdir /etc/mysql
[root@node2 mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf
[root@node2 mysql]# vim /etc/mysql/my.cnf
[mysqld]
datadir = /data/mydata
thread_concurrency = 2
[root@node2 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@node2 mysql]# chmod +x /etc/rc.d/init.d/mysqld

啓動服務:

[root@node2 mysql]# chkconfig --add mysqld
[root@node2 mysql]# chkconfig mysqld on
[root@node2 mysql]# service mysqld start
安裝memcached:

memcached依賴於libevent API,yum安裝過程當中會自動解決依賴關係從base源中下載並安裝libevent;

[root@node3 ~]# yum install memcached

啓動服務:

[root@node3 ~]# chkconfig memcached on
[root@node3 ~]# service memcached start

分別在nginx和php-fpm上部署站點文件:

[root@node0 ~]# mkdir /web
[root@node0 ~]# tar xf wordpress-4.4.1-zh_CN.tar.gz 
[root@node0 ~]# mv wordpress /web/www
[root@node0 ~]# cd /web/www/
[root@node0 www]# cp wp-config-sample.php wp-config.php
[root@node0 www]# vim wp-config.php
define('DB_NAME', 'webdb');
define('DB_USER', 'webuser');
define('DB_PASSWORD', 'webpass');
define('DB_HOST', '192.168.1.6');
[root@node0 ~]# scp -r /web/www node1:/web/

建立數據庫並受權:

[root@node2 ~]# mysql
mysql> create database webdb;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on *.* to webuser@'192.168.1.%' identified by 'webpass';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

站點安裝:

輸入圖片說明

輸入圖片說明

安裝memadmin狀態頁面:

  MemAdmin是一款可視化的Memcached管理與監控工具,使用PHP開發,體積小,操做簡單。

主要功能:

  服務器參數監控:STATS、SETTINGS、ITEMS、SLABS、SIZES實時刷新

  服務器性能監控:GET、DELETE、INCR、DECR、CAS等經常使用操做命中率實時監控

  支持數據遍歷,方便對存儲內容進行監視

  支持條件查詢,篩選出知足條件的KEY或VALUE

  數組、JSON等序列化字符反序列顯示

  兼容memcache協議的其餘服務,如Tokyo Tyrant (遍歷功能除外)

  支持服務器鏈接池,多服務器管理切換方便簡潔

nginx和php都要安裝memadmin狀態頁面:

[root@node0 ~]# tar xf memadmin-1.0.12.tar.gz -C /web/www/

登錄添加服務器:

輸入圖片說明

輸入圖片說明

輸入圖片說明

輸入圖片說明

相關文章
相關標籤/搜索