How to Install Apache with Varnish on Ubuntu Linux



The content provides a detailed guide on how to boost website performance by installing and configuring Varnish as a proxy server for Apache2. Varnish, an open-source HTTP accelerator, serves HTTP/HTTPS requests promptly and can act as a load balancer. The guide details steps to install Apache2 and Varnish, modify Apache2’s default port to 8080, and…

This article explains installing Apache on Ubuntu Linux with Varnish caching support.

Installing Apache2 with Varnish caching support can significantly improve the performance of your website. Varnish acts as a proxy server in front of Apache2 and quickly serves HTTP/HTTPS requests. By storing web caches in the system’s memory, Varnish ensures faster retrieval in subsequent requests for the same resource.

Configuring Varnish as the front end to Apache2 or other web servers can help distribute loads across multiple web servers and enhance server performance. This can result in faster website loading times and a better visitor user experience.

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 Apache2 webserver.

sudo apt-get update
sudo apt-get install apache2

After installing Apache2, the commands below can stop, start, and enable Apache2 always to start 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 automatically bound to ports 80 and 443 for HTTPS by default. 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 Apache2 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 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 the 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 the Apache2 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";

Please save the file and close it 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 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:

Like this:

3 responses to “How to Install Apache with Varnish on Ubuntu Linux”

  1. Tuvshinjargal Byambajav Avatar
    Tuvshinjargal Byambajav

    Thank you ver much. This is useful

  2. Jakke Lehtonen Avatar
    Jakke Lehtonen

    Useful yes, but not quite enough. What shall wwe do with SSL?

    1. Sangam Avatar

      Varnish does not supports SSL termination.

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.