Category: PHP

Is there a speed gain when moving from Apache Mod PHP to Nginx PHP-FPM?

I had a chance to deploy one of my running websites on another virtual machine.
I wanted to improve performance as customers are paying for the product and wanted to give a faster experience.

On the old site I used Apache with PHP mod apache to run the site. On the new site I went with Nginx and PHP-FPM.

The Server Setups

Both websites use the Yii Framework on PHP with a MySQL database. There has been some performance tweaks on the Old Site. The new site I left everything standard.

Old Site:

  • 2GB RAM (free 222MB)
  • CPU(s): 2
  • Site shared - vhosts with a few other sites
  • HTTPS Enabled (letencrypt)
    * PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
  • Server hosted in Nederlands (Testing from South Africa)

New Site:

  • 2GB RAM (Free 1161MB)
  • CPU(s): 2
  • Site dedicated, not other site on server
  • No HTTPS
  • PRETTY_NAME="Ubuntu 18.04.4 LTS"
  • Server hosted in South Africa (Testing from South Africa)


The method for the performance test is as follows.

  1. Enable response time logging in the access logs of both apache and nginx - I wrote a post on this with apache and there are docs online for nginx
  2. Browsing Test - I will browse as a non logged in and logged in user on both sites in isolation. The statistics of response times will be recorded from the user's perspective in the browser and from the log response times.
  3. WebPage Test - I will use Web Page Test to compare both sites for a few pages.
  4. Load Test - I will test concurrent load with locustio
  5. Test - Test using open source sitespeed testing utility

This will not be a scientific comparison - purely anecdotal

Browsing Test

PageNginx + PHP-FPM (ms)Apache + ModPHP (ms)Difference
Home Page1380166020%
Contact Us1060131024%
About Us997128028%
Login (POST)14107550435%
Portfolio (Db intentensive)19206960263%
Chart (TTFB)105348231%

From the chart above it is safe to say that without a shadow of a doubt, the new site is faster.

Naturally the server being much closer helps. Instead of 9354km the new server is about 50km away. The average latency on a ping is 187 ms to the old server and about 12ms to the new one.

WebPage Test

I tested both sites from south africa, here are the screenshots and relevant info below:

Speed Test of the New Nginx PHP-FPM website
Speed Test of the Old Apache ModPHP website
WebPageTest MetricNginx + PHP-FPMApache + ModPHP
First Byte102895
Speed Index7691660
Document Complete Time38503353
Document Complete Requests3633
Fully Loaded Time47464087
Fully Loaded Requests4846

Surprisingly the new website performed worse (in total). It was faster to first byte but full load was worse. Furthermore no caching and webpagetest does not like that.

WebPageTest MetricNginx + PHP-FPMApache + ModPHP
First Byte126913
Speed Index8001681
Document Complete Time68252989
Document Complete Requests1816
Fully Loaded Time68693215
Fully Loaded Requests1917

The results of this were also pretty annoying. It seems that webpagetest wants me to cache static content, gzip assets and use a CDN. Then it will be happy.

Let me add gzip and static caching to nginx and see.
Just uncomment the gzip section in the default nginx.conf.

After adding updaing it is looking a bit better:

After updating the new site enabling gzip compression and browser caching

I then removed the twitter feed and things were better:

Old Site:

New Site:


Load Test

I created a test to make some GET requests against the server - while not logged in. The test has users spawn at 1 a second.

The new site performed as follows

N umber of users nginx php-fpm
Response times (ms) php-fpm nginx
Total requests per second nginx php-fpm

So it can run stably from 80 to 100 RPS.

The old site performed terribly. When I got up to 2 RPS all the other sites monitoring was saying it was down. It was weird that the RPS didn't grow according to users as fast with the old site - perhaps locust knows it couldn't handle that spawn rate.




To do a more comprehensive test I employed I then ran the test against both sites are here are the results...

The Old Mod-PHP and Apache site


The New PHP-FPM and Nginx site


Update Adding HTTP/2

