Varnish on a Virtualmin Server

This document describes how to run the Varnish cache on a Virtualmin server.

We're often asked about Varnish support in Virtualmin. Virtualmin is only designed to manage web servers, such as Apache and Nginx. However, with some manual configuration, it's possible to setup Varnish on a server being managed by Virtualmin.

Note that this setup isn't designed for a typical shared hosting server. Varnish isn't a general-purpose web server. Applications running behind Varnish need to be configured to work with the cache.

However, if your server is dedicated to the purpose of hosting a site or sites which are in need of the speed offered by the Varnish cache, this may be for you.

If you're looking for the speed of Varnish for a domain on a shared hosting server, you can still use Varnish, but you would want to install it onto a second server, rather than putting it onto the server hosting your domains. The steps below cover installing Varnish onto your Virtualmin server, but you could modify those steps to put it on a second server if that's what you need.

We'd encourage you to perform these steps first on a test system, before making any changes to a live server.

The following instructions are for CentOS 7+, Ubuntu 12.04+, and Debian 7+ (other distros/versions are likely to work, but may require some modification to the steps):

**Install Varnish**

*CentOS*


yum install epel-release
yum install varnish

*Ubuntu/Debian*

`apt-get install varnish`

**Edit the Varnish startup options**

*CentOS*

Edit `/etc/varnish/varnish.params`, and set:

`VARNISH_LISTEN_PORT=80`

*Ubuntu/Debian*

Edit `/etc/default/varnish`, and set:


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

**Edit the Varnish config.**

Edit `/etc/varnish/default.vcl`, and set the following host and port:


backend default {
.host = "x.x.x.x";
.port = "8080";
}

Where "x.x.x.x" is your server's primary IP address (often "eth0").

**Change the Apache "Listen" port**

*CentOS*

Edit `/etc/httpd/conf/httpd.conf` and change the "Listen 80" line to "Listen 8080".

*Ubuntu/Debian*

Edit `/etc/apache2/ports.conf`, and change port 80 to 8080 so the file looks like this:


Listen 8080
Listen 443

**Change Apache port for Existing Domains**

If you have any existing domains setup, you'll need to edit the port for each one in Virtualmin. To do that, go into Server Configuration -> Change IP Address for the selected domain, and on that screen, change `New HTTP port` from `80` to `8080`.

**Restart Apache and Varnish**


service apache2 restart
service varnish restart

**Edit Virtualmin config**

Lastly, in Virtualmin, we need to tell it to add new Apache websites on the new port. To do that, go into
System Settings -> Server Templates -> Default -> Apache Website -> Port number for virtual hosts, and there, set the port to `8080`.

**Varnish Setup Finished!**

If you access the websites on your server, they should now be going through your Varnish cache, rather than just Apache.

**Application Setup**

At this point, the only remaining step is to configure your application to use Varnish. Applications need a way to tell Varnish to purge the cache when the content changes. Applications such as WordPress, Drupal, and Joomla provide plugins for that.

As a temporary solution, you can also manually purge it by restarting the Varnish service.