Lightning fast Wordpress on Apache and PHP-FPM with Batcache

Written by
Date: 2012-12-26 21:00:00 00:00


This is the third (and last) post of the series where the first two are:

In this post I will show you how to install Wordpress on LAMP (Apache2 MPM worker, PHP-FPM, MySQL) on Ubuntu server.

We will assume we already have the LAMP server installed and ready.

Install Wordpress

When installing Wordpress on Ubuntu I like to do it with one of these two options:

  • Using apt
  • Using svn

Using svn

I recommend you using this or apt rather than the tradicional way of downloading the .tar.gz file as upgrades are easier this way.

To install create a folder where you will install wordpress.

 sudo mkdir -p /var/www/yoursite/{public_html,logs}

Then download the latest stable copy of Wordpress in the public_html folder.

 cd /var/www/yoursite/public_html

Using svn, if subversion is not installed run: sudo apt-get install subversion and install Wordpress with:

 sudo svn co http://core.svn.wordpress.org/tags/3.5 .

To update:

 sudo svn sw http://core.svn.wordpress.org/tags/3.5.1/ .

You can read more about it here

Using apt

This is very easy, and the to keep the code up to date is also easy.

To install:

 sudo apt-get install wordpress

To update:

 sudo apt-get update

and:

 sudo apt-get upgrade wordpress

That will install wordpress on /usr/share/wordpress/ folder. You can create a symlink to /var/www/your-site/ and create the corresponding virtual host under /etc/apache2/sites-available/. Read more here

Configure Wordpress

Now that we have the code installed we need to finish the configuration, I am not going to go through the whole process as it is well documented all over the web. Your best shot is to follow the official instructions here

What I am going to cover here is how to make it really fast and efficient. Because we have installed Apache MPM worker and PHP-FPM serving static files like .css .js and others will be really efficient, maybe as efficient as with NGINX.

Caching Wordpress

The only way to make Wordpress fast in a small VPS server is by caching. Which method of caching should you choose? That depends on your needs and your configuration and the type of traffic you have.

If you have thousands of posts and your traffic is spread all over them, your best option is WP Super-Cache. And configure it to save the file for a long time on disk. The disk is always bigger than RAM and if you have the traffic spread between thousands of posts the provability to hit a post in RAM is small, but big to hit a WP-Super-Cache generated file on disk.

On the other hand, disk is slower than RAM so, if you have rather small traffic, but burst from time to time to a popular post, then any RAM option is better than disk. One option is Vanilla Wordpress and Varnish. Another is using Batcache, which is what I am going to cover here.

Installing APC

Make sure you have php-apc installed

 sudo apt-get install php-apc

Then install the APC Object cache backend. To activate this plugin:

  1. Verify that you have PHP 5.2.4+ and a compatible APC version installed.
  2. Copy object-cache.php to your WordPress content directory (wp-content/ by default).
  3. Done!

Installing Batcache

We will now install Batcache, which is used by wordpress.com.

To install it:

  1. Upload advanced-cache.php to the /wp-content/ directory

  2. Add this line the top of wp-config.php to activate Batcache:

 define('WP_CACHE', true);

Done. Now if suddenly one of your posts become popular, your site will be able to manage the load.

Final step

Restart Apache

sudo service apache2 restart

And you may restart php-fpm too.

sudo service php5-fpm restart

Done!

Conclusion

I have performed tests with blitz.io and for 250 concurrent users the server does not even see the load, the CPU use is under 1%. These results show that this configuration is really efficient.