• Home
  • My Work
  • How To Install Varnish + Memcached to improve your WordPress page load speed
https://torbjornzetterlund.com/wp-content/uploads/2014/05/wordpress-logo-notext-rgb-960x600_c.png

How To Install Varnish + Memcached to improve your WordPress page load speed


When a website traffic grows to see an increase in traffic, one of the components that shows stress the fastest is the backend database. If your database is not distributed and configured to handle high loads, it can easily be overwhelmed by a relatively modest increase in traffic. Using the Varnish + Memcached combination I can offload stress on the server.

I’m going to explain how to install Varnish + Memcached on a server that is running Ubuntu 14.04. Why am I installing Varnish + Memcached, the reason is to speed up the web page load time from my WordPress site.

What is Varnish + Memcached

Varnish is an HTTP accelerator to speed up a server, especially during times when there is high traffic to a site. It works by redirecting visitors to static pages whenever possible and only drawing on the server itself if there is a need for an active process.

Memcached is a caching system that works by temporarily storing information in memory that would usually be retrieved from a database. The next request for the in-memory information is then incredibly fast without putting stress on the backend database.

Prerequisites

Before we get started, you should have a regular, non-root user on your server who has access to sudo privileges.

Install Memcached

The first step will be to update our local package index. Then we can install our programs.

I assume you already have a LAMP stack installed, so we’re going to install memcached as well as a PHP extension that handles memcached interactions. You can get everything you need by typing:

sudo apt-get update
sudo apt-get install php5-memcached memcached

Check the Installation

Memcached is already installed and ready to go. We can test this by checking whether the memcached service is running by typing:

ps aux | grep memcached
memcache 24262 0.0 0.0 325392 1176 ? Sl 07:58 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
root 24586 0.0 0.0 11740 936 pts/0 S+ 08:35 0:00 grep --color=auto memcached

You can query the service for stats by typing:

echo "stats settings" | nc localhost 11211

If you ever need to stop, start, or restart the memcached service, this can be done by typing something like this:

sudo service memcached restart

Install Varnish

The varnish site recommends installing the varnish package through their repository. You can start that process by grabbing the repository:

sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

The next step is to add the repository to the list of apt sources. Go ahead and open up the file.

sudo nano /etc/apt/sources.list

Once inside the file, add the varnish repository to the list of sources.

deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0

Save and exit.

Finally, update apt-get and install varnish.

sudo apt-get update
sudo apt-get install varnish libvarnish-dev

Configure Varnish

Varnish will serve the content on port 80, while fetching it from apache which will run on port 8080.

Go ahead and start setting that up by opening the

/etc/default/varnish

file:

sudo nano /etc/default/varnish

Find the lines under “DAEMON_OPTS”— in the Alternative 2 section, and change the port number by “-a” to 80. The configuration should match the following code:

 DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

That’s the only change you need to make there. Save and exit out of that file and open up the default.vcl file:

sudo nano /etc/varnish/default.vcl

This file tells varnish where to look for the webserver content. It should already be configured to have the backend (ie. apache) listening in on port 8080.

We need to use this file for a secondary purpose. WordPress is chock full of various cookies that make caching it very difficult. In order to have varnish work as efficiently as possible, we need to tell it to drop all the cookies that don’t relate to the admin side of the WordPress site.

Additionally, we need to tell varnish to remove the cookies that make WordPress very difficult to cache.

The beginning of the default.vcl file should look like this:

[...]
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

# Drop any cookies sent to WordPress.
sub vcl_recv {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset req.http.cookie;
        }
}

# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;
        }
}

[...]

A cautious note – you to understand what functionality that you need use on the wordpress site to get varnish configured correctly, if you for instance selling things on your site using a shopping cart like Woocommerce your varnish configuration will look different, here is an example:

# Drop any cookies sent to WordPress.
sub vcl_recv {
        if (!(req.url ~ "(wp-login|wp-admin|cart|my-account|checkout|addons)")) {
                unset req.http.cookie;
        }
}
# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
        if (!(req.url ~ "(wp-login|wp-admin|cart|my-account|checkout|addons)")) {
                unset beresp.http.set-cookie;
        }
}

Configure Apache

So far we have told varnish that apache ports will be running on 8080. However the default settings for apache are still on port 80. We will correct the discrepancy now. Open up the apache ports file:

sudo nano /etc/apache2/ports.conf

Change the port number for both the NameVirtualHost and the Listen line to port 8080, and the virtual host should only be accessible from the localhost. The configuration should look like this:

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080

Change these settings in the default virtual host file as well:

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

The Virtual Host should also be set to port 8080, and updated line looks like this:

 <VirtualHost 127.0.0.1:8080>

Save and exit the file and proceed to restart Apache and Varnish to make the changes effective.

sudo service apache2 restart
sudo service varnish restart

To check your varnish status you can use this command.

varnishstat

To enable Varnish and Memcached in your WordPress

Now after you have installed  Varnish + Memcached on your server, there is only one step left to make WordPress take advantage of Varnish + Memcached, install the WordPress plugin  W3 Total Cache, after plugin is installed you can configure each option to improve your sites performance.

If you have any comments on this, leave one below.

One Respond

  • John Peterson on April 6, 2017, 19:55:50

    Great info and great cache systems. Will need a better way to install for the general public to catch on. Of course you can say oh well, but the public is saying that now.

    Reply to John

Menu