Home Home Page WHM How to install Varnish on cPanel?

A quick reminder on what varnish is.

Varnish is a web cache that runs on linux, it listens on port 80 (the usual HTTP port) and connects to an web server such as Apache running on al alternate port on the server, such as 8080. Varnish stores / caches the copies of your pages and when requested by a visitor it serves those pre-built pages without having to process them over and over again.

Read more: https://host4geeks.com/optimizing-your-server-for-wordpress/

Since Varnish listens on Port 80 (HTTP), we will first need to change the port for Apache running on our server. We will be changing the Apache port to 8080. To do this, login to your WHM as root, click on Tweak Settings from the sidebar, head over to the system tab and change the Apache Non-SSL port to 8080.

Now, we can proceed with the installation of Varnish. You need to be logged in as root via SSH. Type:

Install Varnish Repository

rpm -Uvh http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm

Install Varnish

yum install varnish

Once the installation completes, we will configure Varnish to listen on port 80.

Edit the file /etc/sysconfig/varnish using your preferred editor.

nano /etc/sysconfig/varnish


Varnish is not a web server, it is merely a proxy sitting in front of a web server such as Apache, this implies that you would still need to run Apache on an alternate port on your server (such as 8080) and varnish in turn connects to Apache on the backend and listens on port 80 in the front end.

We now need to configure Varnish to connect to Apache on port 8080. Type in the following:

cd /etc/varnish/
mv default.vcl default_bak.vcl
touch default.vcl #creating a new default config file
nano default.vcl

Paste the following code.

backend default {
.host = "<your server's IP address>";
.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;

Make sure you replace the <your server’s IP address> with the IP address of your server. Finally enter:

chkconfig varnish on

service varnish start

You can monitor the performance of your Varnish cache using the command line tool :


That is it,  you now have varnish running on your server.

If you have a Managed VPS or Dedicated Server with Host4Geeks, our technicians would be glad to set this up for you. Just write to us.


Leave a Reply