思路:
   搭建wordpress个人博客,nginx做动静态文件分离,nginx处理静态内容(本地)

   动态内容由上游服务器php-fpm来完成


规划:

   nginx     反代后端 php-fpm            172.16.43.1
   php-fpm,php-extentsion(memcache),php-extentsion(xcache) 172.16.43.2
   mariadb 10       172.16.43.3


实现过程:

1. nginx安装及使用(172.16.43.1)

#nginx的安装需要环境: gcc-c++,pcre,zlib,nginx
.
# i) 安装gcc编译器
yum -y install gcc-c++
.
# ii) 安装pcre
tar xf pcre-8.33.tar.gz
cd pcre-8.33
./configure --prefix=/usr/local/pcre
make && make install
.
# iii) 安装zlib
tar xf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib
make && make install
.
# iv) 安裝nginx
tar xf nginx-1.5.7.tar.gz
cd nginx-1.5.7
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-pcre=/root/pcre-8.30 --with-zlib=/root/zlib-1.2.8
make && make install
.
# v) 为nginx添加启动脚本
# vim /etc/rc.d/init.d/nginx
#!/bin/bash
# nginx Startup script
# chkconfig: - 85 15
# description: nginx Startup script
# processname: nginx
#pidfile: /var/local/nginx/nginx.pid
#config: /usr/local/nginx/nginx.conf
nginxd=/usr/local/nginx/nginx
nginx_config=/etc/nginx/nginx.conf
nginx_pid=/var/local/nginx/nginx.pid
RETVAL=0
prog="nginx"
.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
.
stop() {
    echo -n $"Stopping $prog: "
    killproc $nginxd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        stop
        start
        ;;
    status)
        status $prog
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL
.
# vi) 为nginx做配置处理
chmod a+x /etc/rc.d/init.d/nginx
chkconfig nginx on
useradd -r -s /sbin/nologin nginx -U
.
# vii) 配置规划中的内容
vim /etc/nginx/nginx.conf
############## 配置文件开始 ################
user nginx;
#启动进程,一般和CPU数量一致
worker_processes 2;
#[ debug | info | notice | warn | error | crit ] 错误日志的级别及位置
error_log /var/log/nginx/error.log debug;
#一个nginx进程打开的最多文件描述符数目,最好与ulimit -n的值保持一致
worker_rlimit_nofile 51200;
events {
    #工作进程的最大连接数量,根据硬件调整
    worker_connections 4096;
    #[ kqueue | rtsig | epoll | /dev/poll | select | poll ] 使用epoll(linux2.6的高性能方式)
    use epoll;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    # 内核直接返回请求,提升性能
    sendfile        on;
    # 保持长连接时间
    keepalive_timeout  5;
    # 开启gzip服务器压缩功能
    gzip  on;
    server {
        listen 80;
        resolver www.king.com;
        add_header testserver $server_addr;
        location / {
            root   /var/www/html;
            index  index.html index.htm;
        }
        location ~* \.(jpg|png|gif|jpeg|js|css)$ {
            root /var/www/html;
        }
        location ~ \.php$ {
            # 这里就是关键,root 这里必须与上游服务器文件路径对应
            root /var/www/html;
            fastcgi_pass   172.16.43.2:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
.
# vi) 替换fastcgi参数
# 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;

LNMMP架构搭建wp个人博客实现动静态内容分离_第1张图片

2. php-fpm 安装(172.16.43.2)

# i) 解决php安装依赖
yum -y groupinstall "Desktop Platform Development"
yum -y install libmcrypt-devel
yum -y install bzip2-devel
.
# ii) 安装php with fpm
tar xf php-5.4.19.tar.bz2
cd php-5.4.19
./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 --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
make && make install
.
# iii) 为php提供配置文件:
cp php.ini-production /etc/php.ini
.
# iv) 配置php-fpm
#为php-fpm提供SysV init脚本,并将其添加至服务列表:sapi在源码包下
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
为php-fpm提供配置文件:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pid = /usr/local/php/var/run/php-fpm.pid
listen = 172.16.43.2:9000

3. php拓展xache安装(172.16.43.2)

# i) 安装xcache
tar xf xcache-3.1.0.tar.bz2
cd xcache-3.1.0
/usr/local/php/bin/phpize
./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
make && make install
# 安装结束时,会出现类似如下行, 将后半句复制
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
.
# ii) 编辑php.ini,整合php和xcache:
# 首先将xcache提供的样例配置导入php.ini
mkdir /etc/php.d
# xcache.ini文件在xcache的源码目录中。
cp xcache.ini /etc/php.d
# 接下来编辑/etc/php.d/xcache.ini 修改为如下:
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

4. php拓展memcache安装(172.16.43.2)

# i) 安装memcache
tar xf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
make && make install
# 安装结束时,会出现类似如下行, 将后半句复制
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
.
# ii) 编辑/etc/php.ini,在“dynamically loaded extension”相关的位置添加如下一行来载入memcache扩展:
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
.
# iii) 重启php-fpm服务
service php-fpm restart

LNMMP架构搭建wp个人博客实现动静态内容分离_第2张图片

LNMMP架构搭建wp个人博客实现动静态内容分离_第3张图片

5. MariaDB安装与配置(172.16.43.3)

# i) 创建mysql的数据目录
mkdir /data
groupadd -r mysql
useradd -g mysql -r -s /sbin/nologin -M -d /data mysql
chown -R mysql:mysql /data
.
# ii) 安装二进制mysql
tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv mysql-5.5.33-linux2.6-x86_64 mysql
cd mysql
chown -R mysql:mysql  .
mysql/scripts/mysql_install_db --user=mysql --datadir=/data
chown -R root  .
# 提供mysql的配置文件
cp support-files/my-large.cnf  /etc/my.cnf
# 需要添加如下行指定mysql数据文件的存放位置:
datadir = /data
.
# iii) 为mysql提供sysv服务脚本:
cd /usr/local/mysql
cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
添加至服务列表:
chkconfig --add mysqld
chkconfig mysqld on
echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
.
# iv) 启动服务并授权php服务器账号访问
service mysqld restart
mysql
grant all on *.* to 'wordpress'@'172.16.%.%' identified by '123456';
flush privileges;

6. 配置测试wordpress
   将wordpress的源代码分别拷贝到172.16.43.1与172.16.43.2对应目录
   (为什么是双份这里我们就可以理解了 :)
   unzip wordpress-3.3.1-zh_CN.zip

LNMMP架构搭建wp个人博客实现动静态内容分离_第4张图片

LNMMP架构搭建wp个人博客实现动静态内容分离_第5张图片