Table of Contents

How to Install Varnish with Apache on Ubuntu 22.04

Hello, friends. We already know that Apache is one of the best web servers out there. But there are always tools to improve it and to increase its power. So, today, you will learn how to install Varnish with Apache on Ubuntu 22.04.

According to the tool’s website

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents.

This makes it a very popular tool for many sysadmins who see it as a good way to speed up the web server and its response times.

Fortunately, it is available for Linux, so we can install it without too much trouble.

How to install Varnish with Apache on Ubuntu 22.04

Install Apache on Ubuntu 22.04

The first step is to install Apache on Ubuntu 22.04. To achieve this, open a terminal and update the system first.

# sudo apt update
# sudo apt upgrade

Then, you can install it from the official repositories, which is the most recommended method.

# sudo apt install apache2

Now you will have to configure it a bit for varnish. In this case, we just need to change the port from 80 to 8080 or any other.

To achieve this, edit the Apache port configuration file

# sudo nano /etc/apache2/ports.conf

And look for the line

Listen 80

To change it for this one

Listen 8080

Remember that it can be another port. Now, save the file and exit the editor.

Now do the same but the configuration file of the VirtualHost

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

Replace

<VirtualHost *:80>

By

<VirtualHost *:8080>
Configuring the Apache VirtualHost for Varnish
Configuring the Apache VirtualHost for Varnish

Apply the changes by restarting the service

# sudo systemctl restart apache2

Installing the latest stable version of Varnish

To install the latest stable version of Varnish, you have to add an external repository and set it to high priority.

First, install some necessary packages

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

Then download and add the GPG key from the Varnish repository

curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/varnish.gpg

Next, add the repository as such to the list of package sources.

# sudo nano /etc/apt/sources.list.d/varnishcache_varnish70.list

And in that file, add the following

deb https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main
deb-src https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main

The next step is to change the priority of the repository. To achieve this, create a special configuration for this repository.

# sudo nano /etc/apt/preferences.d/varnish

And add the following

Package: varnish
Pin: origin packagecloud.io
Pin-Priority: 900

Save the changes and close the editor. Now refresh the repositories.

# sudo apt update

Next, install Varnish

# sudo apt install varnish

Configuring Varnish with Apache on Ubuntu 22.04

Now the next step is to configure Varnish to work with Apache. This can be done, by modifying its configuration file.

# sudo vi /etc/varnish/default.vcl

Now modify it so that you see something like this

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}
Configuring Varnish with Apache on Ubuntu 22.04
Configuring Varnish with Apache on Ubuntu 22.04

Save the changes and close the editor.

To make this the default, you have to create a new service file. First the folder:

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

And then the new service file

# sudo nano /etc/systemd/system/varnish.service.d/customport.conf

Add the following

[Service] 
ExecStart=
ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m

Similarly, save the changes and close the editor.

To apply the changes run

# sudo systemctl daemon-reload

And restart Varnish

# sudo systemctl restart varnish

Now check the status of the service to see if everything is OK

# sudo systemctl status varnish

As you can see, everything is fine.

To check if everything is ok, then you can use curl and ask for information from the web server

curl -I http://localhost/

Conclusion

Varnish is a vital cache project for Apache and web servers. It is very fascinating, and now you know how to configure it with Apache in Ubuntu 22.04.

Leave a Reply

Your email address will not be published. Required fields are marked *