How to install Varnish Cache for Apache on CentOS 7

Estimated reading: 4 minutes 126 views

Introduction

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 – 1000x, depending on your architecture. This guide explains how to install Varnish Cache 6.0 with Apache on CentOS 7. If you need to disable SELinux, see our article: “How to Disable SELinux on CentOS
Prerequisites
  • A VPS/server running CentOS 7
  • Full root access

1. Configure Firewall

If you use FirewallD, modify the firewall rules to allow inbound traffic on port 80. If you are unsure of your firewall configuration, see our articles about FirewallD. These commands assume you have a freshly-deployed CentOS 7 instance:
$ firewall-cmd --zone=public --permanent --add-service=http
$ firewall-cmd --reload

2. Install Apache

Install Apache HTTP server.
yum install -y httpd
Set Apache port to 8080. Edit httpd.conf with vi.
vi /etc/httpd/conf/httpd.conf
Change the line “Listen 80” to “Listen 8080“, then save and close the file. The line should be like this when finished. Start the Apache service.
$ systemctl enable httpd.service
$ systemctl start httpd.service

3. Test Apache configuration

Create a test file.
$ touch /var/www/html/test.html
Use curl to test the server at port 8080. This verifies Apache is configured correctly.
$ curl -I http://localhost:8080/test.html

4. Install Varnish

Add the EPEL repository.
$ yum install -y epel-release
Install the dependency packages.
$ yum install -y pygpgme yum-utils
Add the Varnish Cache repository. Edit /etc/yum.repos.d/varnish60lts.repo
$ vi /etc/yum.repos.d/varnish60lts.repo
Paste the following, then save and close 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
Update the yum cache for the Varnish repo.
$ yum -q makecache -y --disablerepo='*' --enablerepo='varnish60lts'
Install Varnish.
$ yum install -y varnish
Verify Varnish is installed and the correct version.
$ sudo varnishd -V
varnishd (varnish-6.0.8 revision 97e54ada6ac578af332e52b44d2038bb4fa4cd4a)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2020 Varnish Software AS
Enable Varnish at system boot.
$ systemctl enable --now varnish
Configure Varnish to listen at port 80, from the default of 6081. Edit varnish.service with vi.
$ vi /usr/lib/systemd/system/varnish.service
Change the line beginning with ExecStart from port 6081 to port 80, then save and close the file. Restart the Varnish service.
$ systemctl daemon-reload
$ systemctl restart varnish

5. Test the Installation

Use curl to test from the server console.
$ curl -I http://localhost/test.html
The output should resemble this. The X-Varnish: 2 and Via: 1.1 varnish (Varnish/6.0) headers appear when Varnish Cache is running. Test from your local workstation, substitute your instance’s IP address. Verify the Varnish headers appear. Linux:
$ curl -I http://(your VPS's IP)/test.html
Windows PowerShell:
PS> curl -Uri http://(your VPS's IP)/test.html

Troubleshooting

Check ports
Use the ss utility to verify which processes are listening on which ports.
# ss -lnpt | grep 80
LISTEN    0    128    *:80           *:*       users:(("cache-main",pid=15058,fd=3),("varnishd",pid=15048,fd=3))
LISTEN    0    128    [::]:80       [::]:*     users:(("cache-main",pid=15058,fd=5),("varnishd",pid=15048,fd=5))
LISTEN    0    128    [::]:8080     [::]:*     users:(("httpd",pid=15280,fd=4),("httpd",pid=14057,fd=4),("httpd",pid=14056,fd=4),("httpd",pid=14055,fd=4),("httpd",pid=14054,fd=4),("httpd",pid=14053,fd=4),("httpd",pid=14052,fd=4))
Make sure varnishd is listening on port 80 and httpd is on port 8080 as shown.
Test with curl
$ curl -I http://localhost/test.html
If curl returns “HTTP/1.1 503 Backend fetch failed” as shown above, check the /etc/varnish/default.vcl file.
$ vi /etc/varnish/default.vcl
Make sure the backend default section points to Apache at port 8080.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

That’s all! Enjoy GreenCloudVPS services! 

Leave a Comment