Nginx (”engine x”) 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor
Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多。
架构说明
前端一台nginx服务器做调度,后端两台PHP(fastcgi)做WEB服务器,这里静态页都由nginx来处理,PHP页面由后端PHP服务器处理
前端nginx服务器,同时也是NFS服务器,我们把后端的两台PHP服务器的web目录挂载NFS服务器,这样网站数据就同步了。
nginx IP 192.168.1.100 php1 IP 192.168.1.200 php2 IP 192.168.1.201
配置步骤:
第一部分 前端nginx调度服务器配置
1.安装Tcmalloc
64位操作系统请先安装libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转
开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。
# tar zxvf libunwind-0.99-alpha.tar.gz # cd libunwind-0.99-alpha/ # CFLAGS=-fPIC ./configure # make CFLAGS=-fPIC # make CFLAGS=-fPIC install
2、安装google-perftools:
# tar zxvf google-perftools-0.97.tar.gz # cd google-perftools-0.97/ # ./configure # make && make install # echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf # /sbin/ldconfig
3.安装 pcre
# tar -zxvf pcre-8.10.tar.gz # cd pcre-8.10 # ./configure # make && make install
4.安装nginx
# tar -zxvf nginx-0.8.53.tar.gz # cd nginx-0.8.53 # ./configure --prefix=/usr/local/nginx # make && make install
启动nginx
# /usr/local/nginx/sbin/nginx
停止nginx
# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
重启nginx
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
添加到自启动
# echo "/usr/local/nginx/sbin/nginx">>/etc/rc.local
5.修改nginx的配置文件
# vi /usr/local/nginx/conf/nginx.conf
user nobody; worker_processes 8; pid /usr/local/nginx/logs/nginx.pid; google_perftools_profiles /var/tmp/tcmalloc; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http{ include mime.types; default_type application/octet-stream; access_log off; error_log /dev/null; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 120; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; open_file_cache max=204800 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; server { server_name _; #default #return 500; rewrite ^/(.*)$ /50x.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } include vhost/*.conf; }
vhost/www.test.com.conf的内容
upstream lbcgi { ip_hash; server 192.168.1.200:9000; server 192.168.1.201:9000; } server { listen 80; server_name www.test.com; index index.html index.htm index.php; root /var/www/bbs/; location /nginx { stub_status on; auth_basic "NginxStatus"; access_log off; } location ~ .*\.(php|php5)?$ { fastcgi_pass lbcgi; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/bbs$fastcgi_script_name; include fastcgi_params; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } # log_format access '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" $http_x_forwarded_for'; # access_log /usr/local/nginx/logs/access.log access; }
6.查看是否好用,启动nginx
# lsof -n|grep tcmalloc
如果出现下面的就表示成功了
nginx 4322 www 10w REG 8,2 0 682436 /var/tmp/tcmalloc.4322 nginx 4323 www 12w REG 8,2 0 682438 /var/tmp/tcmalloc.4323 nginx 4324 www 14w REG 8,2 0 682439 /var/tmp/tcmalloc.4324 nginx 4325 www 16w REG 8,2 0 682440 /var/tmp/tcmalloc.4325 nginx 4326 www 18w REG 8,2 0 682441 /var/tmp/tcmalloc.4326 nginx 4327 www 20w REG 8,2 0 682442 /var/tmp/tcmalloc.4327 nginx 4328 www 22w REG 8,2 0 682443 /var/tmp/tcmalloc.4328 nginx 4329 www 24w REG 8,2 0 682444 /var/tmp/tcmalloc.4329
7.nginx加入到自启动
# vi /etc/rc.d/init.d/nginx
#!/bin/bash # Startup script for the nginx Web Server # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve # HTML files and CGI. # processname: nginx # pidfile: /usr/local/www/nginx/logs/nginx.pid # config: /usr/local/www/nginx/conf/nginx.conf PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH NGINX_HOME=/usr/local/www/nginx/sbin NGINX_CONF=/usr/local/www/nginx/conf if [ ! -f "$NGINX_HOME/nginx" ] then echo "nginxserver startup: cannot start" exit fi case "$1" in 'start') $NGINX_HOME/nginx -c $NGINX_CONF/nginx.conf echo "nginx start successful" ;; 'stop') killall -TERM nginx ;; esac
# chmod a+x /etc/rc.d/init.d/nginx # chkconfig --add nginx # chkconfig nginx on
8.安装NFS
# vi /etc/export
/var/www/bbs 192.168.1.200(rw,sync,no_root_squash),192.168.1.201(rw,sync,no_root_squash)
# service portmap restart # service nfs restart
# showmoun -e查看本机共享的目录
然后后端两台php服务器挂载这个目录
# mount 192.168.1.100:/var/www/bbs /var/www/bbs
=================================================================
第二部分 后端两台PHP(fastcgi)服务器的配置
需要的软件包:
eaccelerator-0.9.6.tar.bz2 gd-2.0.35.tar.gz mysql-5.1.52.tar.gz php-5.2.14.tar.bz2 php-fpm-0.6-5.2.11.tar.gz ZendOptimizer-3.3.0a-linux-glibc21-i386.tar.gz libevent-1.4.13-stable.tar.gz libiconv-1.13.1.tar.gz
安装之前要确保系统中安装了以下这些包。
我们可以用rpm -qa |grep 来查看系统是否装
bzip2-devel zlib-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel openssl-devel libxml2-devel gettext-devel
这些包一般都安了,除了最后一个,我们可以在光盘里找到用rpm -ivh 来安装上.
1、安装MYSQL
这里只需要安装mysql就可以了,不需要启动mysql
# tar -zxvf mysql-5.1.52.tar.gz # cd mysql-5.1.51 # groupadd mysql # useradd -g mysql -s /sbin/nologin -M mysql # ./configure --prefix=/usr/local/www/mysql --with-charset=gbk --with-extra-charsets=all --enable-thread-safe-client # make && make install
2、安装GD2
# tar -jxvf gd-2.0.35.tar.bz2 # cd gd-2.0.35 # ./configure --prefix=/usr/local/www/gd --with-png --with-freetype --with-jpeg --with-zlib --with-fontconfig # make # make install
3、安装PHP
先安装一下autoconf2.13,一定是2.13版本的,要不php执行buildconf的时候会出错
# tar -zxvf autoconf-2.13.tar.gz # cd autoconf-2.13 # ./configure --prefix=/usr/local/www/autoconf # make # make install # export PHP_AUTOCONF=/usr/local/www/autoconf/bin/autoconf # export PHP_AUTOHEADER=/usr/local/www/autoconf/bin/autoheader
# tar zxvf libevent-1.4.13-stable.tar.gz # cd libevent-1.4.13-stable/ # ./configure --prefix=/usr/local/www/libevent # make && make install
# tar zxvf libiconv-1.13.1.tar.gz # cd libiconv-1.13.1/ # ./configure --prefix=/usr/local/www/libiconv # make && make install
# tar zxvf php-5.2.14.tar.gz # tar zxvf php-fpm-0.6~5.2.11.tar.gz # php-fpm-0.6-5.2.11/generate-fpm-patch # cd php-5.2.14/ # patch -p1 < ../fpm.patch # ./buildconf --force # ./configure --prefix=/usr/local/www/php --with-config-file-path=/usr/local/www/php --with-mysql=/usr/local/www/mysql/ --with-config-file-path=/usr/local/php --with-gd=/usr/local/www/gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-ttf --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-xpm-dir --with-gettext --enable-fastcgi --with-fpm --with-zlib --enable-force-cgi-redirect --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-zip --enable-mbstring --enable-pcntl --enable-sockets --enable-soap --without-pear --with-libevent=/usr/local/www/libevent/ --with-iconv-dir=/usr/local/www/libiconv # make # make install # cp php.ini-dist /usr/local/www/php/php.ini
# vi /etc/php-fpm.conf
(1)<value name="listen_address">127.0.0.1:9000</value> 修改为<value name="listen_address">IP:9000</value> //这里写本机的ip (2)下面这两行去掉注释并修改 <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> <value name="display_errors">1</value> (3)<value name="user">nobody</value> //去注释 (4)<value name="group">nobody</value> //去注释 (5)<value name="allowed_clients">127.0.0.1</value> //允许连接的PC,写前端nginx服务器的ip 192.168.1.100 (6)<value name="max_children">128</value> (7)<value name="StartServers">20</value> (8)<value name="MinSpareServers">5</value> (9)<value name="MaxSpareServers">35</value> (10)<value name="rlimit_files">51200</value> (11)<value name="max_requests">102400</value>
启动php-fpm
# ulimit -SHn 51200 # /usr/local/www/php/bin/php-fpm start
注:/usr/local/www/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启
php-cgi,重新加载配置文件使用reload。
添加到自启动
# echo "ulimit -SHn 51200">>/etc/rc.local # echo "/usr/local/www/php/sbin/php-fpm start">>/etc/rc.local
=================================================
第三部分 环境优化
1、安装eaccelerator加速软件(后端两台PHP服务器上)
eaccelerator是php的加速软件,使用后php的执行效率会有很大幅度的提升。
# tar jxvf eaccelerator-0.9.5.3.tar.bz2 # cd eaccelerator-0.9.5.3 # /usr/local/www/php/bin/phpize # ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/www/php/bin/php-config # make # make install
编译安装后我们会看到屏幕提示的eaccelerator.so所在的目录,php5.2.x系列是在/usr/local/www/php/lib/php/extensions/no-
debug-non-zts-20060613,记住这个路径,待会要用到
修改php.ini
# vi /usr/local/www/php/php.ini
安装php扩展
在文件最后,[zend]之前,注意,这部分内容务必放在[zend]之前,不然可能会出现不可预期的服务器问题。添加下列信息:
[eaccelerator] zend_extension="/usr/local/www/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" eaccelerator.shm_size="32" eaccelerator.cache_dir="/tmp/eaccelerator" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="1800" eaccelerator.shm_prune_period="1800" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9"
重启php-cfi然后在IE还是那个php测试页,zend哪又有没有变化如没有,说明这种方法没有好用。
# mkdir /tmp/eaccelerator # chmod 777 /tmp/eaccelerator
在浏览器中打开phpinfo()那个测试页,如果出现以下内容,证明安装成功!
This program makes use of the Zend Scripting Language Engine: Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
2、安装Zend(后端两台PHP服务器上)
# tar zxvf ZendOptimizer-3.3.9-linux-glibc21-i386.tar.gz # cd ZendOptimizer-3.3.9-linux-glibc23-i386 # cp data/5_2_x_comp/ZendOptimizer.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
# vi /usr/local/php/php.ini
添加一行
zend_extension="/usr/local/www/php/lib/php/extensions/no-debug-non-zts-20060613/ZendOptimizer.so"
我们再输入IE看那个测试页Zend是否加载成功。
This program makes use of the Zend Scripting Language Engine: Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator with Zend Optimizer v3.3.9, Copyright (c) 1998-2009, by Zend Technologies
3、安装PDO_MYSQL(后端两台PHP服务器上)
# tar zxvf PDO_MYSQL-1.0.2.tgz # cd PDO_MYSQL-1.0.2/ # /usr/local/www/php/bin/phpize # ./configure --with-php-config=/usr/local/www/php/bin/php-config --with-pdo-mysql=/usr/local/www/mysql # make # make install
# vi /usr/local/php/php.ini
添加一行
zend_extension="/usr/local/www/php/lib/php/extensions/no-debug-non-zts-20060613/pdo_mysql.so"
4、优化Linux内核参数(nginx服务器和php服务器)
# vi /etc/sysctl.conf
在末尾增加以下内容:
net.ipv4.tcp_max_syn_backlog = 65536 net.core.netdev_max_backlog = 32768 net.core.somaxconn = 32768 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_fin_timeout = 1 net.ipv4.tcp_keepalive_time = 30 net.ipv4.ip_local_port_range = 1024 65535 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.ip_local_port_range = 5000 65000 net.ipv4.tcp_max_syn_backlog = 10240 net.ipv4.tcp_max_tw_buckets = 10240
使配置立即生效:
# /sbin/sysctl -p
# vi /etc/security/limits.conf
* soft nofile 51200 * hard nofile 102400
# ulimit -SHn 51200