How To Install Varnish Cache 4.0 In CentOS 7

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. A high level overview of what Varnish does can be seen in the video attached to this web page.

Steps to install Varnish on CentOS

  • My Centos Server: CentOS Linux release 7.1.1503 (Core) 
  • IP: 192.168.136.131

1. Execute the following command to varnish repo for Centos 7

vi /etc/yum.repos.d/varnish.repo
[varnish-4.0]
 name=Varnish 4.0 for Enterprise Linux
 baseurl=https://repo.varnish-cache.org/redhat/varnish-4.0/el7/$basearch
 enabled=1
 gpgcheck=0
 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-VARNISH
 [root@localhost varnish]#

2. Install EPEL Repo for CentOS 7 

3. Then install varnish using yum command

yum update
[root@localhost ~]# yum install varnish
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.denit.net
 * epel: ftp.fau.de
 * extras: centos.mirror.transip.nl
 * updates: mirrors.supportex.net
Resolving Dependencies
--> Running transaction check
---> Package varnish.x86_64 0:4.0.3-3.el7 will be installed
--> Processing Dependency: varnish-libs(x86-64) = 4.0.3-3.el7 for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1(LIBVARNISHAPI_1.3)(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1(LIBVARNISHAPI_1.2)(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1(LIBVARNISHAPI_1.1)(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1(LIBVARNISHAPI_1.0)(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: jemalloc for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libvgz.so()(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libvcc.so()(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libvarnishcompat.so()(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libvarnishapi.so.1()(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libvarnish.so()(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Processing Dependency: libjemalloc.so.1()(64bit) for package: varnish-4.0.3-3.el7.x86_64
--> Running transaction check
---> Package jemalloc.x86_64 0:3.6.0-1.el7 will be installed
---> Package varnish-libs.x86_64 0:4.0.3-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================
 Package                                               Arch                                            Version
=======================================================================================================================
Installing:
 varnish                                               x86_64                                          4.0.3-3.el7
Installing for dependencies:
 jemalloc                                              x86_64                                          3.6.0-1.el7
 varnish-libs                                          x86_64                                          4.0.3-3.el7

Transaction Summary
=======================================================================================================================
Install  1 Package (+2 Dependent packages)

4. Edit the Varnish configuration under /etc/varnish 

Edit varnish

vi  varnish.params
# Default address and port to bind to. Blank address means all IPv4
# and IPv6 interfaces, otherwise specify a host name, an IPv4 dotted
# quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=192.168.1.5
VARNISH_LISTEN_PORT=80

# Admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=192.168.136.131
VARNISH_ADMIN_LISTEN_PORT=6082

and Edit default.vcl

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "192.168.136.131";
    .port = "8080";
}

Edit   apache  configuration  and  change    listen port  to  8080

 vi  /etc/httpd/conf/httpd.conf
Listen 8080

5. Edit firewall to allow the http service

[root@localhost varnish]# systemctl stop firewalld
[root@localhost varnish]# systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: inactive (dead) since Mon 2015-06-01 15:32:19 PDT; 53min ago
 Main PID: 758 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/firewalld.service

Jun 01 13:38:39 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Jun 01 15:32:18 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
Jun 01 15:32:19 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost varnish]#

if you want to keep the firewall running:

# firewall-cmd --permanent --zone=internal --add-service=http
success
# firewall-cmd --reload
success

 6. Enable and start Varnish and Apache daemons

systemctl enable varnish
systemctl enable httpd
systemctl start varnish
systemctl start httpd

7. check if apache running

http://ip  http://hosnmae

8. Now check if you caching system working  

varnish

Or check from the command line as shown below.

[root@localhost varnish]# curl -I 192.168.136.131
 HTTP/1.1 403 Forbidden
 Date: Mon, 01 Jun 2015 23:03:10 GMT
 Server: Apache/2.4.6 (CentOS) PHP/5.4.16
 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: 32779
 Age: 0
 Via: 1.1 varnish-v4
 Connection: keep-alive

 That’s it.