Speed up your website: Enabling Varnish Cache on Ubuntu

Last updated December 6th, 2023 00:21

In today’s world, where speed and instant access to information play a crucial role, it is important for websites to load as quickly as possible. You have probably experienced frustration when a website loads slowly and tested your patience. But don’t despair! There is a tool that can significantly increase the speed of website loading. It’s called Varnish Cache. In this article, you will learn what Varnish Cache is, how it works, and how to enabling Varnish Cache on Ubuntu OS.

Speed up your website: Enabling Varnish Cache on Ubuntu

What is Varnish Cache?

Varnish Cache is an open-source caching system that sits between the web server and the user. Simply put, it acts as a mediator between you and the website you are trying to load. Its task is to store copies of web pages retrieved from the web server and subsequently provide these copies to users more quickly. This means that instead of repeatedly fetching content from the web, Varnish Cache delivers the stored version to users, significantly reducing loading time.

How does Varnish Cache work?

Varnish Cache utilizes the principle of caching. When a user requests a specific web page, Varnish Cache first checks if that page is already stored in its memory. If it is, it immediately delivers it to the user, resulting in fast loading. If the web page is not found in the cache, Varnish Cache sends a request to the web server, retrieves and stores a copy of the page in its memory for future use.

Enabling Varnish Cache on Ubuntu

Let’s take a look at how to install and enable Varnish Cache on an Ubuntu server. As a first step, update the package list:

					sudo apt-get update

Now you can proceed with the installation of Varnish using the following command:

					sudo apt-get install varnish -y

You can check if Varnish Cache was installed correctly by running the command to display its status:

					sudo systemctl status varnish

The output will look something like this. The important part is the section that says “Active: active“.

					root@ubuntu:~# sudo systemctl status varnish
● varnish.service - Varnish HTTP accelerator
     Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-08-04 18:59:09 UTC; 4min 41s ago
       Docs: https://www.varnish-cache.org/docs/6.1/
   Main PID: 3423 (varnishd)
      Tasks: 217 (limit: 2344)
     Memory: 10.7M
     CGroup: /system.slice/varnish.service
             ├─3423 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
             └─3447 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Varnish Configuration

By default, Varnish listens on port 6081 and connects to the local web server on port 8080. In the configuration, you will make adjustments to have Varnish serve the static web under Apache. Typically, you would want the listening port to be 80, but since you are using Apache and Varnish on the same server, you will use port 8080 for Varnish and port 80 for Apache.

Now, in the /etc/systemd/system/ directory, create a file called customexec.conf and a new folder called varnish.service.d, where you will make changes to the default ports.

Use the mkdir command to create the folder:

					sudo mkdir /etc/systemd/system/varnish.service.d

Now use the nano editor to create the customexec.conf file:

					sudo nano /etc/systemd/system/varnish.service.d/customexec.conf

Insert the following lines into the customexec.conf file:

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :8080 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Save the file using the shortcut CTRL+X. Then run this command to reload the systemd services file from disk:

					sudo systemctl daemon-reload

Also, restart Varnish to apply the changes:

					sudo systemctl restart varnish

To verify that everything went smoothly and that Varnish is listening on port 8080, you can run the command:

					sudo netstat -ltnp | grep 8080

As output, you should receive:

					tcp        0      0  *               LISTEN      18689/varnishd
tcp6       0      0 :::8080                 :::*                    LISTEN      18689/varnishd

Editing the default configuration file

Varnish is now running and listening on port 8080. Therefore, make modifications to the configuration file located at /etc/varnish/default.vcl:

					sudo nano /etc/varnish/default.vcl

Find the line with “.port” in it and change the port to 80. This is the port used by Apache for HTTP connections.

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

Save the changes in the configuration file and restart Varnish again.

					sudo systemctl restart varnish

Enabling Varnish Cache on Ubuntu – conclusion.

Now Varnish should be running and caching the data processed by Apache on port 80. If you have a website like WordPress, you can install and configure a plugin that utilizes Varnish cache. Personally, for this cache, I use WP-Rocket, which performs traditional caching, page preloading, and is also integrated with the Cloudflare CDN, serving Varnish Cache. It is a comprehensive plugin that takes care of the complete website cache (among other things, it clears OPcache data on the server when clearing the cache).

The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 0 / 5. Počet hlasování: 0

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.


Subscribe to the Newsletter

Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.

Upozornit na
0 Komentáře/ů
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje