How to Install Varnish to Run with LAMP on Centos (with Pictures)
Edit Article

  • 1 Editor

Nobody ever told us that LAMP with Varnish Cache 4 could be this amazing. The tutorial that we have made for you will guide you on how to install Varnish 4 and to configure it for generating a whole lot of traffic. This tutorial is just for installing Varnish Cache and does not explain what actually Varnish Cache is. But in short we will tell you this; the concept behind Varnish Cache technology is a very simple one. The main goal of Varnish Cache is to improve the performance for busy and dynamic websites. The way it does so is that it redirects traffic to the more static pages whenever possible. The main advantage of traffic redirection is that it reduces the number of dynamic page calls along with shedding a lot of CPU load. All this sounds cool doesn’t it? Well if you have a website or two that are generating a lot of traffic then you will know what we are talking about, all this should be attracting you by now. If you have already tried W3 Cache and or WP Super Cache and still you think that is not enough, then should give it a shot and you won’t be disappointed.


  1. A running and working CentOS sever. In the guide we are going to use CentOS 7 x86_x64. We advise you use minimal template if possible.
  2. If you are a Windows user then you can easily download Putty while those of you who are using Mac or Linux can simply just use Terminal.
  3. In addition to that you will also need a basic knowledge and skill to use Putty and to navigate through SSH.
  4. A cup of tea or coffee and 15 minutes of your time.


Follow all the steps that I mentioned earlier in my previous guide on how to install Apache, MariaDB and PHP on CentOS 7. You should already have a LAMP setup. The point of this whole exercise is to determine whether your server has the necessary software required to host a website. In this care, we are talking about: Apache, MariaDB, MySQL and PHP.



You need to login to your server using Putty or Terminal. Do not forget to login as root or a user which has sudo privileges.



After you have logged in as root, you need to add/install EPEL repository, This repository is needed to obtain Varnish package that is not available by default on CentOS 7. You need to firstly download the .rpm file into your server.



Once you have downloaded the .rpm file, you need to install it. Install the file using this command.

rpm -ivh epel-release-7-2.noarch.rpm



Installing Varnish Cache should be a piece of cake for you now. You can easily install Varnish Cache v4 by using Yum. Just enable the newly installed repoL

yum --enablerepo=epel install varnish –y


Once the process finishes for installing, you r screen should be somewhat like this:


To check which version is installed on the server just enter this simple command below:

varnishd –V

In the example that I have given you it is Varnish 4.0.1



By now you will have Varnish 4 installed on your server. You can now take your website to a whole new level by accelerating your website which you are hosting on your LAMP CentOS 7 VPS. Before you go ahead and run it for the first time you will have to do some minor configurations. Making these configurations will help Varnish Cache work with Apache web server. The varnish configuration file was previously located in:


But since we installed Varnish 4.0 this file directory has been changed.


You can use whatever editor you want to edit varnish.params file. I have used Nano editor in my case:

nano /etc/varnish/varnish.params

So, the part of the configuration file will look a bit like this:



We need to change the Listen Port to 80. We will be running Varnish in front of Apache:



You need to find VARNISH_STORAGE= line by scrolling down a bit down the page. If it is set to default, it will mean that Varnish will be using your server’s disk. The server’s disk will be used for storage purposes by Varnish. If you leave it as it is then you should confirm whether your VPS has a fast SSD disk. If instead you build a dedicated Varnish server to store all the big cached files then you can also leave it to default.


If you’re looking for a faster experience then using RAM-based caching should be preferred. It can easily be done by changing the file part with malloc. The example that we have given below allows varnish to use 1GB of RAM as cache storage:


This example lets it use 256GB of RAM:


Once you have finished editing the file, save the changes that you have made and exit the editor. The editor that we were using which was Nano closes by using Ctrl+O then Ctrl+X.


Once you’re done with that, you need to configure the default Varnish file. This file is located in /etc/varnish/. The VCL is of quite importance to Varnish as it tells varnish where the webserver content is so that it may look there for it. In this exceptional case it is to fetch the file from Apache by another port like 8080. You can use Nano editor to edit the file:

nano /etc/varnish/default.vcl

The default view that you see should look like this:


As it quite clear to that Varnish is already preconfigured to take content from the webserver which is running on port 8080.There is no need to change it. One thing you should notice here is that the other three sections (sub vcl_recv, sub vcl_backend_response and sub vcl_deliver) are left entry less. We can leave them the way they are for now. We will tell you why we did so later. You need to exit the editor now.


Now we will edit the configuration file for Apache webserver.

nano /etc/httpd/conf/httpd.conf

Once here, you should search for the file that says “Listen 80” and then you need to change it to


Once you’re done with it, it will look a bit like this:



If you have already made a virtual host, there is no need to worry. Just make sure you change the value of its listening port from 80 to 8080.



Once you’re done with this, restart Apache. The webserver at first will run on port 8080. This will leave port 80 unusable by Varnish:

systemctl restart httpd.service

Now you will start Varnish for the first time:

systemctl restart varnish.service


To enable Varnish to run every time on reboot you need to enter the following command. Remember, doing this is absolutely necessary:

systemctl enable varnish.service

That is it for the tutorial. You are now finished with installing Varnish. Give it a whirl maybe? Try it out by opening your web browser. Your website should now show that it is loaded via Varnish v4.


If you want to check it directly from the command line, you may do so. All you need to do is use a very simple command. The curl command, which will fetch its header response.

curl -I http://domain-or-ipaddress


Article Tools

Did this article help you?


an Author!

Write an Article