Nginx+PHP高并发、动态解析
TomcatJSP动态解析
Apache稳定的HTML静态访问
一、整体介绍:
1.架构图
当前开发版:Nginx1.1.5|Nginx/windows1.1.5(更新记录)(2011年10月6日)
当前稳定版:Nginx1.0.8|Nginx/windows1.0.8(更新记录)(2011年10月1日)
历史稳定版:Nginx0.8.54|Nginx/windows0.8.55(更新记录)(2011年7月19日)
历史稳定版: Nginx 0.7.69 | Nginx/windows 0.7.69 (更新记录) (2011年7月19日)Nginx有低内存占用,高性能高并发访问,使得很多新平台的搭建、旧应用的迁移开始应用nginx,话说nginx不得不说张宴,就像谈LVS不得不说章文嵩一样;
张宴简历:
张宴,就职于北京金山软件公司,金山游戏官方网站──逍遥网系统架构师,技术支持部平台组组长。曾在新浪网、赶集网等公司任系统工程师、系统架构师,工作内容主要涉及:服务器系统架构设计与部署、系统运维与调优、网络故障解决、网站后端以及接口类PHP程序开发、Unix开源软件二次开发、服务器监控系统开发、系统运维与平台研发团队管理。
采用五台Centos5.4kernel:2.6.18-164.el5xenServer、一台XPsp3Client
IP地址分配:
提供域名www.abc.com
解析地址VIP:172.17.80.10
front1内网IP192.168.1.11
front2内网IP192.168.1.12
前端:Nginx只做高可用和代理
后端:web集群有 nginx PHP 请求;tomcat 动态jsp .do文件;apache 静态html 等软件版本:
nginx:nginx-1.1.6.tar.gz//下载:www.nginx.org
tomcat:apache-tomcat-7.0.21.tar.gz
JDK:jdk-1_5_0_16-linux-i586.bin
PHP:php-5.3.8.tar.gz//提供FastCGI
apache: httpd-2.2.3-31.el5 // yum install httpd phpCentos5.4制作本地yum源
#mkdir/mnt/cdrom{1,2}
#mount/dev/cdrom/mnt/cdrom1
#cp-r/mnt/cdrom1/*/mnt/cdrom2
#cd/mnt/cdrom2
#rpm-ivhcreaterepo-0.4.11-3.el5.noarch.rpm
#createrepo-g/mnt/cdrom/repodata/repomd.xml/mnt/cdrom/CentOS/
#vi/etc/yum.repos.d/server.repo
[CentOS]
name=CentOS
baseurl=file:///mnt/cdrom/CentOS
gpgcheck=0
enable=1
#yum-yinstallgccopenssl-develzlib-develpcre-devel
#yum -y install gcc gcc-c++ autoconf automake //安装编译gcc环境#tarzxvfnginx-1.1.6.tar.gz
#cdnginx-1.1.6
#useradd-s/sbin/nologin-Mnginx//添加nginx用户,没有登录shell,没有家目录
#./configure\
--prefix=/usr/local/nginx\//安装路径
--sbin-path=/usr/sbin/nginx\//可执行文件路径
--conf-path=/etc/nginx/nginx.conf\//默认为<prefix>/conf/nginx.conf最好定义到/etc下
--pid-path=/var/run/nginx/nginx.pid\//pid文件存放位置,后面将会用到
--error-log-path=/var/log/nginx/error.log\//错误日志文件,默认为<prefix>/logs/error.log
--http-log-path=/var/log/nginx/access.log\//访问日志,默认为<prefix>/logs/access.log
--lock-path=/var/lock/nginx.lock\
--user=nginx\
--group=nginx\
--with-http_stub_status_module\//以取得一些nginx的运行状态
--with-http_ssl_module\//支持https加密连接
--with-http_gzip_static_module\//静态缓存模块
--with-http_realip_module\//让Nginx透明获取客户端IP
--http-client-body-temp-path=/var/tmp/nginx/client/\//指定http客户端请求缓存文件存放目录
--http-proxy-temp-path=/var/tmp/nginx/proxy/\//指定http反向代理缓存文件存放目录
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ //指定FastCGI缓存文件存放目录#vim/etc/init.d/nginxd
#############################################BYhanfeng
#datetime 20111031#makerequireddirectories
user=`nginx-V2>&1|grep"configurearguments:"|sed's/[^*]*--user=\([^]*\).*/\1/g'-`
options=`$nginx-V2>&1|grep'configurearguments:'`
foroptin$options;do
if[`echo$opt|grep'.*-temp-path'`];then
value=`echo$opt|cut-d"="-f2`
if[!-d"$value"];then
#echo"creating"$value
mkdir-p$value&&chown-R$user$value
fi
fi
done
}[-x$nginx]||exit5
[-f$NGINX_CONF_FILE]||exit6
make_dirs
echo-n$"Starting$prog:"
daemon$nginx-c$NGINX_CONF_FILE
retval=$?
echo
[$retval-eq0]&&touch$lockfile
return$retval
}echo-n$"Stopping$prog:"
killproc$prog-QUIT
retval=$?
echo
[$retval-eq0]&&rm-f$lockfile
return$retval
}configtest||return$?
stop
sleep1
start
}configtest||return$?
echo-n$"Reloading$prog:"
killproc$nginx-HUP
RETVAL=$?
echo
}restart
}$nginx-t-c$NGINX_CONF_FILE
}status$prog
}rh_status>/dev/null2>&1
}start)
rh_status_q&&exit0
$1
;;
stop)
rh_status_q||exit0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q||exit7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q||exit0
;;
*)
echo$"Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit2
esac
############################################
保存nginxd脚本,赋予执行权限,添加服务和开机启动
#chmod+x/etc/init.d/nginxd
#chkconfig --add nginxd//#description:nginxisaWorldWideWebserver.Itisusedtoserve
#chkconfig--level2345nginxdon
#servicenginxdstart//先启动nginx看看,没问题的话就OK了
Starting nginx: [ OK ]http://172.17.80.11

