ScaleScaleScaleScale

Tips / Nginx


How to install Varnish on Ubuntu

Varnish

Today I will explain How to install Varnish on Ubuntu Linux. In just two simple steps we can have our varnish-cache server working on Ubuntu. Before start you need to move to https://www.varnish-cache.org/releases and get the lastest stable version of Varnish, it is recommended to use the official Ubuntu repository provided in varnish-cache website.

Install Varnish on Ubuntu

Install the GPG key for the repo:

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

Next step is adding the repo to our apt sources lists

sudo nano -w /etc/apt/sources.list

Once you are in there, add this line to the file:

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

Save your file and exit.

Now let’s update the systema install varnish:

sudo apt-get update
sudo apt-get install varnish

Varnish Configuration

Now, I will assume that you have Nginx (or Apache in the worst scenario) installed and running on port 80, right? In order to work with Varnish you will have to change your 80 listen port for another one like 8080 for example. You can do this from Nginx or Apache configuration.

Why do you need to change the port of your web server?

Because now Varnish is the one that is going to recieve all the requests on 80 port, and then it will act a as a front end proxy to pass all the requests to the webserver.

Edit varnish configuration file

sudo nano -w /etc/default/varnish

Remove all the lines starting at the line that says “DAEMON_OPTS” under Alternative 2.
At the end your configuration should look like this:

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

Now let’s edit the Varnish VCL file /etc/varnish/default.vcl

sudo nano -w /etc/varnish/default.vcl

At this file you will configure and tell Varnish where the webserver is located, example:

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

Once you have this configured at Varnish, the only thing left is to restart the services in order to apply changes:

/etc/init.d/varnish restart
/etc/init.d/nginx restart

or

/etc/init.d/apache2 restart

Test Varnish

It can be easily done from the shell using curl:

[me@localhost ~]$ curl -I www.varnish-cache.org
HTTP/1.1 302 https://www.varnish-cache.org/
Server: Varnish
Location: https://www.varnish-cache.org/
Accept-Ranges: bytes
Date: Tue, 08 Apr 2014 13:32:12 GMT
X-Varnish: 1972635367
Age: 0
Via: 1.1 varnish
Connection: close

As you see, you should notice ‘Server: Varnish’ at the header.

Need more information? Check out the Varnish official website.

Popular search terms:

  • /etc/varnish/secret nginx configuration
  • Varnis lucid example
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking