APACHE+PHP vs NGINX+PHP 效能評測(譯文)
來源:互聯網
上載者:User
鑒於論壇裡有會員對NGINX、APACHE各自與各自的方式運行PHP的效能有疑問,我從國外轉載並翻譯一篇文章並在後面發表一點自己的看法,希望對大家有協助。
原文: http://blog.a2o.si/2009/06/24/apache-mod_php-compared-to-nginx-php-fpm/
所 有的軟體均從源碼編譯(詳見如下)。壓測工具使用APACHE的ApacheBench(ab),下面的結果是在同一台機器上測試的,所有伺服系統 (nginx、apache)均關閉了日誌功能(防止對效能的影響),測試過程中keepalive配置的是一次啟用、一次禁用,每樣測試均測試5遍,然 後取得的平均值。測試的檔案如下:
HelloWorld.php ? 簡短的輸出“Hello, World!” (13 bytes)的PHP指令碼,用來代表PHP檔案測試處理過程的開銷,相比的靜態檔案在下面
HelloWorld.txt ? 一個靜態檔案,輸出“Hello, World!” (同樣13 bytes),用來代表靜態檔案測試處理過程的開銷
100KB.txt ? 一個靜態100KB大小的檔案
1MB.txt ? 一個靜態1MB 大小的檔案
index.php ? 一個包含多重複雜的處理過程的PHP檔案,包括資料庫查詢、檔案快取讀取、模板[編譯]處理。
硬體環境
硬體: HP DL380 G5
硬體CPU: 2x Intel Xeon E5420 (4 cores each, total of 8 cores)
硬體記憶體: 8GB of ECC RAM
硬體磁碟: Smart Array P400i RAID-1 with 2x 147GB SAS drives
作業系統: Slackware 12.2 with almost all software compiled from source
檔案系統: ext3
Apache 版本: 2.2.11, php 由mod_php方式運行
Nginx 版本: 0.7.59, php 由 php-fpm (通過socket方式運行)
PHP 版本: 5.2.9
Eaccelerator 版本: 0.9.5.3 (for both, Apache and Nginx)
MySQL 版本: 5.0.77
OpenSSL 版本: 0.9.8k
服務日誌、請求日誌均已禁用
Apache編譯選項:
./configure ?prefix=/usr/local/$PDESTDIR_HTTPD ?sysconfdir=/etc/httpd \
?enable-authn-file ?enable-authn-default \
?enable-authz-host ?disable-authz-groupfile ?enable-authz-user ?enable-authz-default \
?enable-auth-basic \
?disable-include ?disable-filter ?disable-charset-lite \
?enable-log-config \
?enable-env ?enable-setenvif \
?enable-ssl ?with-ssl=/usr/local/openssl-$PVERSION_OPENSSL \
?enable-http ?enable-mime ?enable-status \
?disable-autoindex ?disable-asis \
?enable-info \
?enable-cgi ?disable-cgid \
?enable-vhost-alias \
?disable-negotiation \
?enable-dir \
?disable-actions \
?disable-userdir \
?enable-info \
?enable-rewrite \
?enable-so \
?with-mpm=prefork
Nginx編譯選項:
./configure ?prefix=/usr/local/$PDIR \
?conf-path=/etc/nginx/nginx.conf \
?error-log-path=/var/log/nginx/nginx_error.log \
?pid-path=/var/run/nginx.pid \
?lock-path=/var/run/nginx.lock \
?user=httpd \
?group=httpd \
?with-openssl=/usr/local/openssl-0.9.8k
PHP編譯選項(與APACHE一起啟動並執行MOD_PHP):
??[These lines are for PHP with Apache (mod_php)]?????-
./configure ?prefix=/usr/local/$PDESTDIR_HTTPD/$PDIR \
?with-apxs2=/usr/local/$PDESTDIR_HTTPD/bin/apxs ?enable-cli ?enable-cgi \
?with-config-file-path=/etc/php/httpd \
與NGINX一起啟動並執行(php-fpm)
??[These lines are for PHP with Nginx (php-fpm)]?????-
./configure ?prefix=/usr/local/php-fpm \
?enable-cli ?enable-fastcgi ?enable-fpm \
?with-fpm-conf=/etc/php/php-fpm/php-fpm.conf \
?with-fpm-log=/var/log/php-fpm.log \
?with-fpm-pid=/var/run/php-fpm.pid \
?with-config-file-path=/etc/php/php-fpm \
PHP共同配置
??[These lines are common for both]?????-
?disable-short-tags \
?disable-ipv6 \
?disable-all \
\
?enable-libxml \
?with-openssl=/usr/local/openssl-$PVERSION_OPENSSL \
?with-pcre-regex \
?with-zlib \
?with-bz2 \
?with-curl ?with-curlwrappers \
?enable-dba=shared ?with-db4 ?enable-inifile ?enable-flatfile \
?enable-dom ?with-libxml-dir \
?enable-filter \
?enable-ftp \
?with-gd ?with-jpeg-dir ?with-png-dir ?with-freetype-dir \
?with-gettext \
?enable-hash ?with-mcrypt \
?with-iconv=/usr/local/lib ?with-iconv-dir=/usr/local/lib \
?with-imap=/usr/local/imap-$PVERSION_CYRUSIMAP ?with-imap-ssl \
?enable-json \
?enable-mbstring ?enable-mbregex ?enable-mbregex-backtrack \
?with-mysql=/usr/local/mysql-$PVERSION_MYSQL ?with-mysqli=/usr/local/mysql-$PVERSION_MYSQL/bin/mysql_config \
?enable-pdo ?with-pdo-mysql=/usr/local/mysql-$PVERSION_MYSQL ?with-pdo-sqlite ?enable-sqlite-utf8 \
?enable-reflection \
?enable-session ?with-mm \
?enable-shmop \
?enable-simplexml \
?enable-soap \
?enable-sockets \
?enable-spl \
?with-regex \
?enable-sysvmsg ?enable-sysvsem ?enable-sysvshm \
?enable-tokenizer \
?enable-xml ?enable-xmlreader ?with-xmlrpc ?enable-xmlwriter ?with-xsl \
?enable-zip \
\
?with-pear \
?enable-zend-multibyte
配置文檔:
Apache mod_php: httpd.conf http://blog.a2o.si/wp-content/uploads/2009/06/httpd.conf.txt
Apache mod_php: php.ini http://blog.a2o.si/wp-content/uploads/2009/06/php.ini.txt
Nginx php-fpm: nginx.conf http://blog.a2o.si/wp-content/uploads/2009/06/nginx.conf.txt
Nginx php-fpm: php-fpm.conf http://blog.a2o.si/wp-content/uploads/2009/06/php-fpm.conf.txt
Nginx php-fpm: php.ini與mod_php方式相同
測試結果:
HelloWorld.php
在 這裡你可以看到每個PHP請求施加的開銷。有趣的的是一個事實是Apache在這個測試執行的更好而且好得多。這裡的原因是,Apache的PHP“內 置”通過mod_php,並且模組處理。另一方面Nginx的代理PHP請求到另一個應用程式伺服器(PHP-FPM)。 Nginx的效能在上面的圖是大約為apache的一半,容易解釋。這裡的PHP只是簡單輸出字串。
[font='Lucida Grande', Verdana, Arial, sans-serif]HelloWorld.txt
[font='Lucida Grande', Verdana, Arial, sans-serif]
[font='Lucida Grande', Verdana, Arial, sans-serif]
[font='Lucida Grande', Verdana, Arial, sans-serif]
在本次測試的Apache開始落後。 NGINX的效能超過了apache效能的兩倍。這項測試是展示靜態檔案服務的開銷。
[font='Lucida Grande', Verdana, Arial, sans-serif]100KB.txt
[font='Lucida Grande', Verdana, Arial, sans-serif]
[font='Lucida Grande', Verdana, Arial, sans-serif]
在 這裡的測試已經接近生產應用中的靜態檔案了(100KB),而且,我們能夠證明,Nginx 禁用keepalive後與Apache 啟用KeepAlive壓力發現NGINX仍優於APACHE約兩倍。此次測試的輸送量約為1.2GB/s,但是所有測試均未真正出網際網路(測試是在局域 網進行的)。
[font='Lucida Grande', Verdana, Arial, sans-serif]1MB.txt
這次測試沒所有伺服均未開啟keep alive,每個請求都要建立立串連,但相比於資料轉送,這建立串連的開銷就無所謂了。
真正意義上的PHP程式:
或許你會驚奇地看到,apache跑真實的、複雜點的PHP程式的時候,效能不及NGINX。
但原文作者或許沒有注意到,NGINX PHP這時執行的時候肯定有不少是彈了回錯誤了的(而且作者沒開啟日誌,測試過後沒查證)
作者在原文只是這樣說到:
apache開啟.htaccess(NGINX沒有這個功能)過後,會導致串連資料庫的效能有所下降。
後面還有兩項測試(分別是比較APACHE自己處理靜態與自己處理動態能力和NGINX的能力。)我就不在這裡翻譯了。
總結:
APACHE處理靜態能力不及NGINX,但NGINX處理PHP的穩定性不及apache mod_php。
如果取長補短哩?
NGINX做前端,APACHE做後端。NGINX proxy_pass apache,然後兩個伺服doc_root配成一致,並讓靜態檔案直接讓NGINX輸出,只轉寄PHP的請求給後端apache處理。
有會員的疑問:兩個伺服,根本是多此一舉。
回答:我的目標是高承載能力 穩定同時兼顧。而不是舍一取一。