Understanding Varnish in simple language
Every time a customer comes, you have to go through the ordeal of leaving your soft comfortable chair, search for keys of locker, search for the big fat blue folder amidst other folders, then strain your eyes to search for the customer details.
So …. much of hard work.
Whenever a Customer comes for the first time, Assistant accompanies you in finding the details in the folder, ya the same big fat blue folder kept in record-keeping room located on 5th floor. Huff, i am already sweating.
After attending to the customer he keeps a snapshot of his name and bill amount with him. He repeats the drill for all the customers during their first visit.
It is one time effort. Now, whenever a forgetful customer comes back. your Assistant has the data ready and he hands it over to the customer. People are happy, they don’t have to wait for you for getting the data from 5th floor. And you are also happy that you are able to sit back and have a cappuccino in your AC office siting on this nice chair of yours.
You must be thinking, What has all this got to do with Website and Internet.
Well, You are Apache Server
Your Assistant is Varnish
Your Customer are people visiting your website.
And forgetful means http connection which is stateless.
And you don’t want to keep them waiting, right !
What is Varnish
We all have heard of cache. It undoubtedly remains the single most mission-critical software for a web application.
In the words of search engines If You are not FAST,
You won’t LAST.
As per Wikipedia “Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as heavily consumed APIs.”
Varnish Cache is a open source HTTP engine/reverse HTTP proxy that has the capability to speed up a website by manifolds.
Varnish asks your server once to cache the page, all subsequent requests for the same web resource will be served by Varnish Cache, without troubling your poor overloaded apache servers. This means blazing-fast content delivery and reduced resource fetching from back-end database server. Varnish Cache reduces back-end server load significantly.
It receives requests from clients and tries to answer them from the cache. If it cannot answer from the cache it will forward it to the origin server, fetch the response, store it in cache and deliver it to the client. When Varnish has a cached response ready, it is typically delivered in a matter of microseconds. But you need to configure it correctly with your Ubuntu version.
Who should use it
All those who want their websites to load faster.
This is a Server side setting. You will access to your hosting server to install varnish. If You are on shared hosting, you may get in touch with your hosting provider for the same.
Installing Varnish on Ubuntu 16.04
I assume you already have apache running.
apt-get install apt-transport-https
curl https://repo.varnish-cache.org/ubuntu/GPG-key.txt | apt-key add –
echo “deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0” >> /etc /apt / sources.list.d / varnish-cache.list
apt-get install varnish
You can see following setting in the file
Replace 80 with 8080
Apache will start listening to port 8080 instead of 80
Configure Varnish with Ubuntu 16.04
Edit /etc/default/varnish file in a text editor
Replace 6081 with 80
Edit /etc/varnish/default.vcl with a text editor and ensure the following is present.
.host = “127.0.0.1”;
.port = “8080”;
Tutorial on all official websites ends here (for Ubuntu 16.04). But is it working correctly.
Check on which port Varnish is listening
$ ps aux | grep varnish
As you can see, it still running on port 6081
Configuring Varnish on Ubuntu 16.04 correctly
# cd /lib/systemd/system/varnish.service /etc/systemd/system/
# vim varnish.service
You can see following setting
Description=Varnish HTTP accelerator
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
Replace 6081 with 80 here also
Now reload and restart servers. Everything fixed. Varnish has been configured correctly with Ubuntu 16.04
# systemctl reload varnish.service
# sudo service varnish restart
# sudo service apache2 restart
So, bottom-line is that we have to update port no. at two places in order to Configure Varnish with Ubuntu 16.04
/etc/default/varnish in order to make Varnish work properly.