Wednesday, November 23, 2011

How To Install and configure Varnish Cache in ubuntu

Install & Configure Varnish on Ubuntu Lucid Linux

I decided to install Varnish on an Ubuntu 10.04 Lucid Linux machine.
1. Install Varnish
2. Edit the /etc/varnish/default.vcl file
3. Edit the /etc/default/varnish file
4. Edit apache settings
5. Restart Apache and Varnish
6. Useful commands and information

1. Install Varnish

sudo apt-get install varnish

2. Edit the /etc/varnish/default.vcl file

This is where we tell varnish where to find content. I want varnish to find content on the local machine on port 8080, or basically where to find the webserver. By default apache listens on port 80 so we will need to change the apache settings later on. Edit the /etc/varnish/default.vcl file and change the contents as shown.


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

3. Edit the /etc/default/varnish file

This file contains varnish’s startup settings. The default set up is mostly fine. The main thing here is to tell varnish which port to listen on and how much RAM it can use. I want varnish to listen on port 80 and use 128M of RAM.



DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,128M"

4. Edit apache settings

You must edit /etc/apache/ports.conf file and ensure apache will now listen on port 8080. If you have a vhosts setup you must also change which port the vhosts listen on.

So /etc/apache/ports.conf should look like this


NameVirtualHost *:8080
Listen 8080
.....

Change /etc/apache2/sites-available/default to also listen on port 8080, and do the same for any vhosts in this directory.


NameVirtualHost *:8080
Listen 8080
.....

Example vhosts



ServerName mormanski.net
.....

5. Restart Apache and Varnish

Restart apache.
sudo /etc/init.d/apache2 restart
And start Varnish too.
sudo /etc/init.d/varnish start

6. Useful commands and information

This netstat command will tell you which ports are open
netstat -anp --tcp --udp | grep LISTEN

You can use varnishlog to see what’s happening in real time
varnishlog