Hamsa K
8 min read | 1 month ago

How to Install Varnish with Apache on Centos 7

Install Varnish cache with Apache on Centos 7

Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as APIs. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator.This Article guides you on how to install Varnish Cache 6.0 with Apache on CentOS 7 server.

update the system with the latest packages and security patches and also install epel repository to install dependencies.

sudo yum update 
sudo yum install epel-release

Step 1: install Apache

sudo yum install httpd

once apache is installed set Apache port to 8080 by editing httpd.conf file

sudo vi /etc/httpd/conf/httpd.conf

change the line "Listen 80" as "Listen 8080" and save the file. 

Note: if you have configured vhosts for apache, then you need to change the port as 8080 on your virtual host configuration for each website that you want to serve via Varnish.

once it is done, you can start and enable apache service with below commands.

sudo systemctl start httpd
sudo systemctl enable httpd

Also Read -> How to Migrate from Centos 8 to Centos Stream

Step 2: Install varnish

Now install Varnish high-performance HTTP accelerator and it is available in epel repository. you can run following command to install it.

sudo yum install varnish

After varnish is installed, you can start and enable it to start on boot.

sudo systemctl start varnish
sudo systemctl enable varnish

and also check varnish service and version with following commands.

sudo systemctl status varnish
sudo varnishd -V

Also Read -> How to Install Apache CouchDB on Centos 8

Step 3: configure varnish

By default Varnish listens on port 6081. you need to change port 6081 to 80 so that website requests access the Varnish cache first.To do this we wil edit below varnish.params config file.

sudo vi /etc/varnish/varnish.params

change varnish listen port from 6081 to 80


save and exit the file.

now we will configure it as a reverse proxy for the Apache web server.For that edit the default configuration in default.vcl and this file tells varnish to look for the server content.

sudo vi /etc/varnish/default.vcl

you can find the default backend section like below.

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

If your backend server is running on a different server then change host parameter should point to that server ip address and apache is running on port 8080. Now save and exit the file.

Then restart varnish service to reflect changes as below.

systemctl restart varnish

Step 4: configure firewall

if firewall service is running on your system, then open port for http and https requests with following commands.

firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload

Also Read -> How to Configure NIC bonding on Centos 7

Step 5: Test varnish

To verify varnish is working, you can use the curl command to view http header.

curl -I http://localhost

sample output:

HTTP/1.1 200 OK
Date: Sat, 13 Mar 2021 09:26:07 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
ETag: "1321-5058a1e728280"
Accept-Ranges: bytes
Content-Length: 4897
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive

Its showing as web-page is indeed sent using Varnish. if we don't have any webpage then we will receive HTTP/1.1 403 Forbidden.

That's it.Now we have learned how to install varnish cache server on centos 7.

Also Read -> How to Install and Configure Redis Server on Centos 7

Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy