一、LNMMP的解释及架构示意图:

1、LNMMP的解释:所谓LNMMP,是指在linux系统上搭建nginx网站服务器、mariadb数据库服务器、php服务器、memcached缓存服务器,使它们能够协同工作。

2、LNMMP的工作机制:php服务器要能够与nginx服务器联系,得基于fastcgi协议,来响应动态内容;php与mariadb基于mysqld协议从mariadb数据库取得数据;php有时会把从mariadb数据库中取得的数据缓存在memcached缓存服务器上,这样,当php再次取数据时,会先从memcached中取数据,这样加快了php服务器的响应速度。

3、LNMMP架构示意图:

图1:

wKioL1NdQ2SCogBcAADCdkKSEx4931.jpg

图2:

wKiom1NdQ-KADFv4AADa1Wr_TF0654.jpg

说明:图1是LNMMP的架构示意图,图1显示了nginx、php、mariadb、memcached各是一台服务器;

     图2也是LNMMP的架构示意图,只不过我把nginx、php和mariadb安装在172.16.35.1这台服务器上了,而把memcached安装在另一台服务器172.16.35.2上面;

     此次试验采用图2的架构模式,以下为具体安装步骤。

二、编译安装nginx-1.4.7:

1、解决依赖关系:

[root@www ~]# yum groupinstall "Development Tools" "Server Platform Deveopment" "Desktop platform Development"
[root@www ~]#  yum install openssl-devel pcre-devel

2、添加用户nginx,实现以之运行nginx服务进程:

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

3、解压nginx源码包,然后执行配置脚本:

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

选项说明:

--prefix=/usr                                                    --nginx的安装路径

--sbin-path=/usr/sbin/nginx                                      --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                              --pid文件路径
--lock-path=/var/lock/nginx.lock                                 --锁文件路径
--user=nginx                                                     --以nginx这个用户运行nginx进程
--group=nginx                                                    --以nginx组运行nginx进程
--with-http_ssl_module                                           --支持ssl模块
--with-http_flv_module                                           --支持http的流媒体模块
--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/                     --nginx作为缓存服务器,将用户请求的内容缓存在本地一份
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/                    --后端fcgi服务器所临时使用的文件的存放目录
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi                      --反向代理对于python开发的web页面所使用的协议
--http-scgi-temp-path=/var/tmp/nginx/scgi                        --反向代理用户请求的一种机制,是对cgi协议的扩展

--with-pcre                                                      --支持的pcre程序

4、为nginx提供SysV init脚本:

[root@www ~]# vim /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

5、而后为此脚本赋予执行权限:

[root@www ~]# chmod +x /etc/rc.d/init.d/nginx

6、将nginx添加至服务管理列表,并让其开机自动启动:

[root@www ~]# chkconfig --add nginx
[root@www ~]# chkconfig nginx on

7、而后就可以启动服务并测试了:

[root@www ~]# service nginx start
Starting nginx:                                            [  OK  ]

8、让nginx的主配置文件语法高亮:

[root@www ~]# mkdir .vim/syntax -pv
mkdir: created directory `.vim'
mkdir: created directory `.vim/syntax'
[root@www ~]# cd .vim/syntax/

   下载nginx.vim,下载页面:http://www.vim.org/scripts/script.php?script_id=1886

[root@www syntax]# ls
nginx.vim
[root@www syntax]# cd ..
[root@www .vim]# vim filetype.vim
au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif

9、验证主配置文件:

[root@www ~]# vim /etc/nginx/nginx.conf

wKiom1Nc1JjjBHiNAAEBdmmCn9w237.jpg

三、mariadb-10.0.10的安装配置:

1、为mariadb的数据存放专门准备一个磁盘的一个分区,并使其自动挂载到/mydata目录:

1
2
[root@www ~] # tail -1 /etc/fstab
/dev/sdb1 /   mydata     ext4    defaults        0 0

2、创建mysql用户和mysql组,并指定其家目录为/mydata/data,然后在/mydata下面创建data目录,把data目录作为mariadb的数据存放目录:

1
2
3
[root@www ~] # groupadd -r -g 306 mysql
[root@www ~] # useradd -g mysql -u 306 -r -d /mydata/data mysql
[root@www ~] # mkdir /mydata/data

