How to install the Varnish cache for faster web page loads on AlmaLinux

How to install the Varnish cache for faster web page loads on AlmaLinux

If you’re looking to push the page load speeds for your Apache web server on AlmaLinux, the Varnish caching server might be exactly what you need. Jack Wallen walks you through the installation and setup.

Linux logo

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

SEE: 5 programming languages network architects should learn (free PDF) (TechRepublic)

Varnish cache works by caching content in memory, so page load time is not only reduced, it also helps with your Search Engine Results Page (SERP). In the end, this will improve the user experience on your site. That’s a win-win.

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

What you’ll need

To successfully install Varnish, you’ll need a running instance of AlmaLinux and a user with sudo privileges. That’s it. Let’s make some caching magic.

How to install Varnish

The first thing we need to do is disable the installation of the version of Varnish found in the AlmaLinux default repository. To do that, log into your server and issue the command:

sudo dnf module disable varnish -y

Next, we must 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 configure 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 that to port 80. Look for 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 you don’t already have Apache installed, do so with:

sudo dnf install httpd -y

Start and enable 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

Change that line to:

Listen 8080

You’ll also need to change all listening ports within all virtual host configuration files for each site or application that must be cached via Varnish.

Save and close the file.

Restart Apache with:

sudo systemctl restart httpd

How to verify Varnish is working

To verify Varnish is working, issue 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 caching for your Apache server. You can also run the varnishstat command to view the stats of your Varnish caching server.

And that’s all there is to installing the Varnish caching server for Apache on AlmaLinux. Enjoy that speedier page loading.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Source of Article