Varnish is an HTTP accelerator designed for content-heavy dynamic web sites. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator. Varnish is focused exclusively on HTTP, unlike other proxy servers that often support FTP, SMTP and other network protocols.
Before we install Varnish, we should have apache already installed. To install Apache, we should simple execute:
# sudo apt-get install apache2
Varnish should be available for install in the standard Ubuntu repositories. However, before we uninstall it we need to make sure that we are going to import the correct and latest varnish repository. We will start with importing their key:
# sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
Then, we should open /etc/apt/sources.list file for editing and add the following line:
deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0
We need make sure that our repositories are up-to-date. After then, we are ready to install Varnish.
# sudo apt-get update
# sudo apt-get install varnish
Once we have Varnish installed, we should setup some basic configuration parameters. By default, Varnish will listen on port 80, so we need to edit Apache configuration and set apache to listen on different port. In order to change that port that Apache listens on another port, we should open the /etc/apache2/ports.conf file and edit as follows:
NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080
Then, we should also make changes in the default virtual host file (and in every other virtual host file that exists):
<VirtualHost 127.0.0.1:8080>
In order to set varnish parameters, we should open the default Varnish configuration file, /etc/default/varnish, using our favorite editor, and uncomment the following directive:
DAEMON_OPTS="-a :80
-T localhost:6082
-f /etc/varnish/default.vcl
-S /etc/varnish/secret
-s malloc,256m"
That is all that we need to change in that file. Next, we open /etc/varnish/default.vcl file and edit as follows:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Please note that in 8080 is the port on which Apache listens. If we choose another port for Apache to listen on, that we should use that port number instead. We are done with the basic configuration. Next, we need to restart the services and all traffic will go through Varnish:
# sudo service apache2 restart
# sudo service varnish restart
If we want to check the current Varnish status and the cached sessions and content that is served through Varnish, we should execute:
# varnishstat
Hitrate ratio: 3 3 3
Hitrate avg: 0.3745 0.3745 0.3745
869844 4.97 2.89 Client connections accepted
31200592 164.04 103.52 Client requests received
11705560 58.66 38.84 Cache hits
Your pages are not being cached using Varnish and you can start by further referring to Varnish documentation to optimize your configuration.