How To Install and Configure Varnish To Run with Apache 2 on Ubuntu Server

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 – 1000x, depending on your architecture.

Why Varnish :

  1. To reduce server load.
  2. To make a website load faster, because cache stored in RAM.
  3. To gain more visitors, because every webmaster love it.

As Varnish can be installed and run along with either Nginx or Apache but in this tutorial I will show you how to install and setup Varnish on Apache-based Ubuntu server.

varnish-apache-gootum

Prerequisites :

To install varnish on server you will need to have both, a user with sudo privileges and Apache install on your VPS.

To install Apache on your VPS you can simply use a single command from apt-get repository.

sudo apt-get install apache2

Step 1 : Install Varnish

Varnish is available for install in the standard Ubuntu repositories. Before we install it we will import a key

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

In next step add repository to list of apt source, to do this you should open the file /etc/apt/source.list and add following line

sudo nano /etc/apt/sources.list

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

You need to make sure all repositories are up-to-date and then install varnish

sudo apt-get update

sudo apt-get install varnish

Step 2 : Varnish Configuration

This is how to setup Varnish. Once you have Apache and Varnish install, you should set some basic configuration parameters. All you have to do is making Varnish to run on port 80 while Apache on port 8080. To do the same edit a file /etc/default/varnish.

nano /etc/default/varnish

Uncomment all of the lines under “DAEMON_OPTS”—under Alternative 2, 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"

Once done hit Control+O to save then Control+X to exit.

Now you have to edit the file /etc/varnish/default.vcl. This file tells varnish where to look for the webserver content. Normally Apache listens on port 80 by default, we will change the settings for Apache later. Within this file, we will tell varnish to look for the content on port 8080. This step is also called as to set the backend server. Edit a file using nano

nano /etc/varnish/default.vcl

Make configuration should look like this

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

The config above says that Apache as the backend server is available on localhost at port 8080 and Varnish will run in front of it listening on port 80 that previously used by Apache.

Step 3 : Apache Configuration

You have setup a varnish to work on port 80 and you have told varnish that apache ports will be running on 8080. Now you have to make Apache to work on port 8080. We will correct the discrepancy now. Open up the apache ports file:

 sudo nano /etc/apache2/ports.conf

Change the port for both NameVirtualHost and Listen to 8080. The configuration should look like this:

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080 

Also make the changes in default virtual host file as its still listing on port 80 change and make it to 8080.

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

updated line should look like this:

 <VirtualHost 127.0.0.1:8080>

Once you have done with all the configuration for Apache and Varnish, you should restart both to make the changes effective.

sudo service apache2 restart
sudo service varnish restart

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

varnishstat

Hope you find this tutorial helpful. Feel free to ask questions! Don’t forget to like or to leave a comment if its really help you.

Advertisements

3 thoughts on “How To Install and Configure Varnish To Run with Apache 2 on Ubuntu Server”

  1. Hi, can I apply this on an Odoo v8 Server (Ubuntu 14.04 LTS Linode VPS)?? I just did your mod_wsgi on Ubuntu/Linux Server tutorial.. many thanks!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s