How to Install Varnish with Apache

How To Install and Configure Varnish with Apache on Ubuntu

Varnish is a popular cache module that enables you to speed up your website performance and reduce server load. Let us take a look at how to install Varnish with Apache. We will basically run Varnish as a reverse proxy on port 80 and run Apache on a non-standard on port 8080, behind Varnish.


How to Install Varnish with Apache

Here are the steps to install Varnish with Apache on Ubuntu. Open a terminal session or SSH into your Linux system.


1. Change Apache’s Default Port

First, we change Apache’s default port from 80 to 8080. Open Apache’s port config file in a text editor

$ sudo vim /etc/apache2/ports.conf


Change the line

Listen 80


Listen 8080


Save and Close the file


Restart Apache Web Server to apply changes

$ sudo service apache2 restart


Now your Apache web server will listen to port 8080.


2. Install & Configure Varnish

Install Varnish with the command

$ sudo apt install varnish

Start and enable Varnish

$ sudo systemctl start varnish
$ sudo systemctl enable varnish


Before we configure varnish, take a backup of its default config file

$ sudo cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak


Next, open its default config file in a text editor

$ sudo vim /etc/varnish/default.vcl


Update Apache’s backend port from 80 to 8080. Locate the “backend default” block and update it to look like the following:

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


Next, change the default port of Varnish to 80. Run the command

$ sudo vim /etc/default/varnish


Change DAEMON_OPTS line to 80 as shown

varnish change default port


Save and close the file.


We need to also change the default port of varnish, when it runs as a service. So run the following command:

$ sudo vim /lib/systemd/system/varnish.service


Change ExecStart line to use port 80

change varnish port service


Save and close the file.


Restart systemd daemon and varnish service

$ sudo systemctl daemon-reload
$ sudo systemctl restart varnish


3. Test Varnish Configuration

Run a curl command to your website (or server IP). The output should contain via : 1.1 Varnish-v4 as shown below

varnish test configuration

If you open the same page on web browser, you will see the default Apache home page (or your website home page if you have changed it).


That’s it! You have installed Varnish in Apache.


About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!