Install Apache2 with Varnish Support on Ubuntu 16.04 LTS Server

If you want your websites to run fast, you may want to install Varnish to be a proxy server to Apache2. To install and configure Varnish to be a proxy server to Apache2, then the steps below is a good starting point.

Varnish is an open source HTTP accelerator. It is usually configured to sit in front of webservers to quickly serve HTTP/HTTPS requests. Varnish can also be used as load balancer to distribute loads across multiple webservers.

This brief tutorial is going to show students and new users how to install and configure Varnish with Apache2 on Ubuntu 16.04 LTS. In this post, we’ll setup Varnish to be the doorway or front-end to Apache2 to quickly serve HTTP requests.

When you configure Varnish to be the font-end to Apache2 or other webservers, it can greatly enhance the server performance. This is because Varnish stores web caches in system’s memory ensuring faster retrieval in subsequent requests for the same resource.

To get this working, follow the steps below:

Step 1: Install Apache2

First run the commands below to install Apache2 webserver.

sudo apt-get update
sudo apt-get install apache2

After installing Apache2, the commands below can be used to stop, start and enable Apache2 to always startup everytime the server boots up.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

By default apache2 HTTP service automatically is bond to port 80 and 443 for HTTPS. This

Step 2: Install Varnish

Now that Apache2 is installed, run the commands below to install Varnish

sudo apt-get install varnish

After installing Varnish, the commands below can be used to start, stop and enable Varnish to always start up when the server boots

sudo systemctl stop varnish.service
sudo systemctl start varnish.service
sudo systemctl enable varnish.service

Step 3: Switch Apache2 default port to 8080

Since we want Varnish to listen for all traffic coming to port 80 which is Apache2 default port, let’s configure Apache2 to use another port number. You can open Apache2 default port configuration file at /etc/apache2/ports.conf and change the Listen value to 8080.

To quickly change the port run the commands below to open Apache2 default port configuration file.

sudo nano /etc/apache2/ports.conf

Then make sure the file has these lines. Save when done.

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf


Next, open Apache2 default virtualhost config file.

sudo nano /etc/apache2/sites-available/000-default.conf

Then make the highlighted change below.


Save then file and exit.

Then restart Apache2

sudo systemctl restart apache2.service

Now to access Apache2, you’ll have to enter the server IP or hostname followed by port # 8080.


Step 4: Configure Varnish to use Port 80

Now that port 80 is free, let’s configure Varnish to use that post instead. To assign port 80 to Varnish, run the commands below.

Varnish default configure file is location at /etc/default/varnish

Open it by running the commands below:

sudo nano /etc/default/varnish

Then look for the config block under Alternative 2 and make the highlighted changes as shown below.

## 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.
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"

Save the file when you’re done.

Next, run the commands below to open the default.vcl file

sudo nano /etc/varnish/default.vcl

Then verify the line shown below is what you see.

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

Save the file and close out.

After that, restart both Apache2 and Varnish

sudo systemctl restart apache2.service
sudo systemctl restart varnish.service

Next, run the commands below to start Varnish if it won’t start.

sudo /usr/sbin/varnishd -a :80 -b localhost:8080

If everything is setup correctly, Varnish should be the default listener of port 80. To test, run the commands below.

curl -I

The results should be something like the one below

HTTP/1.1 200 OK
Date: Sun, 23 Jul 2017 17:45:49 GMT
Server: Apache/2.4.25 (Ubuntu)
Last-Modified: Sun, 23 Jul 2017 17:01:05 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 10 3
Age: 9
Via: 1.1 varnish (Varnish/5.0)
ETag: W/"2aa6-554ff0b3c88c9-gzip"
Accept-Ranges: bytes
Connection: keep-alive

Congratulations! You’ve just installed Apache2 with Varnish support.

You may also like the post below: