Installation of Varnish caching server

Installation of Varnish caching server in Redhat/CentOS/Fedora

Varnish will significantly improve your website performance as it responses static pages from cache. After installation of Varnish, for same number of simultaneous requests less requests go to your web server as many of them will be served by Varnish. So, you can consider your web server simultaneous requests handling capacity is two or three times than earlier. Installation of Varnish is an easy task.  Here I will describe “Installation of Varnish caching server in Redhat/CentOS/Fedora” and configure it to work with Apache.

Installation of Varnish caching server in Redhat/CentOS/Fedora

  1. Check Server Architecture
    # uname -a
    Linux localhost.localdomain 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
    
  2. Get the latest stable RPMs repo from the page https://www.varnish-cache.org/installation/redhat and install it
    #rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpm 
    

     

  3. Install Varnish by yum command
    # yum install varnish
  4. Edit /etc/sysconfig/varnish file
    # vi /etc/sysconfig/varnish
  5. Edit the following lines
    VARNISH_LISTEN_PORT=80
    

    This is the listening port of Varnish and it is obviously http port(80). http traffic will come first Varnish then forwarded to Apache. So Apache listening port should be changed other than 80.

    VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
    VARNISH_ADMIN_LISTEN_PORT=6082
    

    These are the listening address and port to run some administrative command.
    Save and exit out of that file.

  6. Open up the /etc/varnish/default.vcl file
    # vi /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 like this:

    backend default {
      .host = "127.0.0.1";
      .port = "8080";
    }
    
  7. Configure Apache for working with Varnish
    So far we have told varnish that Apache ports will be running on 8080. However the default settings for Apache remain on port 80. We will correct the discrepancy now.
    Open up the Apache 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:

    Listen 127.0.0.1:8080
    NameVirtualHost 127.0.0.1:8080
    

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

     <VirtualHost 127.0.0.1:8080>
        ServerAdmin webmaster@dummy-host.example.com
        DocumentRoot /var/www/html/results
        ServerName www.example.com
        ErrorLog logs/example.com-error_log
        CustomLog logs/example.com_log common
    </VirtualHost>

    Save and exit the file.

  8. Restart Apache and Varnish to make the changes effective
    # /etc/init.d/httpd restart
    # /etc/init.d/varnish restart
    
  9. Allow port 8080 on your firewall if you have firewall enabled
# iptables -I  INPUT -p tcp   --dport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT

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

The above method is a quick method of  “Installation of Varnish caching server in Redhat/CentOS/Fedora” and any one can within 15 minutes complete this and enjoy the benefit. After installation of Varnish caching server you definitely will get better performance  for your static HTTP traffic.

2 thoughts on “Installation of Varnish caching server

    1. Varnish can work together both Nginx and Apache. It can be done easily. You need to configure Apache Listening port as example 8080 and Nginx listening port 8081. Varnish Listening port always is 80. Then Varnish on load sharing basis forward request to both Apache and Nginx. so the backend code with only Apache previously was
      backend default {
      .host = “127.0.0.1”;
      .port = “8080”;
      }

      The New code will be

      backend apache {
      .host = “domain.com”;
      .port = “8080”;
      }
      backend nginx {
      .host = “domain2.com”;
      .port = “8081”;
      }

Comments are closed.