{"id":4165,"date":"2014-06-17T09:48:11","date_gmt":"2014-06-17T01:48:11","guid":{"rendered":"https:\/\/www.icocean.com\/blog\/?p=4165"},"modified":"2014-06-17T10:22:01","modified_gmt":"2014-06-17T02:22:01","slug":"eginxphp-fpm%e4%b8%8eapache2php-fpm%e7%9a%84%e6%af%94%e8%be%83","status":"publish","type":"post","link":"https:\/\/www.icocean.com\/blog\/?p=4165","title":{"rendered":"nginx+php-fpm\u4e0eapache2+php-fpm\u7684\u6bd4\u8f83"},"content":{"rendered":"<p>Time is money. This somewhat shallow and overused saying fits perfectly well into any on-line business. Users are impatient and every millisecond brings them closer to leaving your website.<\/p>\n<p>If you\u2019ve ever been trying to squeeze more out of hardware you must have come across Nginx (engine x). Nginx usually appears in context of PHP-FPM (FastCGI Process Manager) and APC (Alternative PHP Cache). This setup is often pitched to be the ultimate combo for a web server but what that really means? How much faster a PHP application is going to be on a different web server? I had to check it and the answer as often is \u2013 that depends.<\/p>\n<p>I benchmarked three different types of PHP software:<\/p>\n<p>&#8211; Large application based on Zend Framework 1<br \/>\n&#8211; Small PHP script<br \/>\n&#8211; WordPress<\/p>\n<p>The software was hosted on Amazon EC2 large instance. All benchmarks were run from EC2 Tiny instance to be as close as it possible to the web server.<\/p>\n<p>To make sure I benchmark web servers instead of disk I\/O I set all logs to go to memory (\/dev\/shm). PHP sessions were directed to memcached.<br \/>\nBoth servers were using <a href=\"https:\/\/github.com\/zend-dev\/ZendOptimizerPlus\" target=\"_blank\">Zend Optimizer Plus<\/a> with opcache.revalidate_freq set to 1 hour. I use Zend Optimizer because APC wasn\u2019t stable for me with PHP 5.4.x.<br \/>\nIf you aren\u2019t familiar with PHP accelerators they convert PHP scripts into byte code and keep them in shared memory. That brings significant boost of performance (40-90%) because PHP scripts don\u2019t have to be read from a disc and parsed on every request. Using the accelerator helped me removing I\/O from the equation.<\/p>\n<p>It\u2019s very important to make sure Apache won\u2019t read .htaccess. You can achieve it by setting Allow Overwrite to none. Parsing htaccess in real time will drop performance on the Apache side. It\u2019s recommended to not use this file in production environment.<!--more--><\/p>\n<p>I kept dstats open during all test to make sure there is no IO and CPU cycles lost (after all EC2 is only a virtual server).<\/p>\n<p>First set of benchmarks was performed against the large ZF application. Each test consisted of 1000 requests and different level of concurrency (30\/60\/100). I tested 3 different setups: Apache2 with PHP5_MOD, Apache2 with PHP-FPM and NGINX with PHP-FRM.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.icocean.com\/blog\/wp-content\/uploads\/auto_save_image\/2014\/06\/0148126HR.png\" alt=\"\" \/><\/p>\n<p>Nginx is slightly ahead of Apache2 with FastCGI. The difference is usually 3.5%. The other interesting observation is that PHP-FPM performs only slightly better then PHP5_MOD.<\/p>\n<p>The second test was against a tiny PHP script. The script was printing \u201cLorem Ipsum\u201d 50 times. I benchmarked it with 1000 requests and concurrency set to 50.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.icocean.com\/blog\/wp-content\/uploads\/auto_save_image\/2014\/06\/014812yzu.png\" alt=\"\" \/><\/p>\n<p>As previously there is a slight difference (4%) between web servers.<\/p>\n<p>The last set of tests was done against WordPress. First I benchmarked the installation page. It was worth doing because it\u2019s almost a static page without much computing or DB interaction.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.icocean.com\/blog\/wp-content\/uploads\/auto_save_image\/2014\/06\/014813p78.png\" alt=\"\" \/><\/p>\n<p>This is very interesting, 32.5% difference in performance. Nginx had a chance to unfold wings because it loves static content.<\/p>\n<p>The last check was against WordPress homepage. There were 2 post and no caching.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.icocean.com\/blog\/wp-content\/uploads\/auto_save_image\/2014\/06\/014813We7.png\" alt=\"\" \/><\/p>\n<p>There isn\u2019t big difference between Apache2 and Nginx in PHP context. Yes, Nginx can be much faster when delivering static content but it won\u2019t speed up PHP execution. Running a PHP script seams to be so CPU challenging task that it completely eclipse any gain from a web server.<br \/>\nObviously a web application doesn\u2019t consist of PHP files only. There are plenty of static assets which have to be delivered. If you are not planning to use any content distribution network Nginx will definitely help with that.<\/p>\n<p>The conclusion is that it doesn\u2019t matter which server you are going to chose. The real performance wins are purely on the PHP side. Using an accelerator with caching can multiply the number of requests your infrastructure can maintain.<br \/>\nI guess the next big thing will be compiling PHP into binary and running it as FastCGI. Unfortunately it might take some time to have a stable all purpose <a href=\"http:\/\/en.wikipedia.org\/wiki\/HipHop_for_PHP\" target=\"_blank\">HipHop-PHP<\/a>. If you need more performance now, look into <a href=\"http:\/\/systemsarchitect.net\/boost-wordpress-performance-with-varnish-cache\/\" target=\"_blank\">Varnish Cache<\/a>.<\/p>\n<p>I\u2019m going to use Nginx because I\u2019m not tied to a particular web server. It\u2019s slightly faster for PHP (and apparently takes less memory but I\u2019m not able to prove it) and much faster for static resources. Why not to use it?<\/p>\n<p>Posted by Lukasz Kujawa\t on Mar 28, 2013<br \/>\n<Apache2 vs Nginx for PHP application> http:\/\/systemsarchitect.net\/apache2-vs-nginx-for-php-application\/\t <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time is money. This somewhat shallow and overused sayin <a href='https:\/\/www.icocean.com\/blog\/?p=4165' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3981],"tags":[638,3517,3975,1881],"class_list":["post-4165","post","type-post","status-publish","format-standard","hentry","category-nginx-","tag-apache","tag-nginx","tag-php-fpm","tag-1881","category-3981-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4165"}],"version-history":[{"count":9,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4165\/revisions"}],"predecessor-version":[{"id":4179,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4165\/revisions\/4179"}],"wp:attachment":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}