Migrate from Apache to nginx + php-fpm and speed up your site twice

By | December 29, 2014

Advertisement from Google

This blog is hosted (together with mintchocolate.org) on a small VPS (1×2.8 Ghz, 256 Мб RAM, 10 Gb SSD). This VPS is shipped with apache2, php and mysql. As you can see I am using wordpress for this blog, also I have approximately 1000 unique visitors per day (half of them visit this and this articles). So it is good to have small response time.


First I need to measure how slow my blog is. I have used loadimpact.com to do that. This is result for apache2:

Apache2 stats

Apache2 blog stats

As you can see load time is ~2 seconds and when I have 50 active clients load time is almost 7! seconds!

Next, I have tried an empty static page. This is apache2 with empty static page:

Apache empty page stats

Apache empty page stats

Better! So, now we know ideal response time for apache. No php, no images, only static html (hello world) – ~700 ms

After this, I’ve decided to move all my sites on this VPS from Apache2 to nginx + php-fpm. 2 seconds is too much! 1 second is the upper limit for load time!


First of all we need to install nginx with php-fpm:

echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/nginx-stable.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
sudo apt-get update
sudo apt-get install nginx php5-fpm

Advertisement from Google

Then change couple settings in /etc/php5/fpm/pool.d/www.conf

listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

And then in /etc/php5/fpm/php.ini


Configure your site:

server {
        server_name druss.info;
        root /var/druss.pp.ua/www;
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php5-fpm.sock;

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;

And now, results for this configuration:

nginx blog stats

nginx blog stats

As you can see it is twice faster that with apache! And now we can handle 50 active connections without any problem. Incredible!


apt-get remove apache2

Leave a Reply

Your email address will not be published. Required fields are marked *