Ubuntu

How to set up Varnish cache on Ubuntu 18.04

Most internet users don’t stick around slow websites longer than 10 seconds. In fact, according to a case study conducted by Financial Times, a small delay in the page loading speed can significantly reduce a user’s session and lead to high bounce rates. This implies that a slow website will negatively impact your online business and lead to a  sharp decline in your revenue. A fast website enables your visitors to gather information in the shortest time possible and will most likely lead to conversations. Additionally, Google now ranks fast websites higher in the SEO score than slow websites. Ever wondered how you can boost your site speed?Varnish cache is an opensource HTTP cache accelerator that ameliorates your site’s speed by up to 300 to 1000 times. It sits in front of a web server and serves HTTP requests to users at remarkably high speeds. It speeds a website by caching content that is frequently accessed by users and storing it in the memory, thereby guaranteeing faster retrieval of webpages.  It can also act as a load balancer in a setup with multiple web servers.  in the In this guide, we let you in on how you can install Varnish cache on Ubuntu 18.04

Step 1: Update the system

To start off, begin by updating the packages in the system to ensure that they are up-to-date. To do this, access your server as root and run the command:

# apt update && apt upgrade

Step 2: Install Apache webserver

Since Varnish cache sits in front of a webserver, we will need to install Apache webserver for demonstration purposes.

To install Apache, run the command:

# apt install apache2

Once the installation of Apache web server is complete, start the webserver and check it’s status using the commands below:

# systemctl start apache2
# systemctl status apache2

The output above confirms that the Apache webserver is up and running.

Step 3: Install Varnish HTTP Accelerator

With Apache webserver installed, install Varnish HTTP accelerator by running:

# apt install varnish

# systemctl start varnish
# systemctl status varnish

Step 4: Configuring Apache and  Varnish HTTP Cache

Apache webserver listens on HTTP port 80 for incoming connections. However, in our setup, we need to make a few adjustments. Since Varnish will be forwarding HTTP requests to the Apache webserver, we will configure the Varnish Accelerator to listen to port 80 and then configure Apache to listen to port 8080.

So, to configure Apache to listen to port 8080, open the configuration file as shown with

# vim /etc/apache2/ports.conf

Save and exit the configuration file.

On the same note, we are going to make changes to the default Apache virtual host file and configure it to listen to port 8080

# vim /etc/apache2/sites-emabled/000-default.conf

Save and exit the configuration file. For the changes to come into effect, restart the Apache webserver

# systemctl restart apache2

At this point, if you try accessing the webserver on port 80, you will get an error due to the changes we’ve just made. It can only be accessed via port 8080 as shown.

Step 5: Setting up Varnish to listen to port 80

We also need to configure Varnish to listen to port 80 in order to forward HTTP requests to the webserver. This will also eliminate the need to appending  8080 at the end of the URL when accessing the web browser.

Launch your preferred text editor and open the  /etc/default/varnish file.

# vim /etc/default/varnish

Scroll and locate the attribute ‘DAEMON_OPTS’. Be sure to change the port from 6081 to port 80

Save and close the text editor.

If you check the /etc/varnish/default.vcl file, you should get the output shown below.

Lastly, we need to edit the /lib/systemd/system/varnish.service and modify the port in ExecStart directive from port 6081 to 80.

Save and exit the text editor.

For the changes to come to effect, we need to restart Apache web servers, reload the system and restart Varnish in the order shown

# systemctl restart apache2
# systemctl daemon-reload
# systemctl restart varnish

Step 6: Testing the Configuration

To confirm that our configuration is all good use the curl command as shown:

# curl -I server_IP

Be on the lookout for this line Via: 1.1 varnish (Varnish/5.2) as indicated on the output above. This shows that Varnish works as expected.

You can now visit your webserver without appending the 8080 at the end.

Conclusion

And that’s how you configure Varnish Cache to work with Apache web server on Ubuntu 18.04. Your webserver should work faster than ever before thanks to the super-fast Varnish HTTP accelerator that will cache frequently accessed webpages and serve them at impressive speeds!

About the author

James Kiarie

Hey there, this is James Kiarie, a certified Linux administrator with over 4 years of experience in the installation and configuration of Linux servers. Additionally, I have over 2 years in penning down high-quality technical articles in Linux, Windows and tech gadgets. In my free time, catch me listening to music, biking, swimming and playing video games.