apache+php vs nginx+php performance evaluation (translated)

Source: Internet
Author: User
Tags apache php imap openssl version keep alive
In view of the forum members of Nginx, Apache and their respective ways to run PHP performance questions, I reproduced from abroad and translated an article and post a point of their own views, I hope to be helpful.
Original: http://blog.a2o.si/2009/06/24/apache-mod_php-compared-to-nginx-php-fpm/

All the software is compiled from the source code (see below). Test tool using Apache Apachebench (AB), the following results are tested on the same machine, all servo systems (Nginx, Apache) are turned off the logging function (to prevent the impact on performance), the test process keepalive is configured to be enabled, Once disabled, each test is tested 5 times, and then the average is obtained. The test files are as follows:

Helloworld.php? Short output "Hello, world!" PHP script (bytes) that represents the cost of the PHP file test process, compared to the static file below
HelloWorld.txt? A static file, output "Hello, world!" (same as bytes), used to represent the overhead of a static file testing process
100KB.txt? A static 100KB size file
1MB.txt? A static 1MB size file
Index.php? A PHP file that contains multiple complex processes, including database queries, file cache reads, and template [compile] processing.

Hardware environment
Hardware: HP DL380 G5
Hardware cpu:2x Intel Xeon E5420 (4 cores each, total of 8 cores)
Hardware memory: 8GB of ECC RAM
Hardware disk: Smart Array p400i RAID-1 with 2x 147GB SAS drives
Operating system: Slackware 12.2 With almost all software compiled from source
File system: Ext3
Apache version: 2.2.11, PHP run by mod_php mode
Nginx version: 0.7.59, PHP by PHP-FPM (run by socket)
PHP version: 5.2.9
Eaccelerator version: 0.9.5.3 (for both, Apache and Nginx)
MySQL version: 5.0.77
OpenSSL version: 0.9.8k
Service logs, request logs are disabled
Apache Compilation options:

./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 Compilation options:

./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 compilation options (mod_php running with Apache):

?? [These lines is 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 \

Run with Nginx (PHP-FPM)
?? [These lines is 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 Common Configuration
?? [These lines is 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


Configuration document:



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 the same way as mod_php


Test results:
helloworld.php




Here you can see the overhead that each PHP request imposes. Interestingly, one fact is that Apache performs better in this test and is much nicer. The reason here is that Apache PHP is "built-in" via mod_php, and the module is processed. On the other hand Nginx proxy PHP requests to another application server (PHP-FPM). The performance of Nginx in the above figure is about half of Apache, easy to explain. PHP Here is just a simple output string.


[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]

In this test the Apache began to lag behind. Nginx performance exceeds twice times the performance of Apache. This test is a demonstration of the overhead of a static file service.


[font= ' Lucida Grande ', Verdana, Arial, Sans-serif]100kb.txt
[font= ' Lucida Grande ', Verdana, Arial, Sans-serif]

[font= ' Lucida Grande ', Verdana, Arial, Sans-serif]

Here the test has been close to the static files in the production application (100KB), and we can prove that Nginx disabled keepalive after the keepalive pressure found with Apache is still better than Apache about twice times. The throughput of this test was about 1.2gb/s, but none of the tests were actually out of the internet (the test was done on a local area network).


[font= ' Lucida Grande ', Verdana, Arial, Sans-serif]1mb.txt


This test does not have all servos open keep alive, each request to establish a new connection, but compared to the data transfer, the cost of the new connection does not matter.




PHP program in real Sense:

You may be surprised to see that Apache runs a real, complex PHP program with less performance than Nginx.
But the original author may not notice, NGINX PHP at this time there must be a lot of play back wrong (and the author did not open the log, after the test did not verify)
In the original, the author only says:
Apache Open. htaccess (Nginx does not have this feature) can result in degraded performance of the connected database.


There are two more tests behind it (comparing Apache's ability to handle static and self-handling dynamics and Nginx, respectively). I'm not here to translate.




Summarize:
Apache is less capable of handling static than Nginx, but Nginx is less stable than Apache mod_php.


What if it's a complement?
Nginx do the front end, Apache do backend. Nginx Proxy_pass Apache, then two servo doc_root are paired together, and let the static file directly let Nginx output, only forward PHP request to the backend Apache processing.


There are members of the question: Two servo, is simply superfluous.
Answer: My goal is to have a stable and balanced high carrying capacity. Instead of taking one.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.