How to Install and Configure Varnish with cPanel

How to Install and Configure Varnish with cPanel (WHM)

There are a few steps you need to follow to install Varnish with cPanel (WHM):

1. Configure Apache to Listen on Port 8080

You can change the Apache listening port by editing httpd.conf or directly from WHM:
Tweak Settings menu > set value of field Apache non-SSL IP/port to 8080 and Save settings.

2. Install Varnish on Your Server

Execute these commands to install Varnish:

# install varnish repository
rpm -Uvh https://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm

# yum installation of Varnish
yum install varnish

3. Edit Varnish Configuration

Edit the configuration file: /etc/sysconfig/varnish and change the value of VARNISH_LISTEN_PORT to 80.

VARNISH_LISTEN_PORT=80

4. Edit Varnish VCL Configuration

Edit the file: /etc/varnish/default.vcl
Use the following content (make sure to set the correct IP address for your backend):

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

sub vcl_recv {
    if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
        return(lookup);
    }
}

# strip the cookie before the image is inserted into cache.
sub vcl_fetch {
    if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
        unset beresp.http.set-cookie;
    }
}

5. Start the Varnish Service

Enable and start the Varnish service on your server:

chkconfig varnish on
service varnish start

6. Verification and Monitoring

You are now all set.
Monitor your Varnish cache with the following command line tool: varnishstat

Testing Varnish Configuration

If you want to make changes to your Varnish configuration, always test your configuration before restarting Varnish with this command:

varnishd -C -f /etc/varnish/default.vcl

It is recommended to maintain backups of your configuration files before making changes.

Be careful when editing configuration files and restarting critical services on production servers. Always test in a staging environment if possible.

×