[Ubuntu/Debian] How To Install and Configure Varnish

LawrenceLawrence Moderator
edited May 12 in Linux Applications


Introduction

Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as heavily consumed APIs. 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. In other words it's an HTTP accelerator and a useful tool for speeding up a server, especially during a times when there is high traffic to a site. It works by redirecting visitors to static pages whenever possible and only drawing on the virtual private server itself if there is a need for an active process.


Requirements

Only requirements are to have user with sudo privileges (root is fine) and Apache installed on your VPS.

Tutorial on how to create new user and grant him sudo privileges can be found here.
Tutorial on how to install LAMP stack (Apache, MySQL, PHP) can be found here.


Installation

The varnish site recommends installing the Varnish package through their repository. You can start that process by grabbing the repository:

sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

Add the repository to the list of apt sources:

sudo nano /etc/apt/sources.list

Paste line below in the file:

deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0

Save and exit ( Ctrl + O, Ctrl + X).

Finally, update apt-get and install Varnish:
sudo apt-get update
sudo apt-get install varnish

Configuration

Once you have Varnish installed, you can start to configure it to ease the load on your server from future visitors. Varnish will serve the content on port 80, while fetching it from apache which will run on port 8080.

Let’s go ahead and start setting that up by opening the /etc/default/varnish file:

sudo nano /etc/default/varnish

Find and make the configuration match the following code:
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
Save and exit ( Ctrl + O, Ctrl + X). Open up the default.vcl file:

sudo nano /etc/varnish/default.vcl

This file tells Varnish where to look for the webserver content. Although Apache listens on port 80 by default, we will change the settings for it later. Within this file, we will tell Varnish to look for the content on port 8080. The configuration should look like this:
backend default {
.host = "127.0.0.1";
.port = "8080";
}

Configuration of Apache

First of all we need to change default port for Apache from 80 to 8080. Open up the Apache ports file:

sudo nano /etc/apache2/ports.conf

Change the port number for both the NameVirtualHost and the Listen line to port 8080, and the virtual host should only be accessible from the localhost. The configuration should look like this:
NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080
Change these settings in the default virtual host file as well:

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

The Virtual Host should also be set to port 8080, and updated line looks like this:

<VirtualHost 127.0.0.1:8080>

Save and exit ( Ctrl + O, Ctrl + X). Restart Apache and Varnish to make the changes effective:
sudo service apache2 restart
sudo service varnish restart

Conclusion

Accessing your domain should instantly take you to the varnish cached version, and you can see the details of varnish’s workings with this command:

varnishstat
Sign In or Register to comment.