I have a quick comparison of how this blog performed with Apache vs how it performs now with NGINX and a cache plugin. So if you have a WordPress blog and wonder should I use NGINX instead of Apache? or is it worth configuring a cache? Maybe this helps you decide.
First of all, the hardware:
- DigitalOcean droplet 5$ month 💓
- 1 vCPU core
- 1GB of RAM
- 25GB SSD
It is a very small virtual server but performs decently considering the traffic I have (pretty much none haha).
I am going to compare 4 scenarios:
- Apache 2.2.22, Ubuntu 14 and PHP 5.3.10 (my old setup)
- Apache 2.4, Ubuntu 18 and PHP 7
- NGINX 1.14.0, Ubuntu 18 and PHP 7
- NGINX 1.14.0, Ubuntu 18 and PHP 7 with Simple Cache
Benchmark results
Let’s throw some requests at the home page of this blog and see what happens. I will use 50 concurrent threads to perform 200 requests, that should be enough for this small server:
ab -n 200 -c 50 https://salvatorelab.com/
The old setup is clearly much slower, choking at 43 seconds per request!
Newer versions of both Apache and NGINX seem to perform reasonably well although I have to point out that I had to change the configuration for Apache mpm_prefork module, reducing the max number of threads to fit in memory under heavy load.
The results also show that adding a cache improves performance drastically. Less than 0.4 seconds per request, compared to 6 or 8 for NGINX and Apache. Serving static files is much much faster than executing PHP and connecting to the database.
Simple Cache configuration
Most cache plugins assume Apache as the web server so I ended up using a very simple plugin that generates static files (html) for non logged users and saves them to disk: Simple Cache.
Then I edited my NGINX virtual server configuration and changed:
try_files $uri $uri/ /index.php$is_args$args;
To:
try_files "/wp-content/cache/simple-cache/${http_host}${request_uri}index.html" $uri $uri/ /index.php$is_args$args;
So NGINX directly serves those static files, if they exist, and otherwise executes the PHP script. This step is not mandatory cause PHP will serve that static file if present but I want to avoid NGINX executing any PHP at all if possible to increase performance even more.
Conclusion
I am surprised to see Apache performing that close to NGINX cause traditionally it’s been quite slow in comparison.
Still NGINX performs better in both low and high traffic scenarios on a small server like mine. It processes requests faster and requires less memory too.
But if you are worried about performance, forget about the server and focus on using a cache plugin. I mean, you can switch to NGINX to gain some performance but it will be a minor improvement compared to a cache.
Nice article! thanks for the work done and for sharing the results.
Today your traffic wasn’t none, you got at least one visit 😀
Haha thank you Carlos!