How to Setup Varnish and Apache on Ubuntu Linux

jay prajapati Jp6Z2Mp5Sq0 unsplash
jay prajapati Jp6Z2Mp5Sq0 unsplash

This article is a tutorial on installing and configuring the open-source HTTP accelerator, Varnish, with Apache2 on Ubuntu 17.04 | 17.10. The guide provides step-by-step instructions to set up Varnish as the doorway to Apache2 to serve HTTP requests quickly, which enhances server performance by storing web caches in the system’s memory for faster retrieval on subsequent requests.

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

This brief tutorial will show students and new users how to install and configure Varnish with Apache2 on Ubuntu 17.04 | 17.10.

In this post, we’ll set up Varnish as the doorway or front-end to Apache2 to quickly serve HTTP requests.

Configuring Varnish as the front end to Apache2 or other web servers can significantly enhance the server’s performance. Varnish stores web caches in the system’s memory, ensuring faster retrieval in subsequent requests for the same resource.

To get this working, follow the steps below:

Install Apache2

First, run the commands below to install the Apache webserver.

sudo apt-get update
sudo apt-get install apache2

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

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

The apache2 HTTP service is default automatically bound to ports 80 and 443 for HTTPS. This

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

Switch the Apache default port to 8080

Since we want Varnish to listen for all traffic coming to port 80, which is Apache2’s default port, let’s configure Apache2 to use another port number. You can open the Apache 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 the Apache 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 the Apache default virtual host config file.

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

Then make the highlighted change below.


Save the file and exit.

Then restart Apache2

sudo systemctl restart apache2.service

To access Apache2, enter the server IP or hostname followed by port # 8080.

ex. http://localhost:8080

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 located 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 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 closeout.

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 set up correctly, Varnish should be the default listener of port 80. To test, run the commands below.

curl -I http://localhost

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:

Posted by

I love computers; maybe way too much. What I learned I try to share at

1 Comment

  1. Nice and easy tutorial. Keep it up.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.