With Cloud Servers, it is easier to spin up a separate server to work as front end. Let us get started with Varnish Cache setup with commands. We talked about Varnish Cache before, we said that; Varnish is a web accelerator for dynamic web pages with lots of content like WordPress. Unlike other client-side proxies or servers (read what is reverse proxy), Varnish was designed from the ground up as a Web accelerator. Varnish works by handling requests before they goes to your backend, whether your backend is Apache2, Nginx – does not matter. If only it has no request cached, it will forward the request to the backend and then cache its output.
Get Started with Varnish Cache : Spin a Server
We are using Ubuntu 14.04 LTS, 512 MB server will work fine for the most. First update the current packages and install Varnish :
1 2 | apt-get update && apt-get upgrade apt-get install varnish |
Edit the daemon options for Varnish to instruct it to load custom configuration:
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | nano /etc/default/varnish # output; scroll down and you'll get ## Alternative 2, Configuration with VCL # # Listen on port 6081, administration on localhost:6082, and forward to # one content server selected by the vcl file, based on the request. Use a 1GB # fixed-size cache file. # DAEMON_OPTS="-a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m" # change # -f /etc/varnish/default.vcl to # -f /etc/varnish/user.vcl |
From the configuration above, we need edit the value -s malloc,256m :
1 2 | -s malloc,360m # for 512mb server, approximately we should allow some RAM left! |
Copy default.vcl
to your own file :
1 2 3 | cd /etc/varnish && cp default.vcl user.vcl nano user.vcl # the file will open |
Get Started with Varnish Cache : Set up Backend
This is default :
1 2 3 4 | backend default { .host = "127.0.0.1"; .port = "80"; } |
It is actually caching Apache which is currently on port 80 on the same server, so change accordingly the IP. You should restart Varnish :
1 | service varnish restart |
Varnish runs on port 6081, we will edit the file :
1 2 3 4 | sub vcl_fetch { set beresp.ttl = 2h; } |
# We should change Apache’s port, edit /etc/apache2/ports.conf
:
nano /etc/apache2/ports.conf
Find NameVirtualHost *:80 and Listen 80 and change 80 in each to another port. We will change to 8080:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | NameVirtualHost *:8080 Listen 8080 # NameVirtualHost will not be required in later versions # edit /etc/apache2/sites-available/000-default.conf and set virtual host to listen at : # <VirtualHost *:8080> # configure Varnish to start on port 80 : nano /etc/default/varnish # should look like this DAEMON_OPTS="-a :80 # Open /etc/varnish/user : nano /etc/varnish/user # should look like this : backend default { .host = "127.0.0.1"; .port = "8080"; } # change host! that is for single server setup # restart services service apache2 reload service varnish restart |
This is the basic setup with Varnish. It itself will give you a good polish. Get used with Varnish and tweak the performance. We could install on same server, but actually its better to use a separate server. It makes the load balance management easier.