6月 172014
 

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.

If you’ve 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 – that depends.

I benchmarked three different types of PHP software:

– Large application based on Zend Framework 1
– Small PHP script
– WordPress

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.

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.
Both servers were using Zend Optimizer Plus with opcache.revalidate_freq set to 1 hour. I use Zend Optimizer because APC wasn’t stable for me with PHP 5.4.x.
If you aren’t 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’t have to be read from a disc and parsed on every request. Using the accelerator helped me removing I/O from the equation.

It’s very important to make sure Apache won’t 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’s recommended to not use this file in production environment.

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).

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.

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.

The second test was against a tiny PHP script. The script was printing “Lorem Ipsum” 50 times. I benchmarked it with 1000 requests and concurrency set to 50.

As previously there is a slight difference (4%) between web servers.

The last set of tests was done against WordPress. First I benchmarked the installation page. It was worth doing because it’s almost a static page without much computing or DB interaction.

This is very interesting, 32.5% difference in performance. Nginx had a chance to unfold wings because it loves static content.

The last check was against WordPress homepage. There were 2 post and no caching.

There isn’t big difference between Apache2 and Nginx in PHP context. Yes, Nginx can be much faster when delivering static content but it won’t 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.
Obviously a web application doesn’t 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.

The conclusion is that it doesn’t 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.
I 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 HipHop-PHP. If you need more performance now, look into Varnish Cache.

I’m going to use Nginx because I’m not tied to a particular web server. It’s slightly faster for PHP (and apparently takes less memory but I’m not able to prove it) and much faster for static resources. Why not to use it?

Posted by Lukasz Kujawa on Mar 28, 2013
http://systemsarchitect.net/apache2-vs-nginx-for-php-application/

 回复

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>