3、解压mariadb数据库,并把解压后的mariadb目录剪切到/usr/local下面,并重命名为mysql:

1
2
[root@www ~] # tar xf mariadb-10.0.10-linux-x86_64.tar.gz
[root@www ~] # mv mariadb-10.0.10-linux-x86_64 /usr/local/mysql

说明:此mariadb包解压后不需要编译安装,只需执行数据库初始化等操作。

4、更改mariadb安装路径下的所有文件的属主为root,属组为mysql:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@www mysql] # pwd
/usr/local/mysql
[root@www mysql] # chown -R root:mysql ./*
[root@www mysql] # ll
total 204
drwxr-xr-x  2 root mysql  4096 Apr 11 01:01 bin
-rw-r--r--  1 root mysql 17987 Mar 31 01:56 COPYING
-rw-r--r--  1 root mysql 26545 Mar 31 01:56 COPYING.LESSER
-rw-r--r--  1 root mysql  1553 Mar 31 01:56 CREDITS
drwxr-xr-x  3 root mysql  4096 Apr 11 00:59 data
drwxr-xr-x  2 root mysql  4096 Apr 11 01:02 docs
drwxr-xr-x  3 root mysql  4096 Apr 11 01:02 include
-rw-r--r--  1 root mysql  8694 Mar 31 01:56 INSTALL-BINARY
drwxr-xr-x  3 root mysql  4096 Apr 11 01:02 lib
drwxr-xr-x  4 root mysql  4096 Apr 11 00:59 man
drwxr-xr-x 11 root mysql  4096 Apr 11 01:03 mysql- test
-rw-r--r--  1 root mysql 90897 Mar 31 01:56 README
drwxr-xr-x  2 root mysql  4096 Apr 11 01:02 scripts
drwxr-xr-x 27 root mysql  4096 Apr 11 01:02 share
drwxr-xr-x  4 root mysql  4096 Apr 11 01:02 sql-bench
drwxr-xr-x  4 root mysql  4096 Apr 11 00:59 support-files
-rw-r--r--  1 root mysql  2311 Mar 31 01:56 TODO

5、更改mariadb数据目录的属主属组为mysql:

1
[root@www mysql] # chown -R mysql.mysql  /mydata/data/

6、以mysql用户的身份来初始化mariadb服务器,并指定mariadb的数据存放目录为/mydata/data:

1
[root@www mysql] # scripts/mysql_install_db --user=mysql --datadir=/mydata/data/

7、提供mariadb的服务脚本,使脚本mysqld有执行权限,并加入系统服务,让其开机自启动:

1
2
3
4
5
6
7
8
[root@www mysql] # pwd
/usr/local/mysql
[root@www mysql] # cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@www mysql] # chmod +x /etc/rc.d/init.d/mysqld
[root@www mysql] # chkconfig --add mysqld
[root@www mysql] # chkconfig mysqld on
[root@www mysql] # chkconfig --list mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

8、创建mariadb的主配置文件放置目录,为mariadb提供主配置文件,然后编辑主配置文件my.cnf,并设置thread_concurrency=2(CPU核数的两倍),并指定mariadb的数据存放路径:

1
2
3
4
5
[root@www mysql] # mkdir /etc/mysql
[root@www mysql] # pwd
/usr/local/mysql
[root@www mysql] # cp support-files/my-large.cnf /etc/mysql/my.cnf
[root@www mysql] # vim /etc/mysql/my.cnf
thread_concurrency = 2
datadir = /mydata/data
innodb_file_per_table = ON

9、在PATH环境变量中添加mariadb的二进制程序的路径,并使配置立即生效:

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

10、启动mysqld服务,然后连接mariadb服务器:

1
2
3
4
5
6
7
8
9
[root@www ~] # service mysqld start
Starting MySQL..                                           [  OK  ]
[root@www ~] # mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.0.10-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

四、编译安装php-5.4.26:

1、让编译的php支持mcrypt、mhash扩展和libevent等,安装这些依赖包:

[root@www ~]# yum -y install mhash-devel  libmcrypt-devel libcurl-devel bzip2-devel gd-devel libxml2-devel libevent-devel

