Make Your WordPress At Least 3x Faster By Migrating To Nginx + PHP-FPM

Published on Author gryzliLeave a comment

If you want to make your WordPress at least 3x faster , then you should definitely read below….

Yesterday I woke up, and my first thought was – I need to make my WordPress blog faster !

 

Caring about performance !

That’s a great idea and most of the time, there is bunch of things you can do about this, all you need is a little bit enthusiasm, not so geeky knowledge.

 

My current running environment

It’s worth being said, what was my “starting point” before trying to do whatever optimizations I fell necessary, and here it was:

  • Centos 6.7
  • Php 5.3.x by the Centos repo
  • Apache 2.x by the Centos repo
  • MySQL 5.1.x

 

My past curve of WordPress speed optimizations

1. First I installed WordPress by the laziest way human beings know and using the laziest (still slowest) technologies – Apache + mod_php + php 5.3.x

2. Then I saw my WP site was tremendously slow and I tought to myself, lets get it little bit faster, what I’ve done was easy and simple:

  • I switched my PHP setup to mod_fcgid (instead the slow mod_php)
  • I installed WordPress caching module (WP Super Cache), which caused some serious impact on my load times

 

3. At a certain point in time, I again felt like being no enough fast

  • I setup static content compression by using Apache mod_deflate + mod_rewrite rules

     
  • I installed PHP caching module (APC), which had its good for me
    The APC configuration was as follows

     
  • Tuned my mysql configuration a little bit

At that point I think, I already doubled my performance grade, and my blog was loading 2 to 3 times faster.

Then I wanted some more speed on my WordPress, it still wasn’t fast enough

That was the time i woke up with the idea of switching to Nginx. I’ve already migtrated hundred of sites to Nginx+PHP-FPm and I already understand the huge benefits you get by using php-fpm.

 

Why use Nginx ?

Well some people tend that Nginx serves static content faster than Apache. I’m not quite sure, how much is the truth inside this, but for sure there is something real about it.

Despite the static content serving rumors, that wasn’t the reason I needed it, the real reason was:

Nginx has native support for executing php scripts under PHP-FPM and that’s great !

The configuration is so much straight forward, that I was tempted to switch to it.

 

How I Installed and Configured my Nginx ?

First I thought about installing Nginx from my Centos Repo, but that wasn’t the greatest idea, because it was too old. When it comes to software, most of the times the newer is the better (or at least we all hope for this).

So I decided to install the latest Nginx version, which also happened to be a damn easy task.

Just install Nginx repo, based on your Linux distro

Current Nginx Repo Lists

 

After successfully installing Nginx 1.8.x it came the time for configuring it.

 

Here are my current configs:

/etc/nginx/nginx.conf

You may notice the following directives:

  • user apache; – I wanted to reuse my Apache permissions
  • worker_processes 4; – This is the same as the number of virtual CPU’s I have

 

/etc/nginx/conf.d/gryzli.info.conf

 

And the wordpress related configuration options:

/etc/nginx/custom_conf/wordpress.conf

 

 

Installing and configuring PHP 5.6.x as FPM

As with Nginx my thought about PHP was the same – the newer the better.  And again, there are rumors about php 5.6 being considerably faster than its predecessors. Still no tests by myself to acknowledge this, but lets trust the internet guys…

 

In the matter of fact, running php as FPM is the thing which makes the most of the speed increase magic. Why ? It’s simple, when you run PHP-FPM, you have the following BIG PLUSES:

  • PHP FPM threads are sharing their memory, which gives them the chance to use the same cache container (when you use opcode caching). Also after the child processes respawn, the cache base stays tight with the parent process, so the cache is not flushed.
  • If you use static created childs, you will benefit one more thing, skip the “event driven fork” part, which slows your request processing

 

This time I decided to compile my own copy of PHP 5.6, in order to include as minimal as possible modules and stuff.

  • Download the latest php from php.net
  • tar xvzf php-5.6.*.tar.gz
  • cd php-5.6*
  •  Execute configure

     
  • Then install it

     
  • Finally copy the init script

     
  • Add the service and make it autoload at boot time

We already have php-fpm installed in /usr/local/php5.6.

The last thing is to tune a little bit the php-fpm configuration file.

vim /usr/local/php5.6/etc/php-fpm.conf

 

The cherry of the pie – Installing opcache for php 5.6

There is one last thing – installing opcache for php5.6.  Opcache installation is pretty straight forward:

  • Install opcache from pecl (with pecl install opcache) or by manually compiling it
  • Modify php.ini config to enable opcache

     

Voalla ! Now we have WordPress at least 3x times faster …

That was all of it in summary:

  1. Installed Nginx and configured it to serve my blog
  2. Installed PHP5.6 as FPM and make it statically pre-spawn its childs
  3. Added PHP 5.6 init script and make it autoload at system startup
  4. Configured Nginx to communicate with PHP-FPM through unix socket
  5. Installed Zend Opcache for doing the opcode cache part

And here we are, what I’ve achieved by this setup was:

3x faster wordpress by the Apache Bench (ab) judgement.

 

Before I was doing about 8-9 requests per second, now I’m able to make about 30 requests per second.

I have already done some example optimizations for Magento sites, you can also check it here:

Optimize your server for best Magento performance

Leave a Reply

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

Captcha * Time limit is exhausted. Please reload CAPTCHA.