Varnish Cache Can Vastly Speed Up Your WordPress Site

Depending on the complexity of your WordPress theme, performance can actually be quite slow on AWS. That’s where Varnish can make a tremendous difference. Varnish is a cache which stores copies of pages for users that aren’t logged in (most aren’t). When requests come in to your server, Varnish returns these pre-built pages instantly rather than requiring Apache, PHP and WordPress to rebuild the page for each request. Later, W3 Total Cache can be set up to regularly pre-build pages of your site so they are always ready for Varnish. The power of Varnish is dramatic when interest in your site spikes such as an appearance on BoingBoing, Slashdot or Oprah or @itsthegrumpycat.

We’ll set up Varnish to listen for HTTP (web) traffic on port 80 with Apache listening only to Varnish on port 8080. When Varnish needs a page to be built manually, it will pass the request on to Apache port 8080.

To get started, we need to install Varnish on our instance:

sudo apt-get install varnish

Edit Apache’s ports.conf file to listen on port 8080:

sudo nano /etc/apache2/ports.conf

Here’s what it should look like:

NameVirtualHost *:8080
Listen 8080

Edit your Apache site configuration file to respond to port 8080:

sudo nano /etc/apache2/sites-available/wordpress

Change the VirtualHost line from *:80 to *:8080

<virtualhost *:8080>

You may need to change your Apache default site to listen on port 8080 or disable it:

sudo a2dissite 000-default

Move the original Varnish configuration files to your home directory as backups (in case you decide to review them later):

sudo mv /etc/default/varnish ~/default-varnish-old
sudo mv /etc/varnish/default.vcl ~/varnish-default-vcl.old

Replace the Varnish default configuration with the file below (copy and paste).

sudo nano /etc/default/varnish

[sociallocker id="3080"] Replace the Varnish VCL configuration with the file below (copy and paste). Note: you may get VCL errors when you attempt to restart Varnish if you do not properly maintain line breaks when pasting.

sudo nano /etc/varnish/default.vcl


Now, let’s restart Apache and Varnish:

sudo service apache2 restart
sudo service varnish restart

Remember, if you get VCL errors at this point, it may be because of broken line-breaks from pasting in the Varnish default.vcl file.

Visit your site at http://yourdomain.com and everything should begin working with Varnish. The second time you load a static page, it should be significantly faster.

Next step is Installing the W3 Total Cache plugin.

Please feel free to post corrections, questions or comments below. You can also follow me on Twitter @reifman or email me directly.

Sharing this page supports our site:
Tweet about this on Twitter0Share on Facebook0Share on Google+3Share on Reddit0Share on LinkedIn0Share on TumblrEmail this to someoneShare on StumbleUpon0

About The Author