Tutorials

How to setup Varnish + Nginx with WordPress on Ubuntu 12.04

What is Varnish

Varnish is a caching HTTP reverse proxy / web application accelerator. It is installed infront of Apache or Nginx on a server and it can increase the speeds by upto 300-1000 times, depending on the server architecture and configuration.

Prerequisites

You need have a user with root / sudo privileges for a Ubuntu 12.04 server with Nginx, MYSQL and PHP installed.
How To Install (LEMP), Linux, nginx, MySQL, PHP stack on Ubuntu 12.04

You also require WordPress to be installed on the server.
How to install WordPress and Nginx on Ubuntu 12.04

Step 1 : Install Varnish on your server

It is recomended that one should install varnish from there repo, which can be done using the following command :

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

Now add the repo to the apt sources. For this we first need to open the file in any editor of your choice (we are using nano for this example)

sudo nano /etc/apt/sources.list

Add the following line at the end of the list in the file :

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

Install Varnish :

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

Step 2 : Now we configure the fresh Varnish Install. We will setup Varnish to run on port 80 and Nginx to run on port 8080.

Open the file Varnish file in the etc/default folder

sudo nano /etc/default/varnish

In this file you will find a section that looks like :

## Alternative 2, Configuration with VCL

In this section add the following lines and save and exit the file :

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

Step 3 Configure default.vcl file for Varnish for WordPress

Open the file :

sudo nano /etc/varnish/default.vcl

The following blocks shoudl look like :

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;
        }
}

Step 4 Configure Nginx for Varnish

Since we have configured Varnish to detect Nginx running at port 8080, we have to configure Nginx for the same. Open the WordPress Nginx configuration file (for this example purposes we call this file as wpnginx) :

sudo nano /etc/nginx/sites-available/wpnginx

In your file set the 'listen' value to 8080

server { 
listen 8080;
....

Also make the same change in your nginx default file (/etc/nginx/sites-available/default)

Step 5 Restart Nginx and Varnish to implement the changes.

Test that the change nginx files do not have any error using the command :

nginc -t

This should return the following if there are no errors :

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx and Varnish

service nginx restart 
service varnish restart

Copyright © 2018 WPOven