Installation and Configuration of Varnish with Apache 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. 


For performing the steps of this guide, apache needs to be deployed on your VPS and you will require a user having sudo privileges.

In order to deploy Apache on your VPS, run this command given below from the apt-get repository.

sudo apt-get install apache2


Step One—Varnish Installation

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 | 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 lucid varnish-3.0

Save and then exit. 

Lastly, update apt-get and deploy varnish.

sudo apt-get update
sudo apt-get install varnish


Step Two—Varnish Configuration

When you have installed varnish and apache, 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 apache 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 uncommented, and 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"

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. By default Apache uses port 80 to listen to, but we would modify its settings later. Inside this file, varnish would be told to seek on port 8080 for the content. The configuration would look like as given below:

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


Step Three— Apache Configuration

Till now varnish has been told that apache ports would be operating on 8080. Whereas apache’s default settings still exist on port 80. So now we need to correct that discrepancy. 

Through the command given below apache ports file can be opened up:

sudo nano /etc/apache2/ports.conf

Edit the number of the port for both the Listen line and NameVirtualHost to port 8080, and the virtual host need to be available only from the localhost. The configuration would look like as given below:


In the default virtual host file also, modify these settings:

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

The Virtual Host needs to be fixed to port 8080, and modified line should look as given below:


Continue to restart Varnish and apache after saving and exiting the file for the changes to take place.

sudo service apache2 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:



KB Admin has written 28 articles

Leave a Reply