nginx 0.8.x + PHP 5.2.13 勝過apache十倍的web服務器


  Nginx ("engine x") 是一個高性能的 HTTP 和反向代理服務器,也是一個 IMAP/POP3/SMTP 代理服務器。 Nginx 是由 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,它已經在該站點運行超過兩年半了。Igor 將源代碼以類BSD許可證的形式發佈。

  Nginx 的中文維基:http://wiki.codemongers.com/NginxChs

  在高併發連接的情況下,Nginx是Apache服務器不錯的替代品。Nginx同時也可以作爲7層負載均衡服務器來使用。根據我的測試結果,Nginx 0.5.31 + PHP 5.2.4 (FastCGI) 可以承受3萬以上的併發連接數,相當於同等環境下Apache的10倍

  以下是 Nginx 0.5.31 + PHP 5.2.4 (FastCGI) 服務器在3萬併發連接下的TCP狀況:

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'


  各種狀態TCP連接數如下(各項值所代表的含義見我的另一篇文章《查看Apache併發請求數及其TCP連接狀態》):
 

引用
LAST_ACK 1
SYN_RECV 991
CLOSE_WAIT 1
ESTABLISHED 18789
FIN_WAIT1 1478
FIN_WAIT2 181
TIME_WAIT 506


  根據我的經驗,4GB內存的服務器+Apache(prefork模式)一般只能處理3000個併發連接,因爲它們將佔用3GB以上的內存,還得爲系統預留1GB的內存。我曾經就有兩臺Apache服務器,因爲在配置文件中設置的MaxClients爲4000,當Apache併發連接數達到3800時,導致服務器內存和Swap空間用滿而崩潰。

  而這臺 Nginx 0.5.31 + PHP 5.2.4 (FastCGI) 服務器在3萬併發連接下,開啓的10個Nginx進程消耗100M內存(20MB*10=100M),開啓的250個php-cgi進程消耗1G內存(4MB*250≈1GB),加上系統自身消耗的內存,總共才消耗2GB內存。如果服務器內存較小,完全可以只開啓25個php-cgi進程,這樣php-cgi消耗的總內存數才100M。在開啓25個php-cgi進程的情況下,每分鐘的處理能力只比開啓250個php-cgi進程時低了不到一半。

  以下爲 Nginx 0.5.31 + PHP 5.2.4 (FastCGI) 服務器在3萬併發連接下,開啓的10個Nginx進程和250個php-cgi進程時的系統負載情況:
  點擊在新窗口中瀏覽此圖片
 



  安裝步驟:
  (系統要求:Linux 2.6+ 內核,本文中的Linux操作系統爲CentOS 4.4)

  一、獲取相關開源程序:
  1、下載程序源碼包到當前目錄:
  本文中提到的所有開源軟件爲截止到2007年9月21日的最新穩定版。我將它們打了兩個壓縮包。

  第一個壓縮包:nginx_php_mysql_1.0_1of2.zip:
  下載地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2289607

  第二個壓縮包:nginx_php_mysql_1.0_2of2.zip:
  下載地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2289595

  2、解壓縮:
 

unzip nginx_php_mysql_1.0_1of2.zip
unzip nginx_php_mysql_1.0_2of2.zip


 



  二、安裝PHP 5.2.4(FastCGI模式)
  1、編譯安裝PHP 5.2.4所需的支持庫:
 

tar zxvf libiconv-1.11.tar.gz
cd libiconv-1.11/
./configure --prefix=/usr/local/webserver/lib/libiconv
make && make install
cd ../

tar zxvf freetype-2.3.5.tar.gz
cd freetype-2.3.5/
./configure --prefix=/usr/local/webserver/lib/freetype
make && make install
cd ../

tar zxvf libpng-1.2.20.tar.gz
cd libpng-1.2.20/
./configure
make && make install
cd ../

tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure --enable-static --enable-shared
make && make install
cd ../

tar zxvf gd-2.0.35.tar.gz
cd gd-2.0.35/
./configure --prefix=/usr/local/webserver/lib/gd --with-freetype=/usr/local/webserver/lib/freetype --with-jpeg --with-png
make
make install
cd ../

tar zxvf libxml2-sources-2.6.30.tar.gz
cd libxml2-2.6.30/
./configure --prefix=/usr/local/webserver/lib/libxml
make && make install
cd ../


 



  2、編譯安裝MySQL 5.0.45
 

/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
tar zxvf mysql-5.0.45.tar.gz
cd mysql-5.0.45
./configure --prefix=/usr/local/webserver/mysql/ --without-debug --with-unix-socket-path=/tmp/mysql.sock --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client
make && make install
chmod +w /usr/local/webserver/mysql
chown -R mysql:mysql /usr/local/webserver/mysql
cp support-files/my-medium.cnf /usr/local/webserver/mysql/my.cnf


 



  附:以下爲附加步驟,如果你想在這臺服務器上運行MySQL數據庫,則執行以下兩步。如果你只是希望讓PHP支持MySQL擴展庫,能夠連接其他服務器上的MySQL數據庫,那麼,以下兩步無需執行。
  ①、以mysql用戶帳號的身份建立數據表:
 

