How to install Varnish 4 version on CentOS 7 / RHEL 7

In this tutorial we will learn, how to install Varnish 4 version on CentOS 7 / RHEL 7 . Varnish is populalry known for Frontend Web Cacheing software. Whereas it can also be used as loadbalancer.
Recently , the Varnish version 4 is released, which comes up with new features and also new syntaxes/parameters.

The Varnish 4 has lots of changes. We will strictly recommend if you are planning to upgrade Varnish from version 3 to 4, do it in test or staging server. You will see lots of syntax and parameters difference. Hence, when you upgrade the Varnish by using yum or apt-get command, the Varnish service will not start, only due to incompatible syntaxes/parameters.

Before starting installing Varnish 4.x version on CentOS 7 Server. Do not forget to read below given links.

1. Changes in Varnish version 4
2. Varnish VSL Query Expression

Follow the Given below steps to install Varnish 4.x on CentOS 7 / RHEL 7

Step 1. Create varnish.repo file in /etc/yum.repos.d/ . We use vi editior, you can use your favourite file editor.

vi /etc/yum.repos.d/varnish.repo

Paste below given yum configuration in varnish.repo file .

[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

Step 2. Use yum command to install Varnish

yum install varnish varnish-libs varnish-libs-devel

Step 3. Now you will get new varnish configuration directory and files.

Varnish Configuration Directory : /etc/varnish
Varnish Configuration Files : We will get 3 new files in /etc/varnish directory
(a) default.vcl
(b) secret
(c) varnish.params

Step 4. Bydefault Varnish 4 listens on port 6081 . This information we can get in file varnish.params. I hope you are also aware that http listen on port 80. Hence, we will change the port 6081 to 80 so that website first access Varnish Cached in frontend.

By default Varnish listen on port 6081.

VARNISH_LISTEN_PORT=6081

Edit varnish.params file and in paramter VARNISH_LISTEN_PORT change port 6081 to 80 , so that Varnish will listen on port 80

vi /etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80

Step 5. In this step we will decide the Varnish Cache storage. We have two option here,
NOTE: select any one option. NOT BOTH

(i) Varnish Cache storage on Disk
(ii) Varnish Cache storage on Memory (RAM) [ For more faster access ].

Here, you have to decide which option varnish storage option you will go for.

(i) Setting Varnish Cache Storage on disk :
In this setting, edit varnish.params file and set value on the parameter VARNISH_STORAGE .

vi /etc/varnish/varnish.params

Below given is default value for VARNISH_STORAGE . The storage file varnish_storage.bin has size of 1 GB and located at /var/lib/varnish/

VARNISH_STORAGE="file,/var/lib/varnish/varnish_storage.bin,1G"

(ii) Setting Varnish Cache Storage on RAM :
This is another option which is faster than varnish storage on disk and that is using RAM.If you have server with high RAM configuration and have enough of free RAM for Varnish,use this option then.
NOTE:Meaning of Malloc is “Allocate memory block”.

vi /etc/varnish/varnish.params

We have assigned 512m RAM for Varnish storage. (You can replace with your desired value as per your system’s RAM availability)

VARNISH_STORAGE="malloc,512m"

Learn More About Varnish Storage Backend

Step 6. Now about default.vcl file . It is set of VCL rules that are built in to Varnish.

Below given is default.vcl reference.

# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

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

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    # 
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    # 
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    # 
    # You can do accounting or modifying the final object here.
}

The default.vcl file is actually is not fully configured.Hence, there is nothing for vcl_recv, vcl_backend_response and vcl_deliver.

It is important to know, to direct the marker for Varnish 4.x setting, in default.vcl file vcl 4.0; should be set. (see above default.vcl file)

As per backend director defined in default.vcl file. The web traffic goes to loopback IP address 127.0.0.1 and connect to port 8080 for http communication.

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

As per above backend default configuration, it means – the Varnish and web server are running in single same server.
We have already changed the Varnish port to 80 in Step 4. Now as per configuration, web server should listen the port no. 8080. If your web server is running on different port no. , give the same port no. in “backend default” respective to server.

Varnish 4

Change apache port no. to 8080

Here, we will quickly change the Apache’s port no. to 8080 because the port no. 8080 we have defined in varnish default.vcl file.

change the value of Listen in httpd.conf

vi /etc/httpd/conf/httpd.conf

Listen *:8080

