How to install Varnish for Apache Web Server

Varnish or Varnish cache is an open source http proxy/accelerator that is put in front of webserver, for the purpose of speeding up the webserver (Apache & nginx both), it’s especially meant for webservers that are serving content heavy dynamic websites. Varnish works by redirecting user requests to static, thus reducing the calls to dynamic pages & thereby reducing load. It visits server once to cache the page & then all the future calls to the page are served by varnish cache.

It makes website really fast. Depending upon the architecture being used, it can make website 3 to 10 times faster. In this tutorial, we are going to discuss the installation of Varnish for Apache Webserver on RHEL/CentOS .

Pre-requisites

– We need to have an apache webserver installed

To setup an apache webserver, read our article STEP BY STEP APACHE SERVER INSTALLATION.

– We also need to have EPEL repositories installed on our system

Varnish packages are available in the EPEL repository. To install EPEL repositories on the system execute the below mentioned command, , depending on the OS & architecture,

RHEL/CentOS 7

$ sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-10.noarch.rpm

RHEL/CentOS 6 (64 Bit)

$ sudo rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

RHEL/CentOS 6 (32 Bit)

$ sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

 

Installation

We are now ready to install varnish on our machine & with EPEL repositories installed & enabled, it can be done through yum. To install varnish, open your machine terminal & execute the following command,

$ sudo yum install varnish

After installation is complete, we will start the service & enable it at boot,

$ sudo systemctl start varnish

 

Configuration

We will now configure both Varnish cache & apache server to work with each other. As mentioned above varnish server is placed before apache webserver, so firstly we will configure varnish server to listen to port 80. To do this open the varnish configuration file ‘/etc/varnish/varnish.params’

$ sudo vim /etc/varnish/varnish.params

& change the port from 6081 to port 80,

VARNISH_LISTEN_PORT=80

After making changes, save file & exit. Next, we will set the IP address & port on which the varnish server will look for the server content, to do this we need to edit ‘/etc/varnish/default.vc1’

Open ‘default.vc1 & make the following entries,

$ sudo vim /etc/varnish/default.vc1

backend default {

.host = “127.0.0.1”;

.port = “8080”;

}

We can change the IP address to 127.0.0.1 to IP address of the webserver, if varnish cache & webserver are on separate machines. Also we can make change to port, as per our requirement. Next, restart the varnish service to implement the changes made,

$ sudo systemctl restart varnish

Now we will make changes to apache webserver, so that it listens to the port that we have mentioned in on varnish cache in ‘default.vc1’ file. Open ‘httpd.conf’

$ sudo vim /etc/httpd/httpd.conf

& change the port number,

Listen 8080

Save file, exit & restart the apache service to implement the changes.

$ sudo systemctl restart httpd

Our setup is now complete. To test that things are workin as they are supposed to, execute the following command,

$ curl -I http://localhost

It should return with output with varnish version, something like

HTTP/1.1 403 Forbidden

Date: Mon, 18 Sep 2017 01:28:41 GMT

Server: Apache/2.4.27 (CentOS) PHP/5.4.16

Last-Modified: Tue, 14 Mar 2017 11:27:18 GMT

ETag: “1896-6152e1i536981”

Accept-Ranges: bytes

Content-Length: 4897

Content-Type: text/html; charset=UTF-8

X-Varnish: 13

Age: 0

Via: 1.1 varnish-v4

Connection: keep-alive

This shows varnish is working as it’s supposed to & serving the httpd pages.

That’s it for now. If you have any queries or questions or just want to provide valuable suggestions, please do so using the comment box below.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

Leave a Reply