2015-05-19

  
How to Setup Varnish Cache with VHosts

Learn how to install and configure Varnish 4 with Sentora and Virtual Hosts.

What's Varnish?

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture.
read more at the varnish-cache.com about page or watch a quick video about varnish (2min)

System Requirements

Make sure you have the following:

Root Access.

64bit OS (this guide is only for Ubuntu 14.04, but Varnish can be installed on other systems).

1Gb of RAM is recommended.

more details on the official varnish-cache.com requirments page

Installation

Login to your server via SSH, make sure you are logged in as root.

Enter the following commands in the Terminal:

Code:

apt-get install apt-transport-https

curl https://repo.varnish-cache.org/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

Setup Varnish

Installing was easy, not let's get our hands dirty and begin the fun part.

I will try to avoid server downtime, however keep in mind that you will need to restart apache at least once.

By default Varnish listens on port 6081 and uses the backend (apache) on port 8080;

In a production server we want Varnish to listen on port 80.

Test Drive Varnish (optional step)

Let's just verify if Varnish is up and running, to do this we need to change the default.vcl file.

Open /etc/varnish/default.vcl on your favorite text editor and change the .port to 80 (should be line 18):

PHP Code:

# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
#.port = "8080";
.port = "80";
}

see the default default.vcl file at the varnish GitHub Repository

Restart the Varnish service to apply the changes (execute the command on the terminal):

Code:

service varnish restart

Now open your website on port 6081 (http://domain.tld);

If Varnish is running correctly you should see your website, for further confirmation you can check the website headers (link to header-checker tool) for the  information Via => 1.1 varnish-v4.

Now that we tested it is working revert your default.vcl file back to.port 8080 (no need to restart varnish service right now).

Change Varnish Listening Port

First we will change the Varnish listen port from 6081 to 80;
Open /etc/varnish/default.vcl on your favorite text editor and change the DAEMON_OPTS="-a to 80 (~ line 40):

PHP Code:

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

Change Apache Port

Now we need to switch apache to port 8080;

Let's use the setso --set command that Sentora provides to easily change settings;

The first command changes the global apache port and the second one changes the Sentora Panel port.

Code:

setso --set apache_port 8080

setso --set sentora_port 8080

Launch Varnish

Login to Sentora Control Panel as zadmin and on Module Manager select Apache Config and switch the Apache Port from 80 to 8080 scroll to the end of the page and check the Force Update box to ensure the vhosts file will be updated on the next Daemon Run.

WARNING: After you Save the settings your server will be switched to port 8080, this means that every website in your server will not be accessible. To minimize downtime we need to make sure that the following commands are done quickly.

Let's manually trigger the sentora Daemon and restart Varnish to apply the changes:

Code:

php -q /etc/sentora/panel/bin/daemon.php

service varnish restart

If everything went ok, without errors, Varnish should now be in front of your server (on all VHosts).

Verify your website (normal url, no port required) and check the website headers (link to header-checker tool) for the  information Via => 1.1 varnish-v4.

Last Comments

Not being hard to install and use, Varnish is a advanced tool and should be used with care.

Soon I will create a new guide about basic VCL scripting and how you can use your VCL files to cache only some websites on your Sentora Server.

If this guide helped you, please press the Thanks button and consider giving me some Positive Reputation using the Rate button.

I'll be happy 

Show more