How To Install Varnish on Ubuntu 22.04 LTS

Install Varnish on Ubuntu 22.04

In this tutorial, we will show you how to install Varnish on Ubuntu 22.04 LTS. For those of you who didn’t know, Varnish is an open-source, high-performance HTTP accelerator that is used to improve the performance and scalability of web applications. It is a reverse proxy that sits in front of a web server and caches static content, such as HTML, CSS, and JavaScript files. By caching this content, Varnish reduces the workload of the web server and speeds up the delivery of content to users. Varnish offers many features such as private CDN, Gzip compression and decompression, HTTP streaming pass & fetch, etc.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the Varnish cache on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Varnish.
  • A non-root sudo useror access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Varnish on Ubuntu 22.04 LTS Jammy Jellyfish

Step 1. The first step is to update your system to the latest version of the package list. To do so, run the following commands:

sudo apt update
sudo apt install debian-archive-keyring curl gnupg apt-transport-https

Step 2. Installing Apache.

By default, Apache is available on the Ubuntu base repository. Now run the following command below to install the latest stable of Apache to your system:

sudo apt install apache2

By default, after the Apache installation successfully completed, the service should be up and running. To be certain, we need to start it again:

sudo systemctl enable apache2
sudo systemctl start apache2

By default, the Apache service is running on port 80. We need to change that for we can configure the Varnish cache to work with the Apache web server later:

nano /etc/apache2/ports.conf

Find the following line:

Listen 80

And, replaced it with the following line:

Listen 8080

Save and close the file, then edit the Apache default virtual host configuration file:

nano /etc/apache2/sites-available/000-default.conf

Find the following line:

<VirtualHost *:80>

And replace it with the following line:

<VirtualHost *:8080>

Save and close the file, then restart the Apache service to apply the changes:

systemctl restart apache2

For additional resources on installing and managing Apache, read the post below:

Step 3. Installing Varnish on Ubuntu 22.04.

By default, Varnish is available on Ubuntu 22.04 base repository. Now run the following command below to install the latest version of the Varnish cache to your Ubuntu system:

sudo apt install varnish

After that, start the service and enable it to run at system startup:

sudo systemctl enable varnish
sudo systemctl start varnish

Next, you will need to configure it to work with your web server. To do this, open the Varnish configuration file (/etc/varnish/default.vcl) and enter the following lines:

nano /etc/varnish/default.vcl

Change the following lines as per your backend server:

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

Step 4. Test Varnish.

To test that Varnish is working correctly, you can use the curl command to send a request to your web server. If Varnish is working correctly, the response should come from the Varnish cache. You can test this by entering the following command:

curl -I http://localhost

Output:

HTTP/1.1 200 OK
Date: Tue, 16 Jan 2023 14:18:34 GMT
Server: Apache/2.4.52 (Ubuntu)
Last-Modified: Tue, 16 Jan 2023 14:12:15 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/7.0)
ETag: W/"29af-g0detz6b9e071-gzip"
Accept-Ranges: bytes
Content-Length: 10671
Connection: keep-alive

Congratulations! You have successfully installed varnish. Thanks for using this tutorial for installing Varnish with Apache on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the Varnish website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!