当前位置:Gxlcms > PHP教程 > APACHE+PHPvsNGINX+PHP性能评测(译文)

APACHE+PHPvsNGINX+PHP性能评测(译文)

时间:2021-07-01 10:21:17 帮助过:8人阅读

鉴于论坛里有会员对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处理。


有会员的疑问:两个伺服,根本是多此一举。
回答:我的目标是高承载能力 稳定同时兼顾。而不是舍一取一。

人气教程排行