说明:libevent是一个异步事件通知库文件,其API提供了在某文件描述上发生某事件时或其超时时执行回调函数的机制,它主要用来替换事件驱动的网络服务器上的event loop机制。目前 来说, libevent支持/dev/poll、kqueue、select、poll、epoll及Solaris的event ports。

2、解压php-5.4.26的源码包,然后执行配置脚本,编译并安装:

[root@www ~]# tar xf php-5.4.26.tar.bz2
[root@www php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm  --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml  --with-mhash --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
[root@www php-5.4.26]# make
[root@www php-5.4.26]# make install

说明:  

       --prefix=/usr/local/php5 :                        指定php的安装目录;

        --with-mysql=/usr/local/mysql :                  让php可以到mysql目录下找各种数据文件;

       --with-openssl :                                  让php支持openssl功能,配置openssl后将来可以在网络安全访问网页;
       --with-mysqli=/usr/local/mysql/bin/mysql_config : 让php寻找mysql的接口程序,相当于驱动,方便php与mysql通信;
       --enable-mbstring :                               启动多字节字串;
       --with-freetype-dir :                             支持freetype,它是一个字体解析工具;
       --with-jpeg-dir --with-png-dir :                  让php能够处理jpeg和png图片;
       --with-zlib :                                     支持压缩库;
       --with-libxml-dir=/usr --enable-xml  :            启动xml功能和指定xml的路径;
       --enable-sockets :                                支持基于socket通信;
       --enable-fpm :                                    启动php的fastcgi协议功能;
       --with-mcrypt :                                   让php支持mcrypt扩展功能;
       --with-config-file-path=/etc/php5 :               指定php的配置文件路径;
       --with-config-file-scan-dir=/etc/php5.d :         把/etc/php.d下的配置文件作为主配置文件的一部分;      

       --with-bz2 :                                      支持bz2压缩格式;

       --with-curl:                                       支持curl。

3、为php提供配置文件:

[root@www php-5.4.26]# pwd
/root/php-5.4.26
[root@www php-5.4.26]# cp php.ini-production /etc/php.ini

4、为php-fpm提供Sysv init脚本,并将其添加至服务列表:

[root@www php-5.4.26]# pwd
/root/php-5.4.26
[root@www php-5.4.26]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@www php-5.4.26]# chmod +x /etc/rc.d/init.d/php-fpm
[root@www php-5.4.26]# chkconfig --add php-fpm
[root@www php-5.4.26]# chkconfig php-fpm on

5、为php-fpm提供配置文件:

[root@www php-5.4.26]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

6、编辑php-fpm的配置文件:

[root@www php-5.4.26]# vim /usr/local/php/etc/php-fpm.conf

7、配置fpm的相关选项的值,并启用pid文件:

pid = /usr/local/php/var/run/php-fpm.pid
error_log = /var/log/php-fpm.log
pm.max_children = 128
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_requests = 500
pm.status_path = /status
ping.path = /ping
ping.response = pong
rlimit_files = 10240

说明:

pid = /usr/local/php/var/run/php-fpm.pid                  --pid文件所在的位置

error_log = /var/log/php-fpm.log                          --错误日志所在位置

pm.max_children = 128                                     --最大子进程数

pm.start_servers = 5                                      --默认启动的子进程数

pm.min_spare_servers = 2                                  --最小空闲进程数

pm.max_spare_servers = 5                                  --最大空闲进程数

pm.max_requests = 500                                     --每个子进程最多服务多少个用户请求后退出并重新fork子进程

pm.status_path = /status                                  --启动php-fpm的状态信息

ping.path = /ping                                         --探测服务器是否在线

ping.response = pong                                      --响应ping探测

rlimit_files = 10240                                      --进程打开的文件的最大数

8、启动php-fpm,可以看到php-fpm服务的9000端口已处于监听状态:


[root@www ~]# service php-fpm start
Starting php-fpm  done
[root@www ~]# ss -tnlp | grep 9000
LISTEN     0      128               127.0.0.1:9000                     *:*      users:(("php-fpm",39323,7),("php-fpm",39324,0),("php-fpm",39325,0),("php-fpm",39326,0),("php-fpm",39327,0),("php-fpm",39328,0))

五、整合nginx和php,使二者结合起来工作:

