Blog

Installing Varnish Cache to boost Apache’s Performance

Tags: Apache

Published on: August 31, 2019 by Sheldon Moor

Installing Varnish Cache to boost Apache’s Performance

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:

  • CentOS 7
  • A root user or user with sudo privileges

Step 1: Installing Apache Web Server

[root@server ~]# yum install httpd

Step 2: Once Apache is installed start it for time being and enable it to start automatically at system boot.

[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

Step 3: Enable the EPEL repository to install several dependency packages

yum install -y epel-release

Step 4: Install pygpgme, a package for handling GPG signatures and yum-utils, a collection of useful utilities that extend yum’s native features in various ways.

yum install pygpgme yum-utils

Step 5: Create a file named /etc/yum.repos.d/varnish5.repo and add the below configurations

[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:

Step 6: Update local yum cache

yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish5'

Step 7: Install Varnish Cache

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.

Step 8: Confirm Varnish installation by seeing the location of the Varnish executable and version installed on your system.

[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

Step 9 : Configuring Apache to Work With Varnish Cache

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

Step 10: Open the varnish environment configuration file (/etc/varnish/varnish.params) and find the parameter VARNISH_LISTEN_PORT which specifies the port Varnish listens on, and change its value from 6081 to 80.

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

Step 11: Setup Apache as a backend server for Varnish proxy

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";

}

Step 11: Restart httpd and Varnish cache to effect the above changes.

systemctl restart httpd

systemctl start varnish

systemctl enable varnish

systemctl status varnish

Step 12: Test Varnish Cache on Apache

[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

3rd party software installations

Category : Apache

Sheldon Moor

Sheldon Moor

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Get new updates

Categories

$0.000 items