Monday, July 2, 2012

BitNami WordPress now with Varnish Cache

We have just updated BitNami WordPress to version 3.4.1 This version bundles a ready-to-use version of Varnish Cache. 

Varnish Cache is a web application accelerator (also known as a caching HTTP reverse proxy) that can be installed and configured in front of any HTTP server and takes care of caching the content being served. This blog post will show you how to configure a BitNami WordPress Amazon micro instance to use the Varnish for improving the performance.

By default, Varnish is disabled. You can enable it by renaming the control script:

$ cd /opt/bitnami
$ sudo mv /opt/bitnami/varnish/scripts/ctl.sh.disable /opt/bitnami/varnish/scripts/ctl.sh
$ sudo ./ctlscript.sh start varnish

Varnish is installed with a default configuration file, agnostic to the web application being cached. Using this configuration file, although achieving high performance, could lead to some contents not bring properly refreshed in the cache so your users would get an outdated version of your site. The solution is to use a customized VCL configuration file. A good source is the Varnish examples page. In this example, we will change our default.vcl configuration file to a WordPress-specific one (you can find the source file here).

$ sudo mv wordpress.vlc /opt/bitnami/varnish/etc/varnish/

Varnish runs on port 81 by default. You may want to change it to port 80 and change Apache to a different port. We are going to use port 81 for Apache and port 80 for Varnish in this example. You will need to perform the following changes in the server configuration files:

/opt/bitnami/apache2/conf/httpd.conf

Listen 80  change to  Listen 81

/opt/bitnami/varnish/scripts/ctl.sh

VARNISH_PORT=81 change to VARNISH_PORT=80

/opt/bitnami/varnish/etc/varnish/default.vcl where it is specified the Apache port.

backend default {
    .host = "127.0.0.1";
    .port = "80";  change to  "81"
}     

You can now restart the servers:

$ sudo /opt/bitnami/ctlscript.sh restart

In our real life tests running WordPress on an Amazon Web Services micro instance server, a Varnish-enabled instance was able to accommodate between 5 and 10 times more simultaneous users than a vanilla WordPress server.