How to Install Varnish Cache?

This guide will briefly go over how to install and configure Varnish Caching. 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. It typically speeds up delivery with a factor of 300 – 1000x, depending on your architecture. Please keep in mind that this is a fairly advanced section and does require you to use root access to perform any actions described. Also know that unless you pay for a plugin to do so you can not control varnish Cache from the WHM interface.

Varnish Cache

Preparation

This will get Apache ready which should be done prior to the installation to avoid port conflicts.

  1. Log into your servers command-line interface as the root user.
  2. Edit the following file.
    vim /var/cpanel/cpanel.config
  3. Look for the line “apache_port” and change it to the following then save the file.
    apache_port=0.0.0.0:8081
  4. Run this command to update the settings.
    /usr/local/cpanel/whostmgr/bin/whostmgr2 –updatetweaksettings

Installation

This section will show you how to install Varnish 3 onto your dedicated server.

  1. Check what version of CentOS you are running by entering the following command.
    cat /etc/centos-release
    CentOS release 6.7 (Final)
  2. Run the following commands to add the Varnish repositories to your server (Please use the correct command for the current version you are running.).
    CentOS 6 – rpm –nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpm
    CentOS 5 – 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
  3. Install Varnish Cache via yum.
    yum install varnish

Configuration

  1. Edit the following file.
    vim /etc/sysconfig/varnish
  2. Add the following lines to the bottom of the file (After the # DAEMON_OPTS=”” line).
    DAEMON_OPTS=”-a :80 \
    -T localhost:6082 \
    -b localhost:8081 \
    -u varnish -g varnish \
    -s file,/var/lib/varnish/varnish_storage.bin,2G”
  3. Save the file.
  4. Restart the following services for the changes to take affect.
    service varnish restart
    service httpd restart

 

This configuration does the following:

  1. Listen on all IP addresses on port 80
  2. Set the administrative interface on port 6082 (the default)
  3. Forward requests to localhost on port 8081 (Apache server we changed earlier)
  4. Set the user/group for child processes to varnish:varnish
  5. Use the file storage mechanism at /var/lib/varnish/varnish_storage.bin and use only 2GB for the file.

Leave a Reply

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