Latest posts by Goran (see all)
- Deploy with Bitbucket + Webhook + PHP/Bash - August 17, 2016
- How to setup Varnish Cache - December 17, 2015
- ExtJS 6 – So what’s new? - October 29, 2015
What is Varnish?
Varnish is a caching tool/application. They call it also web application accelerator.
It runs on Linux. I will explain how I installed and configured Varnish for this blog.
So I am using digitalocean hosting with 64 bit Ubuntu 14.04. Hosting is very fast, personally I think it’s the best VPS on the world right now for given money.
Ok so site is very fast on digitalocean droplet by itself because it runs on SSD and it’s not shared hosting. I was just curious how Varnish works because I heard great things.
Why to use it and how it works
Varnish cache works like some kind of proxy. We install varnish on host server and adjust it to listen all traffic on port 80 (so all public HTTP traffic), than we setup it to communicate to our core server on port 8080 for example. And setup Apache or Ngnix to listen to 8080 (default is 80).
Varnish caches server response, that is static HTML, JS, images etc and than returns just that, a static content. It will work in some cases even if shut down apache.
So we can reduce server overload by a huge amount and speed up our site by also a huge percent.
How to setup Varnish Cache
It’s not so difficult to install varnish. It can be a bit heavy to tweak some advanced settings.
Ok so let’s go step by step. This is how I setup it on DOcean
- Install it on UbuntuThis are instructions for ubuntu from official varnish site
12345apt-get install apt-transport-httpscurl https://repo.varnish-cache.org/GPG-key.txt | apt-key add -echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.1" >> /etc/apt/sources.list.d/varnish-cache.listapt-get updateapt-get install varnish
- Change default varnish port to 80
1nano /etc/default/varnish
You will see option DAEMON_OPTS – change it to 80. - Change default varnish port toward default server to 8080 (it can be other port if 8080 is taken)
1nano /etc/varnish/default.vcl
Be sure to set port to 8080. This will tell varnish to communicate with default server on port 80.
backend default {
.host = “127.0.0.1”;
.port = “8080”;
} - Now change apache setting to listen to 8080 port
1nano /etc/apache/ports.conf
By default there is setting for port 80 like
Liste 80
replace this line with
NameVirtualHost *:8080
Listen 127.0.0.1:8080We should now change apache setting for our website to listen to 8080
1nano /etc/apache2/sites-enabled/000-default.confChange <VirtualHost *:80> to <VirtualHost *:8080>
000-default.conf is a file for default site which is on /var/www/html
This file can be yoursite.conf. - Restart default server and apache
12service apache2 restartservice varnish restart
You can use also command varnishlog to monitor varnish caching process and than just reload your site.
Hack with ubuntu
In my case there was a problem with setup. I adjusted all settings but varnish used settings from other place so my setup for varnish ports was ignored.
Run this command to try to find other varnish config files on your system
1 |
sudo grep -R 'ExecStart=/usr/sbin/varnishd' /etc/ |
in my case there was a file /etc/systemd/system/multi-user.target.wants/varnish.service which had settings that I needed to change to settings like in /etc/varnish/default.vcl file.
Run this after to reload daemons.
1 2 |
systemctl daemon-reload systemctl restart varnish.service |
So if you have a problem try to apply that fix. Details of this problem are here.
Wp varnish vcl
Varnish uses a VCL scripting for custom setups and requirements.
I just copied script from github for our wp blog.
Here is: https://gist.github.com/nadirlc/46987b42447cf8e3be79
In my case it works. I hope we helped with you with setting up varnish cache. It’s a great tool.