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 1. Create varnish.repo file in /etc/yum.repos.d/ . We use vi editior, you can use your favourite file editor.
Paste below given yum configuration in varnish.repo file .
Step 2. Use yum command to install Varnish
yum install varnish varnishlibs varnishdevel
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
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.
Edit varnish.params file and in paramter VARNISH_LISTEN_PORT change port 6081 to 80 , so that Varnish will listen on 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 .
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/
(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”.
We have assigned 512m RAM for Varnish storage. (You can replace with your desired value as per your system’s RAM availability)
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.# Default backend definition. Set this to point to your content server.
backend default”127.0.0.1″”8080″ 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. 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. 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.
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.
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
Changing port no. in the Apache’s Virtual Host to 8080. Below is the sample vhost configuration.
httpdhttpdVirtualHostServerAdmin sharad@exampleServerName exampleDocumentRootErrorLogsampleerrorCustomLogsampleaccess_log common
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.
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
total 11740 root root 20454 varnish root root 11994544 varnishncsaroot@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.
Cookie __cfduidd8652ab687981884c3091f49db6a3d4771419099580 expires/; domain=.sharadchhetri.com; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
X-Pingback: http:/sharadchhetrixmlrpc//wp.me/3b8mT>; rel=shortlinkVarnish361159134758 varnishPoweredWithTheGraceOfGodCache BULLET