Apache2 vs Nginx for PHP application

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.

Apache Benchmark

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.

Apache Benchmark

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.

Apache Benchmark

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.

Apache Benchmark

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?

You might like to read

Nginx HTTP Server

6 thoughts on “Apache2 vs Nginx for PHP application

  1. ” (and apparently takes less memory but I’m not able to prove it) ”
    For me this is the main thing, the performance seems identical in PHP but you can fit more requests before server begin to use virtual memory with Nginx.


    1. I agree, it’s important. I usually need memory on Web Servers for other services. I’m not sure what is the best way to measure real memory usage by an application. This is the reason why I can’t prove it.


  2. You were running your tests from the same server – right?
    Try to make it slightly closer to a real life conditions – test from some remote system. The difference will grow.

    The difference between 2 web-servers is no that silly. Apache remains online all the time while PHP runs and content being sent to client. While nginx releases PHP as soon as it’s finished. Even making nginx a frontend in front of Apache can relieve the latter from great pains – working the same way, making Apache return resources way faster.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s