Installation of WordPress, Nginx, PHP, and Varnish on Ubuntu 12.04

About Varnish


An HTTP accelerator, Varnish, is a helpful tool to speed up a server, particularly at those times when a site is experiencing very high traffic. It operates by diverting visitors onto static pages as and when possible and only draws on VPS whenever any requirement comes up for an active process.

Setup


A number of prerequisites are there before you begin working through this guide. A user having root privileges is needed, WordPress and the LEMP stack should already be deployed on your server. 

Step One—Varnish Installation


As soon as all the preconditions required for configuring varnish with wordpress are fulfilled, you can begin the installation process of Varnish.

It is recommended by the varnish site that varnish package should be installed via their repository.

That process can be started by accessing the repository:

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


Next step involves adding the repository to an apt sources’ list. Continue and open that file.

sudo nano /etc/apt/sources.list


Within the file, the varnish repository should be added to the sources’ list.

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


Save and then exit.

Lastly, update apt-get and deploy varnish.

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

 

Step Two—Varnish Configuration


When you have installed varnish and nginx, you need to begin configuring them for easing your server load from prospective visitors. 

Port 80 would be utilized by Varnish for serving the content, whereas fetching it through nginx will operate on port 8080.

Now we’ll go further and set that up by opening the varnish file- /etc/default/:

sudo nano /etc/default/varnish


All the lines below “DAEMON_OPTS”—under Alternative 2 needs to be found and port number should be changed from “-a” to 80.  The configuration should be made to match the code given below:

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


Only that much modification was required there. On saving and exiting from that file, default.vcl file should be opened up:

sudo nano /etc/varnish/default.vcl


From this file, Varnish comes to know about where the web server content is located. The configuration should already be established as such that the backend (ie. nginx) should listen in on port 8080.

This file is needed to be utilized for another secondary purpose. WordPress is loaded with a number of cookies which makes caching extremely difficult for it. So for varnish to work at maximum efficiency, we will ask it to  we need to tell it to abandon all those cookies that are not related to WordPress site’s admin site. Also, varnish need to be told for removing all those cookies that make worpdress’s caching very difficult. The default.vcl file’s starting should look like as given below:

[...]
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 Three— Nginx Configuration


Whereas varnish has been configured such that it expects nginx ports to be operating on 8080, nginx’s default settings still exist on port 80. So now we need to correct that discrepancy. 

You need to open up the virtual host file containing Wordpress information:

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


The Virtual Host needs to be fixed to port 8080 and it should be available only from the localhost. The modified line would look like as given below:

[...]
server {
        listen  127.0.0.1:8080; ## listen for ipv4; this line is default and implied
        [...]


One final thing to be done before beginning to run varnish on our site, the default enabled virtual host needs to be removed.

sudo rm /etc/nginx/sites-enabled/default


However, the template will be accessible in the sites-available directory, in case you require it again.

Step Five—Restart


On making all the needed changes, restart nginx and varnish.

sudo service nginx restart
sudo service varnish restart


When you access your domain, you should be immediately forwarded to the cached version of Varnish, and in order to view the details of workings of Varnish you can type in the command given below:

varnishstat

 

KB Admin has written 28 articles

Leave a Reply