How to Install Varnish on CentOS 7

Install Varnish on CentOS 7

In this article, we will have explained the necessary steps to install Varnish on CentOS 7. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Varnish Cache (also referred to as Varnish) is a proxy and cache, or HTTP accelerator, designed to improve performance for busy, dynamic web sites. By redirecting traffic to static pages, varnish reduces the number of dynamic page calls, thus reducing load. Varnish is designed for content-heavy dynamic web sites as well as heavily consumed APIs.

Install Varnish on CentOS

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo yum install epel-release
sudo yum update

Step 2. Install Varnish.

Now install Varnish from the EPEL repository using yum command below:

yum install varnish

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

sudo systemctl start varnish
sudo systemctl enable varnis

To check the status of Varnish, run the following command:

sudo systemctl status varnish

Check the version of Varnish that is running:

sudo varnishd -V

Step 3. Configure Varnish.

Setup Varnish 5 for Apache:

nano /etc/varnish/varnish.params

Change VARNISH_LISTEN_PORT from 6081 to 80:

VARNISH_LISTEN_PORT=80

Now edit Apache configuration file:

nano /etc/httpd/conf/httpd.conf

Search for Listen 80 and replace it with Listen 8080:

Listen 8080

Save and close the file, then restart Apache and Varnish to reflect the changes:

systemctl restart httpd.service
systemctl restart varnish.service

Step 4. Test Varnish.

Now you should have Varnish and Apache running together. To verify that Varnish is on and working, you can use the curl command to view the HTTP header:

curl -I http://localhost

You should see the output something like this:

HTTP/1.1 200 OK
Date: Wed, 12 Feb 2019 10:46:07 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 16 Dec 2019 10:36:53 GMT
ETag: "6c-intanramona61c14"
Content-Length: 108
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive

Congratulation, you have learned how to install Varnish cache on CentOS 7. If you have any question, please leave a comment below.