1、编辑/etc/nginx/nginx.conf,启用如下选项:

location ~ \.php$ {
            root           /www/yanhai.com;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:

[root@www ~]# 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;

3、创建目录/www/yanhai.com,把它作为nginx网站服务器的网页根目录,然后再该目录下创建index.php的测试页面,测试php是否能正常工作:

[root@www ~]# mkdir -pv /www/yanhai.com
[root@www ~]# cd /www/yanhai.com/
[root@www yanhai.com]# vim index.php
<h1>www.yanhai.com</h1>
<?php
phpinfo();
?>

4、编辑nginx的主配置文件:

[root@www ~]# vim /etc/nginx/nginx.conf

wKioL1NdA1-D4TRrAAFTgRWWbvs406.jpg

wKioL1NdHufzm6olAADuBtMR9PU396.jpg

5、检验nginx的主配置文件是否有语法错误,然后重启nginx服务:

[root@www ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www ~]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

6、访问网页,验证nginx是否和php一起工作:

wKioL1NdBK7Bp-zuAAK3rGt0l8Q959.jpg

7、访问网页,查看php-fpm的状态信息和检验php的在线信息:

wKiom1NdH4HT4XZ0AAESldbSIj8854.jpg

wKioL1NdH1jy1A_FAAA8wMT0nCM735.jpg

六、安装xcache,为php加速:

1、解压xcache源码包,进入解压后的xcache目录,执行phpize命令,它是将xcache编译为php的模块或扩展,执行xcache的脚本配置文件,然后编译和安装:

[root@www ~]# tar xf xcache-3.0.3.tar.bz2
[root@www ~]# cd xcache-3.0.3
[root@www xcache-3.0.3]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
[root@www xcache-3.0.3]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
[root@www xcache-3.0.3]# make && make install

说明:--enable-xcache:启用xcache功能;
     --with-php-config=/usr/local/php/bin/php-config:指定php的配置接口路径,让xcache能够识别php。

2、xcache安装完成后会显示xcache模块的路径:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

3、将xcache提供的样例配置导入到php的配置文件php.ini中:

[root@www xcache-3.0.3]# mkdir /etc/php.d
[root@www xcache-3.0.3]# cp xcache.ini /etc/php.d/

4、编辑xcache.ini文件,指定xcache模块的路径,以方便php寻找:

[root@www xcache-3.0.3]# vim /etc/php.d/xcache.ini
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

5、重启php-fpm服务,以便xcache能为php提供缓存加速:

extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

6、验证xcache和php是否整合在一起:

wKioL1NdFBeB5g12AANMhBPTZuM824.jpg

七、安装配置memcached:

1、memcached的介绍:

(1)Memcached是一款开源、高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。
(2)memcached是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。
(3)Memcached是一款开发工具,它既不是一个代码加速器,也不是数据库中间件。其设计哲学思想主要反映在如下方面:
A. 简单key/value存储:服务器不关心数据本身的意义及结构,只要是可序列化数据即可。存储项由“键、过期时间、可选的标志及数据”四个部分组成;
B. 功能的实现一半依赖于客户端,一半基于服务器端:客户负责发送存储项至服务器端、从服务端获取数据以及无法连接至服务器时采用相应的动作;服务端负责接收、存储数据,并负责数据项的超时过期;
C. 各服务器间彼此无视:不在服务器间进行数据同步;
D. O(1)的执行效率;
E. 清理超期数据:默认情况下,Memcached是一个LRU缓存,同时,它按事先预订的时长清理超期数据;但事实上,memcached不会删除任何已缓存数据,只是在其过期之后不再为客户所见;而且,memcached也不会真正按期限清理缓存,而仅是当get命令到达时检查其时长。
(4)Memcached提供了为数不多的几个命令来完成与服务器端的交互,这些命令基于memcached的协议实现:
◆存储类命令:set, add, replace, append, prepend
◆获取数据类命令:get, delete, incr/decr
◆统计类命令:stats, stats items, stats slabs, stats sizes
◆清理命令: flush_all
2、memcached依赖于libevent API,因此要安装libevent,memcached不跟nginx在一个节点,memcached服务器的IP是172.16.35.2:

[root@memcached ~]# yum -y install libevent

3、安装memcached:

[root@memcached ~]# yum -y install memcached

说明:配置好yum源,指向centos6.5-x86_64系统的系统镜像文件。

4、memcached的常用选项:
-l <ip_addr>:指定进程监听的地址;
-d: 以服务模式运行;
-u <username>:以指定的用户身份运行memcached进程;
-m <num>:用于缓存数据的最大内存空间,单位为MB,默认为64MB;
-c <num>:最大支持的并发连接数,默认为1024;
-p <num>: 指定监听的TCP端口,默认为11211;
-U <num>:指定监听的UDP端口,默认为11211,0表示关闭UDP端口;
-t <threads>:用于处理入站请求的最大线程数,仅在memcached编译时开启了支持线程才有效;
-f <num>:设定Slab Allocator定义预先分配内存空间大小固定的块时使用的增长因子;
-M:当内存空间不够使用时返回错误信息,而不是按LRU算法利用空间;
-n: 指定最小的slab chunk大小;单位是字节;
-S: 启用sasl进行用户认证。

5、启动memcached服务,而memcached进程监听在11211端口:

[root@memcached ~]# service memcached start
Starting memcached:                                        [  OK  ]
[root@memcached ~]# ss -tnlp | grep 11211
LISTEN     0      128                      :::11211                   :::*      users:(("memcached",1783,27))
LISTEN     0      128                       *:11211                    *:*      users:(("memcached",1783,26))

6、使用telnet连接memcached服务器的11211端口,连接上之后使用stats命令查看memcached进程的状态信息:

[root@memcached ~]# telnet 127.0.0.1 11211
stats
STAT pid 1783
STAT uptime 233
STAT time 1398279229
STAT version 1.4.4
STAT pointer_size 64
STAT rusage_user 0.005999
STAT rusage_system 0.010998
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0

八、安装memcached的PHP扩展,使php服务器能够联系memcached缓存服务器:

1、安装PHP的memcache扩展,编译安装memcache-2.2.7.tar.gz(此工具包需要安装在php服务器上,也就是安装在172.16.35.1这个节点上):

[root@www ~]# tar xf memcache-2.2.7.tgz
[root@www ~]# cd memcache-2.2.7
[root@www memcache-2.2.7]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
[root@www memcache-2.2.7]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
[root@www memcache-2.2.7]# make && make install

说明:/usr/local/php/bin/phpize是指让php能够识别memcache这个模块。

2、memcache-2.2.7.tar.gz安装完后会有类似以下的提示:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

3、编辑php的主配置文件,在“动态模块”相关的位置添加如下一行来载入memcache扩展:

[root@www memcache-2.2.7]# vim /etc/php.ini
; extension_dir = "./"
; On windows:
; extension_dir = "ext"
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

4、重启php-fpm服务,使php能够重新装载memcache模块:

[root@www ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

5、访问主页,验证memcache是否已成为php的一个扩展模块:

wKiom1NdMdaBz50oAAJx79JdmqI588.jpg

6、网站目录中建立测试页面memcache.php,验证php是否能够连接memcached服务器:

[root@www ~]# vim /www/yanhai.com/memcache.php

wKioL1NdNCzTpQNNAAI3FH-Dkrg563.jpg

7、访问网页,验证php是否连接上memcached服务器:

wKiom1NdNZDjQZAWAACtK6rpOFE447.jpg

九、使用php形式的web页面方式管理memcached:

1、进入nginx服务器网页根目录,把memadmin-master.zip解压在/www/yanhai目录中:

[root@www ~]# cd /www/yanhai.com/
[root@www yanhai.com]# unzip mem
memadmin-master.zip  memcache.php       
[root@www yanhai.com]# unzip memadmin-master.zip
[root@www yanhai.com]# ls
index.php  memadmin-master  memadmin-master.zip  memcache.php

说明:memadmin-master.zip此压缩文件要放在php和nginx在一起的服务器上,即节点172.16.35.1上。

2、访问管理memcached的网站:

wKiom1NdP1Tz8Y2QAAFZcv9PwJo843.jpg

wKioL1NdPy6inzHOAAKsGw_7ws8907.jpg

wKiom1NdP2qDADDdAAOIyxJ_ZT0822.jpg

十、至此,LNMMP已搭建完成~~~。。。