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.

Step 3. Now you will get new varnish configuration directory and files.Varnish Configuration Directory : /etc/varnishVarnish 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/

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 AboutVarnish Storage BackendStep 6. Now about default.vcl file . It is set of VCL rules that are built in to Varnish.
Below given is default.vcl reference.

# 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 th 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. 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.

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.