Install Varnish in front of Apache in Ubuntu

What is Varnish ?

Varnish is an amazing front-end cache that is useful for serving static pages and reducing load on your server. Basically Varnish is an  HTTP accelerator and a useful tool for speeding up a server.  In periods of high server activity and traffic, Varnish can be a life saver. In this article you ill learn how to install varnish also varnish configuration.

Varnish also known as caching HTTP reverse proxy, that developed for busy and heavy content sites, to significantly improve performance of web sites. It works by redirecting visitors to static pages whenever possible. Varnish can be install as an front end to any server that serves HTTP requests and configure it to cache the pages.

Following are the steps to Install Varnish in front of Apache in Ubuntu for caching static content.

Add Varnish Repository

The varnish site recommends installing the varnish package through their repository. so first you have to add varnish repository in your server. For Ubuntu 14.10/14.04/13.10 and older versions server run the following commands in Linux Terminal.

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

Install Varnish

Varnish 3.0 can be install by running the following command.

sudo apt-get install varnish

Configure Varnish

Now next we have to configure Varnish. This can be slightly different based on your CMS and/or framework and how your site is explicitly set up, but we will get you started with a basic configuration.

Once we have Apache and Varnish install on our server we have to configure Varnish on port 80 to serve the content while fetching it from apache which will run on port 8080.

Open below file with any editor

sudo vim /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"

The above configuration will run Varnish on port 80 annd varnish admin on port 6082 with given .vcl file path.

Once you have saved this file with above configuration setup open default.vcl file and update configuration for backend of varnish for fetching content from apache.

sudo vim /etc/varnish/default.vcl

Configuration should look like this –

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

This will setup backend on port 8080 for host 127.0.0.1 on which apache will listen.

Configure Apache

So far we have configure varnish backend on port 8080 so that apache ports will be running on 8080. However the default settings for apache are still on port 80. So we will update the apache conf in below configuration file.

sudo vim /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 all virtual host in /etc/apache/sites-enabled as well:

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

<VirtualHost 127.0.0.1:8080>

At last run the following command to enable caching by restarting Apache and varnish.

service apache2 restart
service varnish restart

Next, verify the Varnish by running the following command.

curl -I http://yourdomainname.com/

Access you domain using browser and instantly you can see the details of varnish working with below command:

varnishstat

Varnish live logs can check using below command:

varnishlog

Varnish Logs(Optional)

By default varnish logs are not enabled from configuration. You can enable varnish logs by updating varnish configuration in below files.

Open the following file:

vim /etc/default/varnishncsa

And set the following setting to “1″.

VARNISHNCSA_ENABLED=1

Now start the service to start logging:

service varnishncsa start

Logs can be checked on below location

tail -f /var/log/varnish/varnishncsa.log

 

Balvinder Singh

Hello, I am Balvinder Singh - DevOps Engineer with 2.5+ year of working experience with different server environments. Tag Line:-Linux | AWS| WHM |Monitoring | Virtualization | Optimization | Performance | Security | Release & Deployment. I love helping companies / clients to deploy their code / applicateions to well managed, optimized, secure server and can go extra mile to satisfy.

You may also like...

Leave a Reply

Your email address will not be published.