/usr/local/webserver/mysql/bin/mysql_install_db --defaults-file=/usr/local/webserver/mysql/my.cnf --basedir=/usr/local/webserver/mysql --datadir=/usr/local/webserver/mysql/data --user=mysql --pid-file=/usr/local/webserver/mysql/mysql.pid --skip-locking --port=3306 --socket=/tmp/mysql.sock



  ②、啓動MySQL(最後的&表示在後臺運行)
 

/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/usr/local/webserver/mysql/my.cnf &


 



  3、編譯安裝PHP(FastCGI模式)
 

cd ..
tar zxvf php-5.2.4.tar.gz
cd php-5.2.4/
./configure --prefix=/usr/local/webserver/php --with-mysql=/usr/local/webserver/mysql --with-config-file-path=/usr/local/webserver/php/etc --with-gd=/usr/local/webserver/lib/gd --enable-gd-native-ttf --enable-gd-jis-conv --with-iconv-dir=/usr/local/webserver/lib/libiconv --with-freetype-dir=/usr/local/webserver/lib/freetype --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-zlib --with-libxml-dir=/usr/local/webserver/lib/libxml --enable-xml  --disable-debug --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect
make && make install
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
cd ../


 



  附:編譯PHP之後,爲PHP添加擴展的方法。(本步驟可選)
 

cd php-5.2.4/pcntl
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install
cd ../../../



 

vi /usr/local/webserver/php/etc/php.ini


修改extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
 



  4、創建www用戶和組,以及其使用的目錄:
 

/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /data0/vshare/htdocs
chmod +w /data0/vshare/htdocs
chown -R www:www /data0/vshare/htdocs



  5、安裝lighttpd中附帶的spawn-fcgi,用來啓動php-cgi
  注:壓縮包中的spawn-fcgi程序爲已經編譯成二進制的版本。
 

cp spawn-fcgi /usr/local/webserver/php/bin
chmod +x /usr/local/webserver/php/bin/spawn-fcgi



  6、啓動php-cgi進程,監聽127.0.0.1的10080端口,進程數爲250(如果服務器內存小於3GB,可以只開啓25個進程),用戶爲www:
 

/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 250 -u www -f /usr/local/webserver/php/bin/php-cgi


 



  三、安裝Nginx 0.5.31
  1、安裝Nginx所需的pcre庫:
 

tar zxvf pcre-7.2.tar.gz
cd pcre-7.2/
./configure
make && make install
cd ../



  2、安裝Nginx
 

tar zxvf nginx-0.5.31.tar.gz
cd nginx-0.5.31/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx
make && make install
cd ../



  3、創建Nginx日誌目錄
 

mkdir -p /data1/logs
chmod +w /data1/logs
chown -R www:www /data1/logs



  4、創建Nginx配置文件
  ①、在/usr/local/webserver/nginx/conf/目錄中創建nginx.conf文件:
 

rm -f /usr/local/webserver/nginx/conf/nginx.conf
vi /usr/local/webserver/nginx/conf/nginx.conf


  輸入以下內容:
 

引用
user  www www;

worker_processes 10;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process. 
worker_rlimit_nofile 51200;

events 
{
        use epoll;

        #maxclient = worker_processes * worker_connections / cpu_number
        worker_connections 51200;
}

http 
{
        include       conf/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  /data1/logs/access.log  main;

        #sendfile on;
        #tcp_nopush     on;

        keepalive_timeout 60;

        #gzip  on;

        server
        {
                listen       80;
                server_name  localhost;
                index index.html index.htm index.php;
                root  /data0/vshare/htdocs;

                if (-f $request_filename/index.html)
                {
                        rewrite (.*) $1/index.html break;
                }
                if (-f $request_filename/index.htm)
                {
                        rewrite (.*) $1/index.htm break;
                }
                if (-f $request_filename/index.php)
                {
                        rewrite (.*) $1/index.php break;
                }

                location ~ .*\.php?$
                {
                        include conf/fcgi.conf;      
                        fastcgi_pass  127.0.0.1:10080;
                        fastcgi_index index.php;
                }
        }
}



  ②、在/usr/local/webserver/nginx/conf/目錄中創建fcgi.conf文件:
 

vi /usr/local/webserver/nginx/conf/fcgi.conf


  輸入以下內容:
 

引用
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;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param  REDIRECT_STATUS    200;



  5、啓動Nginx
 

ulimit -SHn 51200
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf


 



  四、配置開機自動啓動Nginx + PHP
 

vi /etc/rc.local


  在末尾增加以下內容:
 

引用
/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 250 -u www -f /usr/local/webserver/php/bin/php-cgi
ulimit -SHn 51200
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf


 



  五、優化Linux內核參數
 

vi /etc/sysctl.conf


  在末尾增加以下內容:
 

引用
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
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



  使配置立即生效:
 

/sbin/sysctl -p



  (全文完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章