Published on: August 31, 2019 by Sheldon Moor
Scenario:
Varnish Cache is an open-source, elite HTTP accelerator agent. It stores the cache in memory ensuring that web server resources are not wasted in creating the same web page over and over again when requested by a client. Here we are going to look at how a varnish cache can be installed and configured to act as a front-end to Apache HTTP server.
Prerequisites:
[root@server ~]# yum install httpd
[root@server ~]# systemctl start httpd [root@server ~]# systemctl enable httpd [root@server ~]# systemctl status httpd
httpd.service – The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2019-03-01 07:22:15 EST; 1h 15min ago
yum install -y epel-release
yum install pygpgme yum-utils
[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
Make sure to replace el and 7 in the config below with your Linux distribution and version:
yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish5'
yum install varnish
varnish configuration files are located in /etc/varnish/ [root@server ~]# cd /etc/varnish/ [root@server varnish]# ll total 12 -rw-r--r-- 1 root root 1231 Mar 1 07:20 default.vcl -rw------- 1 root root 37 Mar 1 07:12 secret -rw-r--r-- 1 root root 1347 Mar 1 07:18 varnish.params
– /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 varnish configuration language(VCL).
– /etc/varnish/secret – This is varnish secret file.
[root@server varnish]# which varnishd /usr/sbin/varnishd [root@server varnish]# varnishd -V varnishd (varnish-5.2.1 revision 67e562482) Copyright (c) 2006 Verdens Gang AS Copyright (c) 2006-2015 Varnish Software AS
Apache usually listens to port 80 by default. In order to ensure that Apache runs behind varnish, change the default httpd port to 8080. This can be done easily by using the sed command.
sed -i "s/Listen 80/Listen 8080/" /etc/httpd/conf/httpd.conf
vi /etc/varnish/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
Edit the main varnish configuration file (/etc/varnish/default.vcl). By default it is shown below.
Below is the default backend configuration, set this to point to your actual content server.
backend default { .host = "127.0.0.1"; .port = "8080"; }
Set this to point to your content server.
backend default { .host = "136.243.201.227"; .port = "8080"; }
systemctl restart httpd systemctl start varnish systemctl enable varnish systemctl status varnish
[root@server ~]# curl -I server.training5.com
Date: Fri, 01 Mar 2019 14:28:42 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: 32776
Age: 0
Via: 1.1 varnish (Varnish/5.2)
Connection: keep-alive
Category : Apache
Add new commentSIGN IN