How to Install Varnish Cache 4 on CentOS 7

How to Install Varnish Cache 4 on CentOS 7

John Hewick

Varnish is a great tool for improving a poor performing Apache server. Acting as an open source reverse HTTP, it also doubles as an accelerator. Not only does it speed up loading times on your site but prevents a server overload in those times of high traffic. Through load balancing, Varnish redirects traffic to static pages where applicable and decreases the number of dynamic page calls.

Thankfully, this useful application is conveniently easy to set up. Follow this simple install tutorial if you’re wanting to reap the benefits of utilising Varnish for an Apache web server. This guide is only for those installing on CentOS7.

EPEL repository

The first stage before installing Varnish is to add the EPEL repository. To do this, simply add the following command:

sudo yum install -y epel-release

After this installation is finished, you’ll be ready to install Varnish. It’s as easy as entering the following command:

sudo yum install -y varnish

Once this is completed, it’s essential to enable Varnish to start on boot:

sudo systemctl start varnish

sudo systemctl enable varnish

If you wish to check the status of Varnish, run this command:

sudo systemctl status varnish

Finally, to check that the correct version of Varnish is running, enter:

sudo varnishd -V

Configure settings

To ensure that Varnish works in the front end, you’ll have to make some changes to configuration. To do this, find the configuration file located in the /etc/varnish directory in CentOS 7.

Firstly, the default port setting should be changed from ‘port 6081’ to ‘80’ which allows to the website to request access from the Varnish cache. This can be changed in the ‘varnish.params’ config file:

sudo nano /etc/varnish/varnish.params

Editing ‘VARNISH_LISTEN_PORT from 6081 to 80:

VARNISH_LISTEN_PORT=80

From here, save the file before opening the default.vcl file. This allows Varnish to look for the server content:

sudo nano /etc/varnish/default.vcl

Finally, give Varnish the command to access content from port 8080:

backend default {

.host = “127.0.0.1”;

.port = “8080”;

}

Configuring Apache

In order to make sure Apache works with Varnish, you’ll need to change the port setting from 80 to 8080. This can be done by editing the ‘httpd.conf’ config file.

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

Enter the search command ‘Listen 80’ and replace it with ‘Listen 8080’.

Now you can save and close this file before restarting Apache and Varnish to see those changes:

sudo apachectl restart

sudo systemctl restart varnish

Test Varnish

Now everything is fully set up all that’s left to do is check that Varnish and Apache are running together. Use the ‘curl’ command in order to view the HTTP header by inputting:

curl -I http://localhost

The output should look similar to the following which means your set up has been successful:

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

John Hewick

Author John Hewick

More posts by John Hewick