Changing port no. in the Apache’s Virtual Host to 8080. Below is the sample vhost configuration.

vi /etc/httpd/conf/httpd.conf


    ServerAdmin sharad@example.com
    ServerName  example.com
    DocumentRoot /var/www/html/
    ErrorLog logs/sample-error.log
    CustomLog logs/sampleaccess_log common

Final Step: Start/restart the Varnish,Apache services

Now we are almost finish to Varnish setup. Go step wise.

1. Restart the Apache (Webserver) : To effectively change the port no. of Apache service. Restart the apache service on CentOS 7/RHEL 7 server.

systemctl restart httpd

No the apache will lsiten the port 8080. Use ss command.

2. Restart the Varnish server : After varnish get restarted the varnish will lsiten to port 80. Hence, whenever user browse the http website, first the content will be served by Varnish Server from Cache (either from Disk or RAM [malloc] , as we have set in Step 5)

systemctl restart varnishd

3. Enable Varnish logging : Logs are important for any service, therefore we will enable the Varnish logging. Restart the varnishncsa and varnishlog server.

Restart varnishncsa.

systemctl restart varnishncsa

Restart varnishlog .

systemctl restart varnishlog

By defualt Varnish log directory path is /var/log/varnish/

You will see two log files of Varnish

[root@server ~]# ls -l /var/log/varnish/
total 11740
-rw-r--r-- 1 root root    20454 Dec 20 13:09 varnish.log
-rw-r--r-- 1 root root 11994544 Dec 20 13:09 varnishncsa.log
[root@server ~]# 

Now your site is up and running. And at frontend the Varnish Cache server will serve the content first. It will make your website access faster.

Later, We will discuss on VCL file of WordPress for Varnish 4.x version. We have implemented on our blog the same.

Given below is the live status of Varnish running for our blog.

sharad@linuxworld:~/Desktop$ curl -I sharadchhetri.com
HTTP/1.1 200 OK
Date: Sat, 20 Dec 2014 18:19:40 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Set-Cookie: __cfduid=d8652ab687981884c3091f49db6a3d4771419099580; expires=Sun, 20-Dec-15 18:19:40 GMT; path=/; domain=.sharadchhetri.com; HttpOnly
Vary: Accept-Encoding
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Pingback: https://sharadchhetri.com/xmlrpc.php
Link: ; rel=shortlink
X-Varnish: 361159 134758
Age: 67
Via: 1.1 varnish-v4
X-Powered-By: WithTheGraceOfGod
X-Cache: BULLET
X-Cache-Hits: 1
Server: cloudflare-nginx
CF-RAY: 19bdd9770c510be1-HKG

sharad@linuxworld:~/Desktop$ 

7 thoughts on “How to install Varnish 4 version on CentOS 7 / RHEL 7”

  1. Followed your tutorial, but httpd gives me a connection refused on port 8080. It works on port 80 though.

    • The tutorial is correct. We have changed Apache port 80 to 8080 . Varnish server is in frontend. We have made varnish to run on port 80 rather than its default port. Why we did so because the web browser by default open website on port 80. The Varnish Cache serves the traffic on port 80 because it is in frontend and fetch the content from web server by connecting at its port 8080.

      Here is varnish config for this.

      backend default {
          .host = "127.0.0.1";
          .port = "8080";
      }
      
  2. se linux was runing was not able to start varnish issued the command after that able to run varnish thank you: sudo setenforce 0

  3. Hi Sharad,

    Even I have been trying to update my skills on Varnish 4. I am on CentOS 6.6 for testing. My currently installed varnish Version is 4.0.2…. Inside ‘/etc/varnish’… I have ‘secret’ and ‘default.vcl’ and no ‘varnish.params’ file. I have ‘/etc/sysconfig/varnish’ though as in varnish v3… However I have not installed ‘varnish-libs-devel’. The basic caching is fine… Still need to work on it…. But this is strange… you have ‘varnish.params’ to define cache related parameters… Is it something related to CENTOS version..??? I mean different config file for 6 and 7…?? No Idea if I am doing things in the wrong way….

    • When I last upgraded the Varnish 3 to 4 on CentOS 6.5, I have not found varnish.params file. I worked on /etc/sysconfig/varnish on CentOS 6.5 .

      On CentOS 7, I got varnish.params . I am not sure with CentOS 6 relation with varnish.params. I will check and will update .

      Regards
      Sharad

Comments are closed.