How to Install Varnish Cache 5.2 for Nginx on CentOS 7

Varnish Cache (also referred to as Varnish) is an open source, high-performance HTTP accelerator designed for speeding up web servers. In our last article, we’ve explained how to setup Varnish Cache for Apache web server on CentOS 7.

In this article, we will show you how to install and use Varnish Cache 5.2 as a front-end to Nginx HTTP server in CentOS 7. This guide should also work on RHEL7.

Requirements

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

Step 1: Install Nginx Web Server on CentOS 7

1. Start by installing Nginx HTTP server from the default CentOS software repositories using the YUM package manager as follows.

# yum install nginx
Install Nginx on CentOS 7

Install Nginx on CentOS 7

2. When the installation completes, start Nginx service for now and enable it to start automatically at system boot.

# systemctl start nginx
# systemctl enable nginx
# systemctl status nginx
Start and Enable Nginx at Boot

Start and Enable Nginx at Boot

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

# firewall-cmd --zone=public --permanent --add-port=80/tcp
# firewall-cmd --reload
# firewall-cmd --zone=public --permanent --add-port=8080/tcp
# firewall-cmd --reload
Open Nginx Port on Firewall

Open Nginx 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 Nginx to Work With Varnish Cache

10. In this step, you need to configure Nginx to work with Varnish Cache. By default Nginx listens on port 80, you should change the default Nginx port to 8080 so it runs behind Varnish caching.

Open the Nginx config file /etc/nginx/nginx.conf and find the line listen 80 and change it to listen 8080 as in the server block shown in the screen shot below.

# vi /etc/nginx/nginx.conf

Configure Nginx Listening Port

Configure Nginx Listening Port


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 Nginx 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 Nginx HTTPD and Varnish cache to effect the above changes.

# systemctl restart nginx
# systemctl start varnish
# systemctl enable varnish
# systemctl status varnish

Step 4: Test Varnish Cache on Nginx

14. Finally, test if Varnish cache is enabled and working with the Nginx service using the cURL command below to view the HTTP header.

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

Test Varnish Cache on Nginx

You can find additional information from the Varnish Cache Github Repository: https://github.com/varnishcache/varnish-cache

In this tutorial, we explained how to setup Varnish Cache 5.2 for Nginx HTTP server on CentOS 7. Use the comment form below to send us any queries or extra ideas.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

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

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

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...

8 Responses

  1. Gayatri says:

    Can you tell me where is the default.vcl located after installation?

  2. TEst says:

    How do I delete it. I want to remove

    • Aaron Kili says:

      Removing a package installed from source is a little of a challenge, because the system package manager normally doesn’t recognize it(as in keeping track of all its files and documentation etc..).

      But you can try commands like:
      $sudo make uninstall #if you still have the compilation directory

      Or use find together with rm command(to locate and remove all package files), see the receptive man pages for more information.

  3. Việt Hùng says:

    Hello. Please help me.

    [[email protected] varnish-5.1.2]# /usr/local/sbin/varnishd -a :80 -b https://chiasewp.com:8080
    Error: Could not get socket :80: Address already in use
    (-? gives usage)

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.