I was using the default nginx config that is HTTP/1.1, so I updated it to serve with HTTP/2

Now, I have switched over the performance site to be the current site.


  • First bytes is a bit slower 0.192s vs 0.095s on the HTTP/1.1 version
  • Start render is also about 100ms faster at 0.400s vs 0.500s on the HTTP/2 site
  • Document complete and fully loaded is however much faster on HTTP/2 - even with the 100ms handicap. It is about 300ms faster - probably due to HTTP/2 multiplexing of asset acquisition.


Some tests were conclustive - others were still in the balance.
From a load testing and user initial response view - the new site clearly wins. The biggest gain comes more from concurrent users and handling load. Another significant bit was moving the server closer to the users.

The PHP-FPM with Nginx site can handle 40 or more times more load than the other site and has a faster response even with the 200ms handicap.

Next Steps

The next steps to take would be to look at how to maximise performance with nginx and php-fpm

How to ace a PHP job test in South Africa

Initial Preparation

First of all do a project in PHP that utilises the database. I am talking about pure PHP and not a framework. Remember to turn make sure you set the following configuration settings.

display_errors = 1;

You can always view the php documentation which is always of great help.

 Get ready for some specialised questions

The off-the-wall questions

  • What do you think of garden gnomes? Your answer should include some form of analysis and deduction.
  • Why are man-hole covers a circle? The answer is because it is not possible for the cover to fall into the hole that way.

PHP Specific Questions

Increment and Decrement Operators

Example Name Effect
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

Question 1: ++$a * $a++ + $a = ? (Where $a = 5)

Answer 1: 6 * 5 + 5 = 35

Bitwise Operators

Example Name Result
$a & $b And Bits that are set in both $a and $b are set.
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set.
$a ^ $b Xor (exclusive or) Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a << $b Shift left Shift the bits of $a $b steps to the left (each step means "multiply by two")
$a >> $b Shift right Shift the bits of $a $b steps to the right (each step means "divide by two")

Question 2: echo 5 & 7;

Answer 2:

5 = 0101
7 = 0111

Now AND the bits

Answer = 0101 (5)

Question 3: echo 12 ^ 9;

Answer 3:

12 = 1100
9  = 1001
Now XOR the bits
Answer = 0101 (5)

Question 4:$x = 5; echo $x >> 2;
Answer 4:

5 = 0101
Shift bits 2 steps to the right

Answer = 0001 (1)

Learn some functions of the PHP library

array_shift  - Shifts an element off the beginning of an array

array_combine - Creates an associative array by using 1 for keys and 1 for values

final keyword - Prevents overriding and extension of the class

interfaces - Interfaces specify which methods a class must implement, but does not specify the code for the methods.

suppression of errors

Learn some permutations, combinations and factorial

I'm not good at these and if anyone has a tutorial or content to add please free to comment.

Here are some resources that may help, if you find out how to do these please comment below.

Circular Permutations

Example Permutation and Combination Question

Maths is Fun

If ABCDEFGH are sitting at a table, where the configuration of the seats is 1 at each head of the table, and 3 on either side. If A can't sit next to B and F can't sit G, how many different configurations are there?

How many different numberplates can be made, if the numberplate must contain only digits and no signle digit can be repeated if there are 5 spaces for digits on the numberplate?

Please leave comments if you have anything else to add.

PHP job test

PHP Developer Checklist: Application Level Package Managers

PHP Application Level Package Manager: Composer

What is Composer?

Ever used apt, dpkg, yum?  These are package managers for unix systems, they automatically update your server with latest updates and install dependant libraries. Composer does the same thing just for PHP. It allows you to declare the dependant libraries for your project in a:
composer.json file and by running composer install composer will handle installing the dependant libraries needed for the project. I don't know where it stores these files though, in the specfic project folder or in a global php include path...still need to find that out.

Find out more at:

Alternative/ in addition to: PEAR

The PHP Extension and Application Repository

Perl Application Level Package Manager: CPAN

What is CPAN?

It is a package manager for perl.