Configure Varnish With Apache On CentOS 7

Date published: 01/05/2019

Introduction

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. Most certainly, you will install it in front of any server that speaks HTTP and configure it to cache the contents.

Varnish Cache is really, very fast and handles request at high speed. It typically speeds up delivery with a factor of 300 – 1000x, depending on your architecture.

prerequisite

  • However, we need server running CentOS 7 with Apache installed with any version.

Procedure for installation of Varnish

Before installing Varnish in your server, you will have to install the EPEL repository.

sudo yum install -y epel-release

For instance, install varnish in server

 yum install -y varnish

After installation, you will need to start Varnish and enable it to start on boot.

sudo systemctl start varnish
sudo systemctl enable varnish

Moreover, check the status of Varnish.

sudo systemctl status varnish

Likewise, check the version of Varnish

sudo varnishd -V

Configuration of Varnish setup

The default varnish configuration file will be located in the /etc/varnish directory in CentOS 7.

To make Varnish work in front of Apache, you will need to set up some basic configurations like given details below.

By default Varnish listens on port 6081. Use network tools such as netstat for confirmation.

You will need to change port 6081 to 80 so that website requests access the Varnish cache first. Similarly, you can do this by editing the varnish.params config file.

sudo nano /etc/varnish/varnish.params

Change VARNISH_LISTEN_PORT from 6081 to 80:

VARNISH_LISTEN_PORT=80

Save the file, then open the default.vcl file. This file tells varnish to look for the server content:

sudo nano /etc/varnish/default.vcl

All the above, update the Varnish to get the content on port 8080.

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

Configure Apache [backend] To Work With Varnish

By default Apache listens on port 80.

Similarly in the vein, you need to make Apache to run behind Varnish caching by changing the default Apache port to 8080.

To do this, edit the httpd.conf config file of Apache:

sudo nano /etc/httpd/conf/httpd.conf

Search for Listen 80 and replace it with Listen 8080:

Listen 8080

Save and close the file, restart Apache and Varnish to reflect the changes in port.

sudo apachectl restart
sudo systemctl restart varnish

Test the web severs

Now we have successfully configured Varnish and Apache running together.

To verify that Varnish is on and working, you can use the curlcommand to view the HTTP header:

curl -I http://localhost

You should see the output something like this:

HTTP/1.1 200 OK
Date: Wed, 01 May 2019 09:49:37 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 18 Apr 2019 11:39:13 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

That’s it

Thanks for using pheonix solutions.

You find this tutorial helpful? Share with your friends to keep it alive.

Leave a Reply