Installation and Configuration of Varnish with Apache on Ubuntu 12.04

About Varnish

Imagine your site is experiencing with very high traffic and it is causing very slow to operate then An HTTP accelerator, Varnish comes as handy tool  to speed up a server. 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. 


You will need to deploy apache on your VPS,  Execute 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 is adding the repository to an apt sources’ list. Continue and open that file.

sudo vi /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

After you are done installing varnish and apache it is now time to configure them for minimization of 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 vi /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 vi /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 vi /etc/apache2/ports.conf

You will need to 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 vi /etc/apache2/sites-available/default

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


Once configuration is done you will need to restart the varnish and apache as shown below:

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 46 articles