How to Install Varnish Cache for Apache on CentOS 7

What is Varnish Cache?

Varnish is an HTTP accelerator designed for content-heavy dynamic websites as well as APIs. In contrast to other web accelerators, such as Squid, which is a client-side caching proxy, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator.

Step 1: Add EPEL repository

Run the following command to add EPEL to your CentOS 7 system.

yum install -y epel-release

Step 2: Install Dependency packages

We need to install pygpgme & yum-utils if the repository is added via repo management tool.

yum install -y pygpgme yum-utils

Step 3: Add Varnish Cache repository

In this step, we’re adding Varnish Cache repository for CentOS 7. Copy the commands below and paste them on the terminal.

tee /etc/yum.repos.d/varnish60lts.repo<<EOF
[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
EOF

Step 4: Install Varnish on CentOS 7

We are going to install Varnish. Using this command.

yum install -y varnish

More information on installed package can be checked using.

rpm -qi varnish

Step 5: Start and enable Varnish on CentOS 7

Next, start and enable Varnish.

systemctl enable varnish
system ctl start varnish

Check the status of Varnish.

systemctl status varnish

Step 6: Configure Varnish Cache for Apache Web Server

First, install the Apache.

yum -y install httpd

Then we nees to set Listen port to 8080.

vi /etc/httpd/conf/httpd.conf

...
Listen 8080

Restart httpd service after the change.

systemctl restart httpd

Step 7: Configure Varnish Cache Server

Now edit Varnish Cache configuration file and set Listen port to 80.

vi /etc/systemd/system/multi-user.target.wants/varnish.service

Edit the line starting with ExecStart, and change from.

ExecStart=/usr/sbin/varnishd -a :**6081** -f /etc/varnish/default.vcl -s malloc,256m

To:

ExecStart=/usr/sbin/varnishd -a :**80** -f /etc/varnish/default.vcl -s malloc,256m

Then restart Varnish systemd service.

systemctl daemon-reload
systemctl restart varnish

Ensure that Apache is configured as a backend server for Varnish proxy.

vi /etc/varnish/default.vcl

Note port 8080 is configured on Apache web server.

Step 8: Test Varnish Cache on Apache

Finally, we test if Varnish cache is enabled and working with Apache service with the curl command below.

curl -I http://localhost

This gives you the HTTP header information. If you rerun the command, it will show that Varnish cached response.

curl -I http://localhost

Conclusion

We hope everyone understands how to install Varnish on CentOS. Varnish handles all inbound requests before they land to your web server backend: its cache serves all web traffic and, by default, refreshes every two minutes (or a different lifespan, if you decide so). Are you looking for cheap web hosting service providers? Then this is your last destination to end your search. We at TheStack provide premium WordPress hosting, hybrid smart server hosting, SEO dedicated server hosting, and managed virtual private servers at the best price. So, what are you waiting for? Give us a quote to work with us.

Related Articles