Install Varnish Cache with Apache on AlmaLinux 9 – Best Cache Reverse Proxy

This guide will teach you to Install Varnish Cache with Apache on AlmaLinux 9.

Varnish acts as a cache HTTP reverse proxy and sometimes you can also see it defined as a front-end accelerator. It’s not a stand-alone solution, because it needs a dedicated web server to rely on, like NGINX or Apache.

You can use Varnish to cache both dynamic and static content: this is an efficient solution to increase your website speed and server performance.

Steps to Install Varnish Cache with Apache on AlmaLinux 9

To Install Varnish Cache with Apache on AlmaLinux 9, you need some requirements.

Requirements

First, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can visit our guide on Initial Server Setup with AlmaLinux 9.

Also, you must have Apache installed on your server. For this purpose, you can follow our guide on How to Install Apache on AlmaLinux 9.

Apache configuration on AlmaLinux 9

By default, Apache listens on port 80. You need to change it to 8080.

First, open the Apache configuration file with your favorite text editor, here we use vi:

 sudo vi /etc/httpd/conf/httpd.conf

Search for the Listen line and change it to 8080:

Listen 8080

When you are finished save and close the file.

Now restart Apache on AlmaLinux 9 to apply the changes:

sudo systemctl restart httpd.service

Then, create a test file with the following command:

sudo touch /var/www/html/test.html

At this point, use the curl tool to test the server at port 8080. This verifies Apache is configured correctly:

curl -I http://localhost:8080/test.html

In your output you will see:

Output
HTTP/1.1 200 OK
Date: Tue, 29 Nov 2022 08:56:25 GMT
Server: Apache/2.4.53 (AlmaLinux)
Last-Modified: Tue, 29 Nov 2022 08:56:11 GMT
ETag: "0-5ee9828ded2e1"
Accept-Ranges: bytes
Content-Type: text/html; charset=UTF-8

Now you can start to Install Varnish Cache with Apache on AlmaLinux 9.

Install Varnish Cache on AlmaLinux 9

You need to add the Epel repository to your server. To do this, run the following command:

sudo dnf install -y epel-release

Now you need to install the dependency packages with the following command:

sudo dnf install dnf-plugins-core

Add Varnish Cache Repository

By default, Varnish is not available on the AlmaLinux 9 base repository. Now we run the following command to add the Varnish cache repository to your system using the following command:

curl -s https://packagecloud.io/install/repositories/varnishcache/varnish72/script.rpm.sh | bash

Install Varnish Cache

After the repository is added, now install the Varnish on AlmaLinux 9 using the following command below:

sudo dnf install varnish

Start and Enable Varnish Cache

Once the installation is complete, now enable Varnish (to start automatically upon system boot), and verify the status using the commands below:

# sudo systemctl start varnish
# sudo systemctl enable varnish
# sudo systemctl status varnish
Output
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
     Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; vendor p>
     Active: active (running) since Tue 2022-11-29 04:02:24 EST; 9s ago
   Main PID: 73137 (varnishd)
      Tasks: 217
     Memory: 93.7M
        CPU: 1.347s
     CGroup: /system.slice/varnish.service
...

Configure Varnish Cache on AlmaLinux 9

Here you must configure Varnish to listen on port 80. You can do it by editing /usr/lib/systemd/system/varnish.service file:

sudo vi /usr/lib/systemd/system/varnish.service

Find the ExecStart line and add the configuration to change port 6081 to 80 as shown below:

...
ExecStart=/usr/sbin/varnishd \
          -a :80 \
          -a localhost:8443,PROXY \
          -f /etc/varnish/default.vcl \
          -P %t/%N/varnishd.pid \
...

When you are done save and close the file.

Then, restart the Varnish Cache on AlmaLinux 9 with the following command:

# sudo systemctl daemon-reload
# sudo systemctl restart varnish

Now you can test your installation.

Test Varnish Installation on AlmaLinux 9

From your terminal run the following command:

curl -I http://localhost/test.html

In your output you should see the following:

Output
HTTP/1.1 200 OK
Date: Tue, 29 Nov 2022 09:06:27 GMT
Server: Apache/2.4.53 (AlmaLinux)
Last-Modified: Tue, 29 Nov 2022 08:56:11 GMT
ETag: "0-5ee9828ded2e1"
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 (Varnish/7.2)
Accept-Ranges: bytes
Connection: keep-alive

This means that your Varnish Cashe is active and running on your server.

You can test it from your local workstation by substituting your instance’s IP address.

Linux:

curl -I http://192.0.2.123/test.html

Windows PowerShell:

PS> curl -Uri http://192.0.2.123/test.html

Note: Verify the Varnish headers will appear.

Conclusion

At this point, you have learned to Install Varnish Cache with Apache on AlmaLinux 9.

Hope you enjoy it. You may like these articles:

Install Varnish Cache with Nginx on AlmaLinux 9

Install Varnish Cache with Apache on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!