How to install Varnish with CPanel and CentOS to cache static content on serve

There are few things you need to do to install Varnish with Cpanel (WHM):

  1. Let Apache listen on port 8080 – you can do it by editing httpd.conf or directly in WHM: Tweak Settings menu -> set value of field Apache non-SSL IP/portto 8080 and Save settings
  1. install Varnish on your server:

Execute commands:

#install varnish repository

rpm -Uvh

#yum installation of Varnish

yum install varnish

  1. Edit configuration of Varnish /etc/sysconfig/varnish and change value of VARNISH_LISTEN_PORT to 80


  1. Edit Varnish Config: /etc/varnish/default.vcl

Use following content (don’t forget to set correct IP address):

backend default {

  .host = “”;

  .port = “8080”;


sub vcl_recv {

 if (req.url ~ “\.(png|gif|jpg|swf|css|js)$”) {




# 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;



  1. Now start Varnish service on your server:

chkconfig varnish on

service varnish start

  1. Now you are all set.

Now you can monitor your varnish cache e.g. with commandline tool: varnishstat

if you want to make changes to your varnish configuration, test your configuration changes before restarting varnish with following command:

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

This entry was posted in CentOS, cPanel. Bookmark the permalink.

Leave a Reply