How to Install Varnish Cache for Faster Web Page Loads on AlmaLinux

If you are looking to increase the page load speed of your Apache web server on AlmaLinux, the Varnish caching server might be just what you need. Jack Wallen walks you through installation and configuration.

The Varnish cache tool is capable of dramatically speeding up your web page loading by a factor of 10x to 300x. If your sites are in high demand, you’ll want them to load as quickly as possible. For that purpose, you should definitely consider this open source caching HTTP reverse proxy.

TO SEE: 5 Programming Languages ​​Network Architects Should Learn (Free PDF) (TechRepublic)

Cache Varnish works by caching content in memory, so page load time is not only reduced, but it also helps with your search engine results page (SERP). Ultimately, this will improve the user experience on your site. It’s a win-win.

I want to walk you through the process of installing Varnish to serve Apache web pages on AlmaLinux.

What you will need

To successfully install Varnish, you will need a running instance of AlmaLinux and a user with sudo privileges. That’s it. Let’s do cache magic.

How to install varnish

The first thing we need to do is disable the installation of the version of Varnish found in the default AlmaLinux repository. To do this, connect to your server and run the command:

sudo dnf module disable varnish -y

Next, we need to install the EPEL repository with:

sudo dnf install epel-release -y

Add the Varnish cache by downloading and running a simple script with the following commands:

wget https://packagecloud.io/install/repositories/varnishcache/varnish70/script.rpm.sh

sudo bash script.rpm.sh

Finally, install Varnish with:

sudo dnf install varnish -y

Start and enable the Varnish service with:

sudo systemctl enable --now varnish

How to set up varnish

We can now configure Varnish. Open the configuration file with:

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

The default port for Varnish is 6081. We want to change it to port 80. Find the block:

ExecStart=/usr/sbin/varnishd

-a :6081

-a localhost:8443,PROXY

-p feature=+http2

-f /etc/varnish/default.vcl

-s malloc,256m

ExecReload=/usr/sbin/varnishreload

Change 6081 to 80 in that section.

Save and close the file. Reload the systemctl daemon with:

sudo systemctl daemon-reload

Restart Varnish with:

sudo systemctl restart varnish

How to Configure Varnish for Apache

If Apache is not already installed, do it with:

sudo dnf install httpd -y

Start and activate the service with:

sudo systemctl enable --now httpd

Enable http traffic through the firewall with:

sudo firewall-cmd --zone=public --permanent --add-service=http

sudo firewall-cmd --reload

Open the Apache configuration file with:

sudo nano /etc/httpd/conf/httpd.conf

Look for the line:

Listen 80

Replace this line with:

Listen 8080

You will also need to modify all listening ports in all virtual host configuration files for each site or application that needs to be cached through Varnish.

Save and close the file.

Restart Apache with:

sudo systemctl restart httpd

How to check that the varnish is working

To verify that Varnish is working, run the command:

curl -I http://SERVER

Where SERVER is the IP address or domain of the hosting server. You should see output that includes:

X-Varnish: 2

Age: 0

Via: 1.1 varnish (Varnish/7.0)

Accept-Ranges: bytes

Connection: keep-alive

If you see Varnish listed (as you do above), everything is working and Varnish is cached for your Apache server. You can also run the varnishstat command to view your Varnish caching server statistics.

And that’s all there is to installing Varnish Caching Server for Apache on AlmaLinux. Take advantage of this faster page loading.

Subscribe to TechRepublic How to make technology work on YouTube for all the latest tech tips for professionals from Jack Wallen.

Comments are closed.