Make your website load faster – Configure Varnish with Ubuntu 16.04

0
87
Configure Varnish on Ubuntu 16.04

Understanding Varnish in simple language

Suppose you are a Manager in Electricity Department. Customers come to you everyday to inquire about the bill amount. Unfortunately your customers have the habit of forgetting thing and you are so nice. So what happens is that customers visit you many times to ask for the same piece of information. I can imagine your plight. You have stored all the information in a big fat blue Folder stored safely in a locker kept in record-keeping room located on 5th floor. So you should learn to configure Varnish on Ubuntu 16.04. Doesn’t make sense now. Don’t bother just read on.

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.

Now imagine you have a Smart Assistant, who says that he will keep a snapshot of just the name and bill amount of customers for the present month with him.

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 update

apt-get install varnish

Edit /etc/apache2/ports.conf

cd etc/apache2

vim ports.conf

You can see following setting in the file

Listen 80
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutils.c>
Listen 443
</IfModule>

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

cd etc/default

vim varnish

DAEMON_OPTS=”-a :6081
-T localhost:6082
-f /etc/varnish/default.vcl
-S /etc/varnish/secret
-s malloc,256m”

Replace 6081 with 80

Edit /etc/varnish/default.vcl with a text editor and ensure the following is present.

cd /etc/varnish/

vim default.vcl

backend default {
.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

/usr /sbin /varnishd -a :6081 -T localhost:6082 -f /etc /varnish /default.vcl -S /etc /varnish /secred -s malloc,256m

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

[Unit]
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd
[Service]
Type=simple
LimitNOFILE=131072
LimitMEMLOCK=82000
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
ExecReload=/usr/share/varnish/reload-vcl
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true
[Install]
WantedBy=multi-user.target

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/systemd/system/varnish.service

and

/etc/default/varnish in order to make Varnish work properly.

SHARE
Previous articleCurious Case of Salary Slips
Next articleTattoo Trends That Are Way More Stylish
ReadXP is a happiness and wellness influencer in its readers’ life. Self-improvement, homemade beauty products, lifestyle and everything that will make the readers life happy and healthy is being taken care by our readxperts. Latin & Greeks from the Tech-World has also been converted into plain simple English for everyone to understand.