How to Install Varnish Using Apache Benchmark in Linux

Installation of Varnish Using Apache Benchmark in Linux

Varnish is a high performance HTTP accelerator and reverse proxy. The important thing to be noted is that Varnish will save the cache in memory and not in the Disk. This article explains how to install and use Varnish as a front-end to Apache or Nginx in order to cache HTTP responses quickly and without burdening the web server.

Installation of Varnish

At first install the LAMP or LEMP server before proceding with the Varnish.

For step by step installation of LAMP visit,
https://www.linuxhelp.com/install-lamp-fedora-22/

Run the following command to install Varnish from its own repository or install epel-repository and then proceed with the installation.

[root@linuxhelp1 ~]# yum install varnish
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Resolving Dependencies
-->  Running transaction check
--->  Package varnish.x86_64 0:2.1.5-5.el6 will be installed
-->  Processing Dependency: varnish-libs = 2.1.5-5.el6 for package: varnish-2.1.5-5.el6.x86_64
-->  Processing Dependency: jemalloc for package: varnish-2.1.5-5.el6.x86_64
-->  Processing Dependency: libvcl.so.1()(64bit) for package: varnish-2.1.5-5.el6.x86_64
-->  Processing Dependency: libvarnishcompat.so.1()(64bit) for package: varnish-2.1.5-5.el6.x86_64
-->  Processing Dependency: libvarnishapi.so.1()(64bit) for package: varnish-2.1.5-5.el6.x86_64
-->  Processing Dependency: libvarnish.so.1()(64bit) for package: varnish-2.1.5-5.el6.x86_64
-->  Processing Dependency: libjemalloc.so.1()(64bit) for package: varnish-2.1.5-5.el6.x86_64
-->  Running transaction check
--->  Package jemalloc.x86_64 0:3.6.0-1.el6 will be installed
--->  Package varnish-libs.x86_64 0:2.1.5-5.el6 will be installed
.
.
.     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : varnish-libs-2.1.5-5.el6.x86_64                                                                                                           1/3 
  Installing : jemalloc-3.6.0-1.el6.x86_64                                                                                                               2/3 
  Installing : varnish-2.1.5-5.el6.x86_64                                                                                                                3/3 
  Verifying  : jemalloc-3.6.0-1.el6.x86_64                                                                                                               1/3 
  Verifying  : varnish-2.1.5-5.el6.x86_64                                                                                                                2/3 
  Verifying  : varnish-libs-2.1.5-5.el6.x86_64                                                                                                           3/3 

Installed:
  varnish.x86_64 0:2.1.5-5.el6                                                                                                                               

Dependency Installed:
  jemalloc.x86_64 0:3.6.0-1.el6                                               varnish-libs.x86_64 0:2.1.5-5.el6                                              

Complete!

Start Varnish and httpd manually.

[root@linuxhelp1 ~]# service httpd start
Starting httpd:                                            [  OK  ]
[root@linuxhelp1 ~]# service varnish start
Starting varnish HTTP accelerator:                         [  OK  ] 

To Configure Varnish

Apache will listen to 80 port by default, change it to 8080 in the apache main config file in /etc/httpd/conf/httpd.conf

#Listen 12.34.56.78:80
Listen 8080

Next change the port for varnish in the configuration file located in /etc/sysconfig/

VARNISH_LISTEN_PORT=80

Set the backend listening port for varnish in default.vcl file located in /etc/varnish/

backend default {
  .host = " 127.0.0.1"  
  .port = " 8080"  
}

Once the configuration is done restart the httpd and varnish services.

[root@linuxhelp1 ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[root@linuxhelp1 ~]# service varnish restart
Stopping varnish HTTP accelerator:                         [  OK  ]
Starting varnish HTTP accelerator:                         [  OK  ] 

To Test Varnish

Check whether the requests are cached by varnish using curl command.

[root@linuxhelp1 ~]# curl -I http://192.168.5.182/
HTTP/1.1 403 Forbidden
Server: Apache/2.2.15 (CentOS)
Content-Type: text/html  charset=UTF-8
Content-Length: 4961
Date: Thu, 23 Jun 2016 10:46:57 GMT
X-Varnish: 264906919
Age: 0
Via: 1.1 varnish
Connection: keep-alive

When you retry the same command, you can see the X-varnish content increases due to cached.

To view the Varnish logs run the following command

[root@linuxhelp1 ~]# varnishlog

Run the following command to display Varnish cache statistics,

[root@linuxhelp1 ~]# varnishstat

Check the varnish again by benchmarking with the ab tool.
Apache benchmarking tool will be installed by default during apache installation.

Run the following command to benchmark the request and response that hits the apache web server.

[root@linuxhelp1 ~]# ab -n 10000 -c 100 http://192.168.5.182/


Here, n is for number of requests to be performed and &ndash c is for concurrency.

When the number of requests increased, it may increase the server load since it’ s not cached. So after the cache is generated in Varnish server, the load will be back to normal.

Tag : Apache Varnish
FAQ
Q
How to restart the "varnish" service in Linux?
A
You can use the following command to restart the "Varnish" service on the Linux. For syntax: "systemctl restart varnish" (or) "service varnish restart".
Q
How to configure Apache to run on IP address 0.0.0.0 with port 80?
A
First you have to open a " /etc/apache2/ports.conf" configuration file.
Where you can add the line into the configuration file, as "Listen 192.168.1.6:80"
Q
Can you give me the official link for varnish?
A
Here I have give the official link for "Varnish", "https://varnish-cache.org/"
Q
How to install varnish cache on centOS?
A
Use the link to get documentation for install varnish cache on centos, "https://www.linuxhelp.com/how-to-install-varnish-cache-5-1-on-centos-7/"
Q
How to install "Varnish" on Ubuntu?
A
You can use the following command to install "Varnish" on Ubuntu. For syntax: "apt-get install Varnish".