Installing the Varnish Caching Engine

Varnish is a rather powerful web caching server that sits between your site’s visitors and Apache, the actual web server. However, as you will be making modifications to the cPanel’s configuration of the server, it’s recommended that this be performed by one with a little Linux administration experience. I have, however, attempted to make this guide as easy as possible to follow.

Please note that, during this installation, your site will be unavailable for a short while. This is because we must change the port Apache listens on before configuring Varnish. As such, you may want to work on this during a time when your site isn’t busy.

Important: For sections where commands are given to run that have a blue border along the top of it, you must copy everything in the box. These are multi-line commands, and will always have a \ at the end, except for the last line.

Now, let’s get down to business. We need to start off by signing in to WHM as root. Once you’re signed in to WHM, find the search bar above the menu on the left and enter “Tweak” and click “Tweak Settings.”

Tweak Settings

On the top right side of the page, enter “non-SSL” into the search box. This will leave you with two results. The second one (Apache non-SSL IP/Port) is the one we’re interested in. Select the option next to the text box, and change it to the following.

Search for “non-SSL”

Change port to 8080

Now that we have Apache listening on a different port, we can proceed. Now we need to SSH into the server as root. (For information on SSH, please see the links at the bottom of this post.)

For the purpose of this tutorial, I am going to assume you’ve already established an SSH connection to your server. Now, to ensure we install the correct version for our server, we need to figure out which version of CentOS we have. To find the version you have, run the following command.

 cat /etc/redhat-release

Once you run this, you will see something similar to what’s below. I’m including the two most likely to occur.

 [root@server ~]# cat /etc/redhat-release
CentOS release 5.9 (Final)

 [root@server ~]# cat /etc/redhat-release
CentOS release 6.3 (Final)

The numbers in bold are what’s important. Now that you know the CentOS version you have, we can proceed with the installation. Copy the command below that correlates to your version of CentOS and paste it into your SSH session. This will install the Varnish repository. (The –nosignature is required as Varnish’s public key is not yet included in CentOS’ keyring).

CentOS 5

 rpm --nosignature -i\

CentOS 6

 rpm --nosignature -i\

Once you have the appropriate repository installed, it’s time to install Varnish itself. Execute the following command in your SSH window:

 yum install varnish

Yum will now look at what is required by Varnish and what you currently have. If you are missing anything Varnish requires (a dependency), yum will offer to install them as well, provided it knows where to get it. For this situation, it will know.

You will be asked to confirm if the change is ok. Press ‘y’ then Enter, and watch as it performs the installation.

Click to enlarge

Once yum completes the installation, we need to tell Varnish what port to use. To do that, we must edit /etc/sysconfig/varnish. You can either use your favorite editor (nano, vim, etc) to do this, or you can use the command below, which will do it for you. It will backup the original configuration and change the port. (If you wish to do this manually, find the line “VARNISH_LISTEN_PORT=6081” and change the 6081 to 80)

 cp /etc/sysconfig/varnish /etc/sysconfig/varnish.orig && \
sed -i 's/6081/80/' /etc/sysconfig/varnish

If you would like to confirm that this did indeed change the port, feel free to use the following command.

 egrep "^VARNISH_LISTEN" /etc/sysconfig/varnish

 [root@server ~]# egrep "^VARNISH_LISTEN" /etc/sysconfig/varnish

For this next part, you will need your server’s IP address. If you don’t know it, here is a command you can run through SSH to get it.

 curl 2>/dev/null | grep "IP Address" | \
sed 's/<[^>]*>//g' |cut -d':' -f2

Here is where we tell Varnish how to find your websites. We need to edit /etc/varnish/default.vcl so it has the correct IP and port. The setting we need to change is around line 5 of the default.vcl file. You will need to make it look like this using the IP address you obtained in the previous step: (for demonstration purposes, my IP address is

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

And, just as before, here is a command you can run to make the change for you, as well as verify the change. Be sure to replace with the IP you got earlier.

 MYIP="" && \
cp /etc/varnish/default.vcl /etc/varnish/default.vcl.orig && \
sed -i "s/127\.0\.0\.1/$MYIP/" /etc/varnish/default.vcl && \
grep ".host =" /etc/varnish/default.vcl

You should see this once the change is complete:

 [root@server ~]# MYIP="" && \
> cp /etc/varnish/default.vcl /etc/varnish/default.vcl.orig && \
> sed -i "s/127\.0\.0\.1/$MYIP/" /etc/varnish/default.vcl && \
> grep ".host =" /etc/varnish/default.vcl

.host = “”;

Almost done! Now it’s time to start Varnish, and make sure it starts when the server boots up.

 chkconfig varnish on && service varnish start

 [root@server ~]# chkconfig varnish on && service varnish start
Starting varnish HTTP accelerator:          [  OK  ]

That’s it! You’ve now enabled Varnish. You can run the varnishstat command to see the status of Varnish’s cache. If you open your site, you should see it as if there was nothing changed.

In its default configuration, Varnish will not cache very many things. It can take quite a bit of tweaking to get it exactly right for your website(s). I recommend you read the Varnish documentation here, as it explains, in great detail, how to fine tune Varnish.

Related Articles: SSH on Windows with Putty | SSH on Linux with Terminal

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , | Posted under cPanel Tutorials, cPanel/Linux Hosting, Customization, Linux Server Admin | RSS 2.0

Leave a Reply

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