Monday, October 3, 2022

Install and Configure Varnish Cache For Apache on Centos 7

In this article, we want to teach you how to Install and Configure Varnish Cache For Apache on Centos 7.

Varnish is a reverse proxy that makes a copy of the page in the memory to serve it out when the page will be re-visited. The copy will be made based on request headers like URL and Request Method. This is known as Full Page Cache.

Install and Configure Varnish Cache For Apache on Centos 7

To install Varnish Cache on Centos 7, you need some requirements.

Requirements

You need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article about the Initial Server Setup with Centos 7.

Set up a basic firewall. you can use our article about the Set Up a Firewall with FirewallD on CentOS 7.

And you need to have Apache installed on your server. To do this, you can follow our article about How To Install Apache on Centos 7.

This guide uses CentOS 7 without SELinux. You can check our article about How To Disable SELinux on Centos.

When you are done, follow these steps to install Varnish Cache on Centos 7.

Apache configuration on Centos 7

By default, Apache listens on port 80. You need to change it to 8080.

First, open the Apache configuration file with your favorite text editor, here we use vi:

$ sudo vi /etc/httpd/conf/httpd.conf

Search for the Listen line and change it to:

Listen 8080

When you are finished save and close the file.

Now restart Apache to apply the changes:

sudo systemctl restart httpd.service

Then, create a test file with the following command:

sudo touch /var/www/html/test.html

At this point, use the curl tool to test the server at port 8080. This verifies Apache is configured correctly:

curl -I http://localhost:8080/test.html

In your output you will see:

Output
HTTP/1.1 200 OK
Date: Thu, 11 Nov 2021 09:26:05 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 11 Nov 2021 09:25:57 GMT
ETag: "0-5d07fee188311"
Accept-Ranges: bytes
Content-Type: text/html; charset=UTF-8

Now you can start to install Varnish on Centos 7.

How To Install and Configure Varnish on Centos 7

You need to add the Epel repository to your server. To do this, run the following command:

sudo yum install -y epel-release

Now you need to install the dependency packages with the following command:

sudo yum install -y pygpgme yum-utils

Then, you should add the Varnish Cache repository. Create and open the file with your favorite text editor:

sudo vi /etc/yum.repos.d/varnish60lts.repo

Add the following content to the file:

[varnish60lts]
name=varnishcache_varnish60lts
baseurl=https://packagecloud.io/varnishcache/varnish60lts/el/7/x86_64
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish60lts/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300

When you are done, save and close the file.

Here you need to update the yum cache for the Varnish repo with the following command:

sudo yum -q makecache -y --disablerepo='*' --enablerepo='varnish60lts'

Now install Varnish on Centos 7 with the following command:

sudo yum install -y varnish

Verify your installation with the command below:

sudo varnishd -V
Output
varnishd (varnish-6.0.8 revision 97e54ada6ac578af332e52b44d2038bb4fa4cd4a)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2020 Varnish Software AS

Enable Varnish to start at boot:

sudo systemctl enable --now varnish

Now you need to configure Varnish Cache to listen at port 80. Open the Varnish configuration file:

sudo vi /usr/lib/systemd/system/varnish.service

Find the ExecStart line and change it from port 6081 to port 80:

ExecStart=/usr/sbin/varnishd \
-a :80 \

When you are done save and close the file.

Then, restart the Varnish Cache on Centos 7 with the following command:

$ sudo systemctl daemon-reload
$ sudo systemctl restart varnish

Now you can test your installation.

Test Varnish Installation on Centos 7

From your console run the following command:

curl -I http://localhost/test.html

In your output you should see:

Output
HTTP/1.1 200 OK
Date: Thu, 11 Nov 2021 09:48:05 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 11 Nov 2021 09:25:57 GMT
ETag: "0-5d07fee188311"
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

This means that your Varnish Cashe is active and running on your server.

You can test it from your local workstation by substituting your instance’s IP address.

Linux:

curl -I http://192.0.2.123/test.html

Windows PowerShell:

PS> curl -Uri http://192.0.2.123/test.html

Note: Verify the Varnish headers will appear.

Conclusion

At this point, you learn to Install and Configure Varnish on Centos 7.

Hope you enjoy using it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles