您好,欢迎访问一九零五行业门户网

APACHE+PHP vs NGINX+PHP 性能评测(译文)

鉴于论坛里有会员对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处理。
有会员的疑问:两个伺服,根本是多此一举。 
回答:我的目标是高承载能力 稳定同时兼顾。而不是舍一取一。
其它类似信息

推荐信息