How to speed up Apache with Varnish HTTP cache

If you're looking for a way to speed up your Apache server, Jack Wallen walks you through the process of making this so with the help of Varnish.

Image: Jack Wallen

Apache is a powerhouse web server that offers an extensive array of configurations, modules, and third-party addons. Because of this incredible flexibility, there's so much you can do to improve the performance of the web server. One such thing you can do is add the Varnish HTTP accelerator as a reverse proxy for Apache.

Varnish is ideal for high-traffic websites, and is used for the likes of The New York Times. I'm going to show you how to install and configure Varnish HTTP cache, for Apache, on Ubuntu Server 16.04.

The trick to this is running Apache on the non-standard port 8080 and then run Varnish, as a reverse proxy, on port 80. I will assume you already have Apache installed and running. With that in place, let's get to work.

Changing Apache's default port

The first thing we must do is configure Apache to use a non-standard port. Out of the box, Apache defaults to port 80. To change this, open a terminal window and issue the command sudo nano /etc/apache2/ports.conf. Change the line Listen 80 to Listen 8080. Save and close that file.

Restart Apache with the command:

sudo systemctl restart apache2

If you point your browser to http://SERVER_IP (Where SERVER_IP is the IP address of your server), you should not be able to connect to the server. Instead, point it to http://SERVER_IP:8080 (Where SERVER_IP is the IP address of your server), and the Apache server welcome page should show up, or whatever site you have enabled.

NOTE: If you have virtual servers setup, you'll want to edit their configuration file(s) in /etc/apache2/sites-available/ to reflect the change from port 80 to port 8080.

Install and configure Varnish

Back at your terminal window, install Varnish with the command sudo apt install varnish. Start and enable Varnish with the following commands:

sudo systemctl start varnish
sudo systemctl enable varnish

With Varnish installed, change into the /etc/varnish directory and backup the configuration file with the command sudo cp default.vcl default.vcl.BAK. Open the configuration file with the command sudo nano /etc/varnish/default.vcl.

The first thing we must do is define the backend port (Apache). Locate the following section and make sure it looks exactly like this:

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

Next the default Varnish ports must be changed to port 80. Issue the command sudo nano /etc/default/varnish and change the DAEMON_OPTS line option to 80 (as shown in Figure A).

Figure A

Figure A

Changing the default Varnish port.

Save and close the file.

There's one more configuration option to take care of. Issue the command sudo nano /lib/systemd/system/varnish.service and then change the ExecStart line to reflect port 80 (as shown in Figure B).

Figure B

Figure B

Another port change for Varnish.

Save and close that file.

Restart the systemd daemon and restart Varnish with the following commands:

sudo systemctl daemon-reload
sudo systemctl restart varnish

Point your browser back to http://SERVER_IP and the expected page should, once again, load. One final test would be to issue the command curl -I SERVER_IP (Where SERVER_IP is the IP address of the server). The output of the command should include the line Via: 1.1 varnish-v4 (Figure C).

Figure C

Figure C


If you want to check the Varnish log, issue the command sudo varnishncsa. This will run a tail on the Varnish log, so you can view it in real time.


Your Apache server should now see a considerable increase in speed. If not immediately, then it will over time, as the cache builds.

Also See

By Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website