#[email protected]:/root
#scp /etc/nginx/nginx.conf [email protected]:/etc/nginx/nginx.conf1.在front1、front2配置
A:动静分离说明:
有前置nginx 做反向代理,采用nginx的location做动静分离,将静态HTML网页、图片、JS、CSS等使用后端nginx或apache处理,以便得到更快的速度;将.jsp、.jspx、.do等交给后端tomcat来处理,B:负载均衡的说明:
此处采用nginx的proxy_pass将location做动静分离后的jsp、do等jsp程序文件分发到后端upstreamd模块中tomcat集群上,rewrite做正则分发,此时也将应用到nginx经典之处的IP哈希(ip_hash)模块,这样每个访客固定访问一个后端web服务器,可以解决session的问题;
2.分别在front1和front2上配置nginx动静分离和负载均衡
#vi/etc/nginx/nginx.conf
############################################
usernginxnginx;
worker_processes4;
events{
worker_connections1024;
}includemime.types;
default_typeapplication/octet-stream;
sendfileon;
keepalive_timeout65;
gzipon;
upstreamtomcat_server{
#ip_hash;
server192.168.1.17weight=2;
server192.168.1.20max_fails=2fail_timeout=30s;
}
upstreamapache_server{
ip_hash;
server192.168.1.19;
}
upstreamnginx_server{
ip_hash;
server192.168.1.18;
}
server
{
listen80;
server_namewww.abc.com;
location/{
indexindex.htmlindex.phpindex.htmindex.jspindex.dodefault.do;
roothtml;
if(-d$request_filename)
###############################################
{
rewrite^/(.*)([^/])$http://$host/$1$2/permanent;
}
location~\.(jsp|jspx|do)?${
proxy_set_headerHost%host;
proxy_set_headerX-Forwarded-For$remote_addr;
proxy_passhttp://tomcat_server;
}
###############################################
location~\.(php|php5)?${
proxy_set_headerHost%host;
proxy_set_headerX-Forwarded-For$remote_addr;
proxy_passhttp://nginx_server;
}
#############################################
location~\.(html|htm)?${
proxy_set_headerHost%host;
proxy_set_headerX-Forwarded-For$remote_addr;
proxy_passhttp://apache_server;
}
#############################################
location~.*\.(gif|jpg|jpeg|png|bmp|swf)${
expires30d;
}
location~.*\.(js|css)?${
expires1h;
}
#############################################
}
error_page404/404.html;
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
}############################################
3.重启nginx服务
#servicenginxdrestart
Shuttingdowninterfaceeth0:[OK]
Shuttingdowninterfaceeth1:[OK]
Shuttingdownloopbackinterface:[OK]
Bringinguploopbackinterface:[OK]
Bringingupinterfaceeth0:[OK]
Bringing up interface eth1: [ OK ]1.为什么使用keepalived
软件官网:http://www.keepalived.org
软件获得:#wgethttp://www.keepalived.org/software/keepalived-1.2.2.tar.gz
keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,#vim/etc/hosts//修改front1和front2的hosts文件,保证文件信息相同
172.17.80.11front1.ha.comfront1
172.17.80.12front2.ha.comfront2
3.编译安装keepalived
#tarzxvfkeepalived-1.2.2.tar.gz
#cdkeepalived-1.2.2
#./configure--prefix=/usr/local/keepalived
#make&&makeinstall
4.修改Keepalived配置文件
#mkdir/etc/keepalived
#cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf
#cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
#cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
#cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
#chkconfig--level2345keepalivedon
############################################
@front1上的keepalived.conf
#vim/etc/keepalived/keepalived.conf
! Configuration File for keepalivednotification_email{
}
smtp_server127.0.0.1
smtp_connect_timeout30
router_idLVS_DEVEL
}stateMASTER
interfaceeth1
virtual_router_id51
mcast_src_ip192.168.1.11
priority200
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
172.17.80.10
}
}
############################################
@front2上的keepalived.conf
! Configuration File for keepalivednotification_email{
}
smtp_server127.0.0.1
smtp_connect_timeout30
router_idLVS_DEVEL
}stateBACKUP
interfaceeth1
virtual_router_id51
mcast_src_ip192.168.1.12
priority100
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
172.17.80.10
}
}
############################################
#service keepalived start //启动keepalived,如果没有错误高可用到此配置完成1.安装WebServer之Nginx+PHP
配置服务器IP为:192.168.1.18
在nginx的基础上安装FastCGIのPHP使其支持动态PHP请求
说明:此处为yuminstallmysql-develmysql-serverphp-mysqlperl-DBD-MySQLlibxml2-devellibart_lgpllibart_lgpl-devel简单安装mysql
安装后的PHP为nginx提供php-fpm
①:libevent事件触发网络库
libevent-2.0.15-stable.tar.gz
#tarzxvflibevent-2.0.15-stable.tar.gz
#cdlibevent-2.0.15-stable
#./configure && make && make installlibconv-1.13.1.tar.gz
#tarzxvflibconv-1.13.1.tar.gz
#cdlibconv-1.13.1
#./configure&&make&&makeinstall
③:FastCGIのPHP
php-5.3.8.tar.gz
#tarzxvfphp-5.3.8.tar.gz
#cdphp-5.3.8
#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --enable-fpm --with-libevent-dir=/usr/local/lib --@你将会看到:ThankyouforusingPHP
@errorwhileloadingsharedlibraries:libiconv.so.2:cannotopensharedobjectfile:Nosuchfileordirectory
#ln-s/usr/local/lib/libiconv.so.2/usr/lib/libiconv.so.2//做个连接防止make时缺少libiconv.so.2
#ln-s/usr/local/lib/libiconv.so.2/usr/local/libiconv.so.2
#makeZEND_EXTRA_LIBS='-liconv'
@说明:make的时候加参数ZEND_EXTRA_LIBS='-liconv'是因为编译时需要iconv库, 但是configure的时候没有写到Makefile中, 还有一种方法是直接修改Makefile, 在链接库的地方(应该在100行左右)加@久等之后将会看到:Buildcomplete.Don'tforgettorun'maketest'.不等test了,直接makeinstall安装
#makeinstall
#cpphp-5.3.8/php.ini-production/usr/local/php/etc/php.ini
④接下来配置PHP及其他文件
#cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf
#vi/usr/local/php/etc/php-fpm.conf
@开启以下几项
pm.max_children=50
pm.start_servers=20
pm.min_spare_servers=5
pm.max_spare_servers=35
⑤在后台启动php-fpm,并ps查看进程运行情况
#/usr/local/php/sbin/php-fpm&
#ps-ef|grepphp-fpm
@将其添加到开机自动启动
#echo "/usr/local/php/sbin/php-fpm &" >> /etc/rc.d/rc.local#vi/etc/nginx/fastcgi.conf
@将里面内容替换为
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWAREnginx;
fastcgi_paramQUERY_STRING$query_string;
fastcgi_paramREQUEST_METHOD$request_method;
fastcgi_paramCONTENT_TYPE$content_type;
fastcgi_paramCONTENT_LENGTH$content_length;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
fastcgi_paramSCRIPT_NAME$fastcgi_script_name;
fastcgi_paramREQUEST_URI$request_uri;
fastcgi_paramDOCUMENT_URI$document_uri;
fastcgi_paramDOCUMENT_ROOT$document_root;
fastcgi_paramSERVER_PROTOCOL$server_protocol;
fastcgi_paramREMOTE_ADDR$remote_addr;
fastcgi_paramREMOTE_PORT$remote_port;
fastcgi_paramSERVER_ADDR$server_addr;
fastcgi_paramSERVER_PORT$server_port;
fastcgi_paramSERVER_NAME$server_name;
#PHPonly,requiredifPHPwasbuiltwith--enable-force-cgi-redirect
fastcgi_paramREDIRECT_STATUS200;
#vi/etc/ngnix/nginx.conf
#####
location~\.php${
root/www;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
includefastcgi_params;
}
#####
location/{
root/html;
indexindex.phpindex.htmlindex.htm;
}
##################################
#vi/usr/local/nginx/index.php
<?php
$link=mysql_connect("localhost","root","123456");
if(!$link)echo"FAILD!";
elseecho"OK!";
phpinfo();
?>
#servicenginxdrestart
@优化linux内核参数:
#vi/etc/sysctl.conf//增加一下内容,并是修改生效
net.ipv4.tcp_max_syn_backlog=65536
net.core.netdev_max_backlog=32768
net.core.somaxconn = 32768net.core.rmem_default=8388608
net.core.rmem_max=16777216
net.core.wmem_max = 16777216net.ipv4.tcp_synack_retries=2
net.ipv4.tcp_syn_retries = 22.安装WebServer之tomcat+JDK使其支持动态jsp程序和.do文件
配置服务器IP地址为:192.168.1.17/20
①:JDK的安装和部署
#chmod+xjdk-1_5_0_16-linux-i586.bin
#./jdk-1_5_0_16-linux-i586.bin
#mvjdk1.5.0_16/usr/local/jdk1.5.0_16
#vi.bash_profile//修改环境变量文件,增加一下内容
JAVA_HOME="/usr/local/jdk1.5.0_16"
CLASS_PATH="$JAVA_HOME/bin"
CATALINA_HOME="/usr/local/tomcat-6.0.18"
exportJAVA_HOMECATALINA_HOME
#source.bash_profile//使修改的环境变量生效
#tarzxvfapache-tomcat-6.0.18.tar.gz
#mvapache-tomcat-6.0.18/usr/local/tomcat-6.0.18
#cd/usr/local/tomcat-6.0.18/bin
#./startup.sh测试环境准备
1.将测试域名www.abc.com解析到前置机front1、front2的虚拟(virtual)IP上!
在XP客户端使用浏览器http://www.abc.com/index .*测试首页测试测试页面分别针对前置机nginx的动静分离和到tomcat上负载均衡
3.分别在对应的WebServer上制作测试页面,并用不同IE在客户端测试
①:index.html
Thisisahtmltestpage,theserveraddressis192.168.1.19
②:index.php
Thisisaphptestpage,theserveraddressis192.168.1.18
③:index.jsp和index.do只做测试页面,不做具体动态程序
Thisisajsptestpage,theserveraddressis192.168.1.17/20
This is a do test page, the server address is 192.168.1.17/20此时在客户机上pingwww.abc.com-t并将front1的eth1给ifdown掉!观察至丢失了一个数据包即恢复了域名到服务器的访问!
5.在linux 使用ab命令,对前置front1、front2做简单的压力测试!