Varnish Cache – Speed Up Your Webserver

Varnish Cache is an open source, state of the art web application accelerator. You install it on your web server and it makes your website fly. It really true, if you have a big website with huge visitors then Varnish Cache will be really a boon for you.
Varnish Cache

Even Facebook recommends and uses Varnish Cache to speed itself up.

So we are gonna see how to install and configure Varnish Cache on Ubuntu Operating system with Apache Web server. (This article is to install varnish on Ubuntu 10.04 but its almost the same for other versions of Ubuntu. The version of apache doesn’t matter .)

1. Installing Varnish Cache

I’m running all the steps in this tutorial with root privileges, so make sure you’re logged in as root. Use this command:

sudo su

Varnish is included in the Ubuntu’s Repositories so its quite simple to install Varnish:

 apt-get install varnish

Ubuntu will now install Varnish and all its dependencies.

2. Configuring Varnish

2.1 Backend Server

Varnish has a concept of “backend” or “origin” servers. A backend server is the server providing the content Varnish will accelerate. In most cases the backend will be webserver which is currently serving the pages. And mostly the backend or origin server is Apache webserver

Our first task is to tell Varnish where it can find its content. Start your favorite text editor and open the varnish default configuration file. (I’ve used nano)

nano /etc/varnish/default.vcl

Somewhere in the top there will be a section that looks a bit like this.:

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

We uncomment in this bit of text , making the text look like.:

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

Now, this piece of configuration defines a backend in Varnish called default. When Varnish needs to get content from this backend it will connect to port 8080 on localhost (127.0.0.1).

Varnish can have several backends defined and can you can even join several backends together into clusters of backends for load balancing purposes.

Have you noticed one thing ? I have set the origin server’s port as 8080. But our origin sever i.e. Apache webserver is running on port 80.Yes that true but we’ll be configuring Apache webserver to listen on port 8080 and Varnish on port 80 (Why?.. Its explained in the later part of this post)

Now that we have the basic Varnish configuration done, lets configure Apache to listen to port 8080

2.2 Configuring Apache Webserver

Now we’ll configure Apache Webserver to listen on port 8080. Ok.. so why its done?

All the web traffic passes through the default port 80. We want to display cached content to our viewers and we cannot configure two programs on one port so we need to configure Apache on port 8080 (any port other than 80 is fine. I’ve used 8080).

Open Apache’s ports configuration:

You need to edit these lines. there would be a lot of lines above and below these. Also there is a chance that they wont be together. If that the case use search function of nano (Ctrl + W)

NameVirtualHost *:80
Listen 80

Change them to:

NameVirtualHost *:8080
Listen 8080

Next edit the Apache virtual host’s configs. You need to do small modification of just one line (Mostly first) in each virtual host.

<VirtualHost *:80>

Change it to:

<VirtualHost *:8080>

Now Restart Apache Webserver

service apache2 restart

3 Starting Varnish

All is done. Only one thing left is to start Varnish. First make sure that no Varnish instances have started automatically (happens sometimes). So just kill all the instances first:

pkill varnishd

Now lets start Varnish:

varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000

Explanation:

-f /etc/varnish/default.vcl
The -f options specifies what configuration varnishd should use.
-s malloc,1G
The -s options chooses the storage type Varnish should use for storing its content. I used the type malloc, which just uses memory for storage. There are other backends as well, described in Varnish Documentation. 1G specifies how much memory should be allocated – one gigabyte. You can add more if you want.
-T 127.0.0.1:2000
Varnish has a built in text-based administration interface. Activating the interface makes Varnish manageble without stopping it. You can specify what interface the management interface should listen to. Make sure you don’t expose the management interface to the world as you can easily gain root access to a system via the Varnish management interace. I recommend tieing it to localhost. If you have users on your system that you don’t fully trust use firewall rules to restrict access to the interace to root only.

Now all the config is done. Go to your web address. Your page wont load faster at first because Varnish hasn’t cached any thing yet. But after a few page views everything will be fast as ever.

This is just the basic configuration of Varnish. It can be tweaked to a great extent to perform wonders. Just read the offial Varnish Documentation

  • http://www.mactonweb.com web design bangalore

    nice post I am new to this site really I got great information from this site