Install & Configure Varnish in Front of Apache Web Server in CentOS/RedHat

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.So we can Configure Varnish in Front of Apache for making our site fly.

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.

Steps to Install & Configure Varnish in Front of Apache Web Server in Centos

Following are the steps to install and configure Varnish in front of Apache Web Server for caching static content.

Get the RPM of Varnish

rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.el5.centos.noarch.rpm

Install the Varnish using yum. This step can be executed in Centos/Redhat/Fedora

yum 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

vim /etc/sysconfig/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.

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.

vi /etc/httpd/conf/httpd.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 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 httpd restart
service varnish restart

Next, verify the Varnish by running the following command.

# curl -I https://www.linuxtweaks.in

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Mon, 25 Feb 2013 17:01:57 GMT
X-Varnish: 896827433
Age: 0
Via: 1.1 varnish
Connection: keep-alive

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

Logs can be checked on below location

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

View more posts in this category !!!

Install Varnish in Apt based Distro’s.

LinuxTweaks

Linuxtweaks Blog helping Server Admin to Manage their servers, Desktop users for making more friendly with linux. Tutorials , guides and tips for linux server maintenance. Here you can learn how to tweak linux servers with code and how to manage it properly.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *