Friday 16 January 2015

Speed Up Your WordPress Website Performance With Varnish Cache

Lets face it, WordPress standard installation is slow. And installing a number of plugins, themes and images makes it a lot more slower. You can minify your CSS and JS with help of “Better WordPress Minify” plugin, or you can use EWWW Image Optimizer plugin to reduce your images file size. However, there are lot of other factors that affects your WordPress site performance.

Possibly by now, you'll be thinking to Hire WordPress Expert to get the job done. But, there's another viable solution. Wondering, what's the solution? Varnish Cache is a web-application accelerator which is designed for dynamic websites with heavy content.

Varnish Cache – A Foreword

This caching system stores copies of pages for readers who aren't logged in into WordPress admin. Whenever users requests for something it reaches to Varnish instead of the server. And then, Varnish looks for what user has requested and then forward the request to the server. The server sends the response to Varnish, which in turn sends that response to user's original request. But if Varnish would only perform such function, it wouldn't be much of help to us.

However, Varnish also help improve performance by storing the responses it has received from the server in the virtual memory. And so, Varnish is able to quickly send the next response to the user instantly from its cache, and thereby the load on server to process users requests is reduced significantly. This eventually help improves the response time and your process requests can be carried on great speeds.

How to Install Varnish Cache?


We’ll be setting up Varnish to listen to all web requests on port 80, while Apache will be supporting Varnish on port 8080. So, when Varnish will ask for a page that needs to be built manually, the request will get passed on to the Apache port 8080. Let's look at Varnish installation on our instance:


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



sudo nano /etc/apache2/ports.conf

You'll get the following outcome:

NameVirtualHost *:8080

Listen 8080



Next, you need to edit your “Apache” website config file in order to respond to port 8080:

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

Now, change the virtual host from *:80 to *:8080

<virtualhost *:8080>


You'll require to change your Apache default website to listen for web request on port 8080 or need to disable it:


sudo a2dissite 000-default


In case, you've decided to review your original Varnish configuration files later, you'll have to move those files to your home directory in the form of backups.

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

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



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

sudo nano /etc/default/varnish



Let's look at the configuration file that needs to be replaced:

/etc/init.d/varnish expects the variables $DAEMON_OPTS, $NFILES and $MEMLOCK

# to be set from this shell script fragment.

#

# Should we start varnishd at boot? Set to "no" to disable.

START=yes

# Maximum number of open files (for ulimit -n)

NFILES=131072

# Maximum locked memory size (for ulimit -l)

# Used for locking the shared memory log in memory. If you increase log size,

# you need to increase this number as well

MEMLOCK=82000

# Default varnish instance name is the local nodename. Can be overridden with

# the -n switch, to have more instances on a single server.

# INSTANCE=$(uname -n)



How to Configure Varnish?

Once Varnish is installed, edit your port and change it to 80. Just remember not to change your Apache port yet, and keep it running till you're sure that everything complies properly. Moreover, if you're not sure about your configuration files, configure Varnish for port 81, and the perform a side-by-side test without losing your users:

nano /etc/default/varnish

DAEMON_OPTS="-a :80 \

             -T localhost:6082 \

             -f /etc/varnish/default.vcl \

             -S /etc/varnish/secret \

             -s malloc,256m"



Next, make you'll need to make copy of the original VCL file:


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


Configuring Apache


After configuring your Varnish cache, edit your Apache (or HTTP Server) so as to use some other port. (8080) Make sure to verify whether virtual host also configs or not.

nano /etc/apache2/ports.conf

NameVirtualHost *:8080

Listen 8080


Now restart the HTTP Server and Varnish. Varnish will listen to HTTP (web) traffic on port 80, and Apache in 8080.  But, how does Varnish will come to know where it needs to look for the HTTP Server? For this purpose, look at the top of default.vcl file.


/etc/init.d/apache2 restart

/etc/init.d/varnish restart

Getting Rid of Content from WordPress


In case you need to purge your WordPress website content manually or automatically when the posts are saved, install the Varnish HTTP Purge extension to your site. For doing so, add the following line of code to your
wp-config.php file:

define('VHP_VARNISH_IP','126.0.0.1');


Wrapping Up!


Adding plugins, images and other elements to your WordPress site makes it sluggish. However, you can make your WordPress site performance faster with help of Varnish Cache. This post will help you gain insight on Varnish cache, it's installation and configuration. 

1 comment: