Install Varnish Cache for Apache on Rocky Linux 8

In this guide, we want to teach you How To Install Varnish Cache for Apache on Rocky Linux 8.

Varnish is a reverse proxy that makes a copy of the page in the memory to serve it out when the page will be re-visited. The copy will be based on request headers like URL and Request Method. This is known as Full Page Cache.

Varnish Cache ensures pages of webshops do not have to be loaded over again. The performance and page load will benefit a lot from Varnish Cache. And that is precisely what you want: a lightning-fast webshop for all your visitors.

How To Install Varnish Cache for Apache on Rocky Linux 8

To install Varnish Cache on your server, you need some requirements.

Requirements

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 Rocky Linux 8.

Because you want to install Varnish for Apache, you must have the Apache web server installed on your server. You can follow this guide on How To Install Apache on Rocky Linux 8.

Now follow the steps below to complete this guide.

Configure Apache To Work with Varnish Cache

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 done, save and close the file.

At this point, 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: Sat, 23 July 2022 08:46:54 GMT
Server: Apache/2.4.37 (Rocky Linux)
Last-Modified: Sat, 23 July 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 Rocky Linux 8.

Install and Configure Varnish on Rocky Linux 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 Rocky Linux 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 Sat 2022-07-23 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>

At this point, 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 Rocky Linux 8 with the following command:

$ sudo systemctl daemon-reload
$ sudo systemctl restart varnish

Now you can test your installation.

Testing Varnish Cache

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: Sat, 23 July 2022 09:03:13 GMT
Server: Apache/2.4.37 (Rocky Linux)
Last-Modified: Sat, 23 July 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.

For more information, you can visit the Varnish Documentation page.

Conclusion

At this point, you learn to Install Varnish Cache for Apache on Rocky Linux 8.

There are plenty of ways to make your website scale, most of which require a considerable amount of time and money. Luckily, a decent caching strategy can reduce the impact of all that traffic. Varnish is a tool that can cache your HTTP traffic and take most of the load off your servers.

Hope you enjoy it.

Also, you may be interested in these articles:

Install and Configure Apache Tomcat on Rocky Linux 8

Secure Nginx with Let’s Encrypt on Rocky Linux 8

How To Set up SVN Server on Rocky Linux 8

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!