192.168.1.208 Nginx负载服务器
192.168.1.210 webA服务器 PHP memcache xcache mysql
192.168.1.211 webB服务器 PHP memcache xcache
# 注意:freetype在生成验证码图片需要用,所以必须要安装的
[root@iZ23g4snm6gZ soft]# yum install openssl-devel libxml2 libxml2-devel curl-devel libevent
[root@iZ23g4snm6gZ soft]# yum install libpng libpng-devel libjpeg libjpeg-devel freetype-devel gd gd-devel
# 源码包安装libiconv
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14/
./configure --prefix=/usr/local/libiconv
make
make install
# 源码包安装libiconv
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure --prefix=/usr/local/libmcrypt/
make
make install
开始编译PHP(Nginx)
./configure --prefix=/usr/local/php/ \
--with-config-file-path=/usr/local/php/etc/ \
--enable-fpm --with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-zlib \
--with-libxml-dir \
--enable-sockets \
--with-curl \
--with-jpeg-dir \
--with-png-dir \
--with-gd \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir= \
--enable-gd-native-ttf \
--with-xmlrpc \
--with-openssl \
--with-mhash \
--with-mcrypt=/usr/local/libmcrypt/ \
--with-pear \
--enable-mbstring \
--enable-sysvshm \
--enable-zip \
--with-mysql=/usr/local/mysql/ \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-mysql-sock \
--with-pdo-mysql \
--disable-fileinfo \
make
make install
设置PHP配置文件
cp php.ini-production /usr/local/php/etc/php.ini
# 拷贝模板文件为php-fpm配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf // 编辑
user = www // 设置php-fpm运行账号为www 默认账号为nginx
group = www // 设置php-fpm运行组为www
pid = run/php-fpm.pid // 取消前面的分号
:wq! // 保存退出
php-fpm启动脚本
#! /bin/sh
### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/usr/local/php
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"
wait_for_pid () {
try=0
while test $try -lt 35 ; do
case "$1" in
'created')
if [ -f "$2" ] ; then
try=''
break
fi
;;
'removed')
if [ ! -f "$2" ] ; then
try=''
break
fi
;;
esac
echo -n .
try=`expr $try + 1`
sleep 1
done
}
case "$1" in
start)
echo -n "Starting php-fpm "
$php_fpm_BIN $php_opts
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi
wait_for_pid created $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Gracefully shutting down php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -QUIT `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
force-quit)
echo -n "Terminating php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -TERM `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
echo -n "Reload service php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -USR2 `cat $php_fpm_PID`
echo " done"
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload}"
exit 1
;;
esac
设置php-fpm开机自启动
mv php-fpm /etc/init.d/ // 移动php-fpm脚本到init.d目录下
chmod a+x /etc/init.d/php-fpm // 添加执行权限
chkconfig --add php-fpm // 添加开机启动配置
chkconfig --level 2345 php-fpm on // 配置开机启动权限级别
# 解压
tar -zxvf xcache-3.2.0.tar.gz
cd xcache-3.2.0
/usr/local/php/bin/phpize
# 配置
./configure --enable-xcache--enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php/bin/php-config
make #编译
make install #安装
touch /tmp/xcache #创建文件
chmod 777 /tmp/xcache #设置权限
# 创建xcache管理员密码为123456
echo -n "123456" | md5sum #记住类似下面一行代码(md5加密之后的密码),后面会用到
e10adc3949ba59abbe56e057f20f883e
4、拷贝xcache后台管理程序到网站根目录
cp -r /data/soft/opcode/xcache-3.2.0/htdocs/ /usr/local/httpd/htdocs/xcache/
chown www.www -R /usr/local/httpd/htdocs/xcache/ #设置xcache目录和网站目录权限相同
# 配置php支持xcache
vim /usr/local/php/etc/php.ini #编辑配置文件,在Dynamic Extensions后添加以下内容
[xcache-common]
extension = xcache.so
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "xcache"
xcache.admin.pass = "e10adc3949ba59abbe56e057f20f883e"
[xcache]
xcache.shm_scheme ="mmap"
xcache.size=60M
xcache.count =1
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=64M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache.readonly_protection = On
xcache.mmap_path ="/tmp/xcache"
xcache.coredump_directory =""
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =""
:wq! #保存退出
# Nginx测试
service php-fpm restart #重启php-fpm
service nginx restart #重启nginx
# Apache测试
service httpd restart #重启apache
浏览器打开网站根目录下面的xcache
输入用户名xcache 密码123456
# 扩展包安装
yum -y install zlib zlib-devel openssl openssl-devel
yum -y install make gcc gcc-c++ ncurses-devel pcre-devel zlib-devel
# 安装Nginx# 安装pcre (支持nginx伪静态)
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
cd /usr/local/src
mkdir /usr/local/pcre // 创建安装目录
tar zxvf pcre-8.30.tar.gz
cd pcre-8.30
./configure --prefix=/usr/local/pcre // 配置
make
make install
# 配置Nginx
[root@admin local]# groupadd www #添加www组
[root@admin local]# useradd -g www www -s /bin/false // 不允许www用户直接登录系统
[root@admin local]# cd /data/soft/
[root@admin local]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@admin local]# tar -zxvf nginx-1.6.2.tar.gz
[root@admin local]# cd nginx-1.6.2/
[root@admin nginx]# ./configure --prefix=/usr/local/nginx
[root@admin nginx]# make
[root@admin nginx]# make install
# Nginx启动脚本
#!/bin/bash
# nginx This shell script takes care of starting and stopping
# nginx
#
# chkconfig: - 13 68
# description: nginx is a web server
### BEGIN INIT INFO
# Provides: $named
# Short-Description: start|stop|status|restart|configtest
### END INIT INFO
#variables
NGINX_BIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
NETSTAT="/bin/netstat"
alter=$1
prog=nginx
#load system function
. /etc/rc.d/init.d/functions
#function:echo ok or error
function if_no {
if [ $2 == 0 ]; then
echo -n $"$1 ${prog}:" && success && echo
else
echo -n $"$1 ${prog}:" && failure && echo
fi
}
#start nginx
function start {
rm -f ${NGINX_PID} 2>/dev/null
if [ -s ${NGINX_PID} ]; then
echo "nginx already running"
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
${NGINX_BIN} -c ${NGINX_CONF}
if_no start $?
else
${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID}
if_no start $?
fi
fi
}
#stp nginx
function stop {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -QUIT
if_no stop $?
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
if_no stop 0
else
rm -f ${NGINX_PID} 2>/dev/null
kill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1`
if_no stop $?
fi
fi
}
function restart {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -HUP
if_no restart $?
else
stop
sleep 1
start
fi
}
function status {
${NETSTAT} -tnpl | grep nginx | grep LISTEN
[ $? == 0 ] && echo "nginx is running" || echo "nginx is not running"
}
function configtest {
${NGINX_BIN} -t
}
case $alter in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
configtest)
configtest
;;
*)
echo "use:${NGINX} {start|stop|restart|status|configtest}"
;;
esac
# 配置Nginx自启动脚本
chmod +x /etc/init.d/nginx
/etc/init.d/nginx start 或 service nginx start // 启动nginx
/etc/init.d/nginx stop 或 service nginx stop // 关闭nginx
/etc/init.d/nginx restart 或 service nginx restart // 重启nginx
chkconfig --add nginx
chkconfig --level 2345 nginx on
vim /usr/local/nginx/conf/nginx.conf
--------- nginx.conf文件内容:---------
# 首行user去掉注释,修改Nginx运行组为www www;
# 必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
user www www;
worker_processes 1;
# 开启nginx错误日志
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
client_max_body_size 2m;
#gzip on;
# 包含域名配置文件( 支持通配符)
include vhost/*.conf;
}
--------- 配置 fastcgi.conf文件:---------
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 脚本文件请求的路径
fastcgi_param QUERY_STRING $query_string; # 请求的参数;如?app=123
fastcgi_param REQUEST_METHOD $request_method; # 请求的动作(GET,POST)
fastcgi_param CONTENT_TYPE $content_type; # 请求头中的Content-Type字段
fastcgi_param CONTENT_LENGTH $content_length; # 请求头中的Content-length字段。
fastcgi_param SCRIPT_NAME $fastcgi_script_name; # 脚本名称
fastcgi_param REQUEST_URI $request_uri; # 请求的地址不带参数
fastcgi_param DOCUMENT_URI $document_uri; # 与$uri相同。
fastcgi_param DOCUMENT_ROOT $document_root; # 网站的根目录。在server配置中root指令中指定的值
fastcgi_param SERVER_PROTOCOL $server_protocol; # 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
fastcgi_param GATEWAY_INTERFACE CGI/1.1; # cgi 版本
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; # nginx 版本号,可修改、隐藏
fastcgi_param REMOTE_ADDR $remote_addr; # 客户端IP
fastcgi_param REMOTE_PORT $remote_port; # 客户端端口
fastcgi_param SERVER_ADDR $server_addr; # 服务器IP地址
fastcgi_param SERVER_PORT $server_port; # 服务器端口
fastcgi_param SERVER_NAME $server_name; # 服务器名,域名在server配置中指定的server_name
#fastcgi_param PATH_INFO $path_info; # 可自定义变量
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
--------- 配置虚拟主机公用配置文件server.conf:---------
# php文件访问配置
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# 静态文件缓存30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
# js,css文件缓存15个小时
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
--------- 配置虚拟主机文件 vhost/yphp.cn.conf:---------
server {
listen 80;
# 配置域名
server_name www.yphp.cn yphp.cn;
# 配置网站目录
root /usr/local/nginx/html/yphp.cn;
# 配置域名重定向
if ($host != 'www.yphp.cn' ) {
rewrite ^/(.*)$ http://www.yphp.cn/$1 permanent;
}
location / {
# 配置rewrite
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
# include /usr/local/nginx/html/yphp/.htaccess;
# rewrite ^/(.+)/(.+)[/]?$ /index.php?m=$1&a=$2 last;
# 配置默认访问文件
index index.php index.html index.htm;
}
# 包含虚拟主机公用配置文件
include server.conf;
}
# Nginx 负载配置
up_stream phpServer{
server 192.168.1.210:80 weight=1 max_fails=2 fail_timeout=3
server 192.168.1.211:80 weight=1 max_fails=2 fail_timeout=3
}
location ~* \.(jpg|jpeg|gif|png){
proxy_pass http://phpServer;
}
# 重启服务器
/etc/init.d/nginx stop # 停止nginx 服务
/etc/init.d/nginx start # 启动nginx 服务