Install and Configure Varnish Cache for Apache on AlmaLinux 8

In this guide, we intend to teach you to Install and Configure Varnish Cache for Apache on AlmaLinux 8.

Varnish cache is a web application accelerator also known as caching HTTP reverse proxy. It acts more like a middleman between your client (i.e. user) and your web server. That means, instead of your web server directly listening to requests for specific content all the time, Varnish will assume the responsibility.

Install and Configure Varnish Cache for Apache on AlmaLinux 8

To install Varnish Cache, 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 follow our guide the Initial Server Setup with AlmaLinux 8.

Also, you need to have an Apache web server installed. To do this, you can follow our guide on How To Install an Apache web server on AlmaLinux 8.

When you are done, follow these steps to install Varnish Cache on AlmaLinux 8.

Apache configuration on AlmaLinux 8

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:

Listen 8080

When you are finished save and close the file.

Now restart Apache 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: Sun, 19 Jun 2022 08:46:54 GMT
Server: Apache/2.4.37 (AlmaLinux)
Last-Modified: Sun, 19 Jun 2022 08:46:44 GMT
ETag: "0-5e1c906edb154"
Accept-Ranges: bytes
Content-Type: text/html; charset=UTF-8

Now you can start to install Varnish on AlmaLinux 8.

How To Install and Configure Varnish on AlmaLinux 8

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

By default, Varnish is available on the AlmaLinux 8 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/varnish70/script.rpm.sh | bash

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

sudo dnf install varnish

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 pre>
Active: active (running) since Sun 2022-06-19 04:58:23 EDT; 15s ago
Main PID: 90410 (varnishd)
Tasks: 217
Memory: 93.1M
CGroup: /system.slice/varnish.service
├─90410 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s m>
└─90420 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s m>

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 -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,2g

When you are done save and close the file.

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

$ sudo systemctl daemon-reload
$ sudo systemctl restart varnish

Now you can test your installation.

Test Varnish Installation on AlmaLinux 8

From your console run the following command:

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

In your output you should see:

Output
HTTP/1.1 200 OK
Date: Sun, 19 Jun 2022 09:03:13 GMT
Server: Apache/2.4.37 (AlmaLinux)
Last-Modified: Sun, 19 Jun 2022 08:46:44 GMT
ETag: "0-5e1c906edb154"
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.0)
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 learn to Install and Configure Varnish Cache for Apache on AlmaLinux 8.

I hope you enjoy using it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!