Ubuntu 12.04 – Install Varnish 3 in front of Apache 2

Step 1: Install latest version of Varnish (3.0)

These first three lines are probably not required on Ubuntu 12.04 because the main repository already contains version 3, but you can use it anyhow. Older version of Ubuntu do need it though!

curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
echo "deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0" | sudo tee -a /etc/apt/sources.list
sudo apt-get update
sudo apt-get install varnish

Step 2: Set backend server (Apache)

Edit the varnish configuration file.

sudo nano /etc/varnish/default.vcl

Set the backend server. The lines below say that the backend server (Apache) is available on localhost at port 8080. Varnish will run in front of it on port 80.

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

Step 3: Bind Apache to port 8080 instead of 80

The Apache webserver is by default bound to port 80. To get Apache to run behind the reverse http proxy Varnish we have to define a different port. The port 8080 is widely used as alternative port.

sudo nano /etc/apache2/ports.conf

Replace the NameVirtualHost and Listen part with the following:

NameVirtualHost *:80
NameVirtualHost *:8080
Listen 8080

These lines say that virtual hosts can be configured for port 80 and 8080. The active port that Apache actually listens to is defined with the “Listen” property.

Your virtual hosts files probably start with something like:

<VirtualHost *:80>

You can configure them to support also extra ports with:

<VirtualHost *:80 *:8080>

Don’t forget to also change this in /etc/apache2/sites-enabled/000-default.
Restart Apache to let the changes take place:

sudo service apache2 restart

Step 4: Starting Varnish

You can start Varnish manually (for a quick test now, but you may skip this) or configure it to start automatically (recommended!).

Manually

Kill any possible running Varnish processes.

sudo pkill varnishd

Start Varnish on port 80 with the default.vcl configuration file and a memory cache storage of 256M and the Administration available on localhost at port 6082.

sudo varnishd -f /etc/varnish/default.vcl -s malloc,256M -T 127.0.0.1:6082 -a 0.0.0.0:80

Automatically

The installation of Varnish with apt-get, already installed a startup script at /etc/init.d/varnish. You can also use it to stop or restart varnish:

sudo /etc/init.d/varnish start|stop|restart|force-reload

Edit the following file to modify the settings of this script:

sudo nano /etc/default/varnish

By default it’s configured to use these settings:

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

Replace this with the following lines to start Varnish on port 80 instead of the default port 6081. The administration must still be a different port like the default 6082.

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

Maybe if you don’t have enough memory, you should set the value 256m to something lower. Varnish has around 1kB of overhead per object cached, so the actual used memory could be double!

Reboot your machine and see if everything is coming up perfectly! With firebug you can see the response headers of a webpage. If it’s served through Varnish, the response headers will contain a line like:

Via	1.1 varnish

You can still access you website by opening it directly through Apache by adding port 8080 like http://localhost:8080/.

Everything should be working fine now. Enjoy!

Step 5 / Logging (optional):

If your server has Webalizer or AWStats installed to display website statistics then Varnish cache hits won’t get logged anymore in the Apace access log and will therefore not be visible in the statistics.

To fix this you can enable varnishncsa that will display Varnish logs in Apache / NCSA combined log format. This format is compatible with Webalizer en AWStats.

Open the following file:

nano /etc/default/varnishncsa

And set the following setting to “1″.

VARNISHNCSA_ENABLED=1

Now (re)start the service to start logging:

service varnishncsa restart

The service is located at /etc/init.d/varnishncsa, so it will automatically start when your server (re)boots.

Related links:

Read more about installing and configuring Webalizer at http://pietervogelaar.nl/ubuntu-11-04-openpanel-install-webalizer.

Tags: ,,,