Varnish Apache Centos Varnish is an open source reverse HTTP proxy, an HTTP accelerator, and a useful tool for speeding up an Apache server. It can listen as your primary end-point and then serve the content of your back-end to the client and cache the content during response. Varnish can improve your normal website loading speed by tremendous amounts if you serve high traffic.

Install Varnish

If EPEL Repository is not present, you can do this by running the following command:

sudo yum install -y epel-release  

Once the repo is present, run:

sudo yum install -y varnish  

After running the above commands, if you want to enable Varnish to run at system startup then execute this:

sudo systemctl enable varnish  

To check the version of Varnish in your system:

sudo varnishd -V  

Configure Varnish

Open the Varnish configuration file at /etc/varnish/default.vcl. In this tutorial, we will run varnish on Port 80 (* standard HTTP port *):

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

Replace IP_ADDRESS with IP of Apache instance ( localhost if it's on the same machine ). We shall make Apache to serve on 8080 Port.

Install Apache

The following script will install apache and start it:

sudo yum install -y httpd  
service httpd start  

Now we have to make sure apache serves on 8080 port, by default it is set to serve at port 80. For this we will edit /etc/httpd/conf/httpd.conf file.
Find and replace Listen 80 with Listen 8080.
After the previous step, we need to restart Varnish and Apache to reflect the changes:

sudo apachectl restart  
sudo systemctl restart varnish  

Your instance should be ready to work now. You can check the deployment by navigating to http://localhost or using curl -I http://localhost, whichever you prefer. The browser navigation should show the apache default page whereas the curl should show the following:

HTTP/1.1 200 OK  
Date: Wed, 04 Nov 2015 10:21:07 GMT  
Server: Apache/2.4.6 (CentOS)  
Last-Modified: Fri, 02 Oct 2015 10:36:53 GMT  
ETag: "6c-5211cdbf61c14"  
Content-Length: 108  
Content-Type: text/html; charset=UTF-8  
X-Varnish: 32770  
Age: 0  
Via: 1.1 varnish-v4  
Connection: keep-alive  
view raw  

Hopefully, I covered all the steps of configuring and installing Varnish with Apache on CentOS. Let me know if you can add to this tutorial. With this post start the SysAd Chronicles at my blog. Thanks :)