How to Setup Varnish HTTP Cache on an Ubuntu 18.04 VPS or Dedicated Server

A slow loading website can affect the overall performance of your business. Even a one second delay can lead to fewer page views, decreased customer satisfaction, and loss of conversions.

Luckily, if you are facing such a challenge, you can use Varnish HTTP Cache to speed up your website by a factor of about 300 to 1000 times depending on your hardware architecture.

Varnish Cache is a web application accelerator that can be used as a proxy to your Apache web server. The open-source software sits in front of your web server to serve web traffic very fast. If you are running multiple servers, Varnish Cache can also be used as a load balancer.

Varnish works by caching regularly requested web content on the system memory, and this ensures faster information retrieval if the same information is asked for several times.

In this guide, we will show you how you can install and setup Varnish HTTP cache on your Ubuntu 18.04 VPS web hosting that's running an Apache web server.

Special Note: if you are concerned about the performance of your current hosting provider, you can use HostAdvice to look for a better one. Browse our Best Web hosting page to find the world's best hosting providers together with reviews, prices and features.

Prerequisites

  • A VPS plan
  • A non-root user with sudo privileges

Step 1: Install Apache

Before you begin, update the package list information to ensure we are installing the newest versions of software applications:

$ sudo apt-get update

Then install Apache web server using the command below:

$ sudo apt-get install apache2

Press Y and hit Enter when prompted to confirm the installation.

Step 2: Test if Apache is working

Once the Apache installation is complete, enter your Ubuntu 18.04 server public IP address or your domain name on a browser:

http://127.0.0.1

Or

http://example.com

You should see the below default Apache web page:

The above page confirms that Apache is working properly.

Step 3: Install Varnish HTTP Cache

Now that Apache is running, we can install Varnish HTTP Cache using the command below:

$ sudo apt-get install varnish

Press Y and hit Enter when prompted to confirm the installation.

Step 4: Change Apache and Varnish HTTP Cache listening ports

By default, Apache listens on port 80 for HTTP traffic. We need to make some changes here. Instead of the default settings, Varnish will instead listen on port 80  and forward all traffic to Apache web server which we will configure to listen on port 8080.

First, let’s assign port 8080 to Apache web server. To do this, edit the file ‘/etc/apache2/ports.conf’ file using a nano editor.

$ sudo nano /etc/apache2/ports.conf
Listen 8080
<IfModule ssl_module>
        Listen 443
</IfModule>
<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

Press CTRL + X, Y and hit Enter to save the file once you make the changes.

Next, edit the default Apache Virtual Host to listen to port 8080 too:

$ sudo nano /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:8080> 
...
</VirtualHost>

Press CTRL + X, Y and Enter to save the file

Restart Apache

$ sudo service apache2 restart

If you attempt to reach your website again from a browser, you will get an error. However, you can append port 8080 on your IP address to reach your server:

http: //127.0.0.1:8080

Step 5: Configure Varnish HTTP Cache to listen on port 80

Next we will configure Varnish to listen on port 80 and forward all requests to our Apache web server.

We can do this by editing Varnish configuration file /etc/default/varnish

$ sudo nano  /etc/default/varnish

Look for the directive ‘DAEMON_OPTS’ and change the listening port to 80.

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

Then, press CTRL + X, Y and hit Enter to save the file.

Next, check the file ‘/etc/varnish/default.vcl’ using a nano text editor. You should see the below content and this means Varnish will forward http traffic to port 8080:

$ sudo nano /etc/varnish/default.vcl

File contents:

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

We also need to edit the port on the file /lib/systemd/system/varnish.servicefile. To do so, type the command below:

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

Change the default port from 6081 to 80 as shown below

[Unit]
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd
[Service]
Type=simple
LimitNOFILE=131072
LimitMEMLOCK=82000
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f$
ExecReload=/usr/share/varnish/varnishreload
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true
[Install]
WantedBy=multi-user.target

Then, press CTRL + X, Y and hit Enter to save the file.

Step 6: Restart Apache, Varnish, and the Systemd Daemon

To reload the changes, we can restart Apache, Varnish and Systemd Daemon using the commands below:

$ sudo systemctl restart apache2
$ sudo systemctl daemon-reload
$ sudo systemctl restart varnish

If the setup was successful, Varnish will now be the default HTTP Listener on port 80.

Step 6: Testing the Setup

You can now try visiting your server one more time on a web browser:

http://public_ip_adress

Or

http://example.com

The server traffic should now be handled by Varnish HTTP Cache software and forwarded to Apache.

To make sure that Varnish is working, use the curl command for verification purposes:

$ curl -I server_ip_address

You should get an output similar to the below text. If you see the line ‘Via: 1.1 varnish (Varnish/5.2)’, then Varnish is working like expected.

HTTP/1.1 200 OK
Date: Thu, 05 Jul 2018 20:56:11 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Fri, 29 Jun 2018 07:19:34 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 7
Age: 0
Via: 1.1 varnish (Varnish/5.2)
ETag: W/"2aa6-56fc2ab77545d-gzip"
Accept-Ranges: bytes
Connection: keep-alive

Conclusion

In this guide, we have shown you how to setup Varnish HTTP cache software on your Ubuntu 18.04 server. If you followed the setup guide completely, your website should now load faster because Varnish will retrieve most of the frequently requested content from the memory.

 

Check out these top 3 Dedicated server hosting services:

Was this article helpful?