Install Varnish Cache 5.2 to Boost Apache Performance on CentOS 7

Varnish Cache (commonly known as Varnish), is an open source, popular reverse-proxy HTTP accelerator intended for speeding up web servers. It is engineered for excessively utilized API endpoints and also for dynamic sites that serve massive-content and experience high-traffic.

For Nginx: How to Install Varnish Cache 5.2 for Nginx on CentOS 7

It basically helps to scale down CPU load; supports load balancing on web servers and enables a web browser to quickly load sites as a result of storing the cache in RAM. A number of big name companies use it including Facebook, Twitter and Wikipedia just to mention but a few.

Requirements

  1. A CentOS 7 with Apache installed
  2. A CentOS 7 with static IP address

In this article I will explain how to install and use Varnish Cache 5.2 as a front-end to an Apache web server in CentOS 7 (also works on RHEL7).

Step 1: Install Apache Web Server on CentOS 7

1. First install Apache HTTP server from the default CentOS software repositories using the YUM package manager as follows.

# yum install httpd
Install Apache on CentOS 7

Install Apache on CentOS 7

2. Once Apache installed, start it for the time being and enable it to start automatically at system boot.

# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd
Start and Enable Apache

Start and Enable Apache

3. Next update system firewall rules to permit inbound packets on port 80 using the commands below.

# firewall-cmd --zone=public --permanent --add-service=http
# firewall-cmd --reload
Open Apache Port on Firewall

Open Apache Port on Firewall

Step 2: Install Varnish Cache on CentOS 7

4. Now there are pre-compiled RPM packages for latest version of Varnish Cache 5 (i.e 5.2 at the time of writing), therefore you need to add official Varnish Cache repository.

Before that you need to enable the EPEL repository to install several dependency packages as shown.

# yum install -y epel-release

5. Next, install pygpgme, a package for handling GPG signatures and yum-utils, a collection of useful utilities that extend yum’s native features in various ways.

# yum install pygpgme yum-utils

6. Now create a file named /etc/yum.repos.d/varnishcache_varnish5.repo that contains the repository configuration below.

# vi /etc/yum.repos.d/varnishcache_varnish5.repo

Important: Make sure to replace el and 7 in the config below with your Linux distribution and version:

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

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

7. Now run the command below to update your local yum cache and install varnish cache 5 package (do not forget to accept the GPG key by typing y or yes while installing the package):

# yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish5'
# yum install varnish 
Install Varnish Cache

Install Varnish Cache

8. After installing Varnish Cache, the main executable will be installed as /usr/sbin/varnishd and varnish configuration files are located in /etc/varnish/:

  • /etc/varnish/varnish.params – this is the varnish environment configuration file.
  • /etc/varnish/default.vcl – this is the main varnish configuration file, it is written using vanish configuration language(VCL).
  • /etc/varnish/secret – varnish secret file.

9. You can confirm that the Varnish installation was successful by seeing the location of the Varnish executable and version installed on your system.

$ which varnishd
$ varnishd -V
Verify Varnish Cache Installation

Verify Varnish Cache Installation

Step 3: Configure Apache to Work With Varnish Cache

10. Now configure Apache to work in conjunction with Varnish Cache. By default Apache listens on port 80, you need change the default HTTPD port to 8080 – this will ensure that HTTPD runs behind Varnish caching.

You can use the sed command to change port 80 to 8080 as shown.

# sed -i "s/Listen 80/Listen 8080/" /etc/httpd/conf/httpd.conf

11. Next, open the varnish environment configuration file and find the parameter VARNISH_LISTEN_PORT which specifies the port Varnish listens on, and change its value from 6081 to 80.

# vi /etc/varnish/varnish.params
Configure Varnish Cache

Configure Varnish Cache

12. Next, setup Apache as a backend server for Varnish proxy, in the /etc/varnish/default.vcl configuration file.

# vi /etc/varnish/default.vcl 

Find the backend section, and define the host IP and port. Below is the default backend configuration, set this to point to your actual content server.

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

13. After performing all the necessary configurations, restart HTTPD and Varnish cache to effect the above changes.

# systemctl restart httpd
# systemctl start varnish
# systemctl enable varnish
# systemctl status varnish

Step 4: Test Varnish Cache on Apache

14. Lastly, test if Varnish is enabled and working working with the HTTPD service using the cURL command below can be used to view the HTTP header.

# curl -I http://localhost
Test Varnish Cache on Apache

Test Varnish Cache on Apache

For more information, check out Varnish Cache Github Repository: https://github.com/varnishcache/varnish-cache

In this tutorial, we explained how to setup Varnish Cache 5.2 proxy for Apache HTTP server on CentOS 7. In case you have any queries or additional ideas to share, use the feedback form below to write back to us.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

5 Responses

  1. KuWIn says:

    It working, thanks for you

  2. Bibin says:

    Hello,

    I have installed varnish and have 4 domains with 4 dedicated iP,
    How to add it to varnish ?

  3. Webtipster says:

    So after you follow these steps and don’t like it, how do you remove it?

    • Aaron Kili says:

      @Webtipster

      Removing packages installed from source is a big challenge, you have to possibly remove all the individual files installed. However, the good new is that we have an updated version of this article that we will publish soon , where installation is done using the package manager. It uses the new Varnish repository and you can easily remove it in case you do not want it any more.

Leave a Reply to Aaron Kili Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.