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

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/port to 8080 and Save settings
 
2. install Varnish on your server:
Execute commands:
 
#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 configuration of Varnish /etc/sysconfig/varnish and change value of VARNISH_LISTEN_PORT to 80
VARNISH_LISTEN_PORT=80
 
4. Edit Varnish Config: /etc/varnish/default.vcl
Use following content (don't forget to set correct IP address):
 
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. Now start Varnish service on your server:
chkconfig varnish on
service varnish start
 
6. 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