In this tutorial, we will show you how to install Varnish on AlmaLinux 8. For those of you who didn’t know, Varnish cache is an open-source, high-performance HTTP accelerator designed for speeding up web servers. Varnish exclusively focuses on HTTP as compared to other proxy servers which focus on FTP, SMTP, and other network protocols. It is purely designed to speed up caching of heavy dynamic websites. Varnish sits between Nginx and clients that receive requests from clients and forwards them to the backend. This will improve the webserver performance because Varnish will serve content from memory.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of the Varnish cache on AlmaLinux 8. You can follow the same instructions for CentOS and Rocky Linux.
Prerequisites
- A server running one of the following operating systems: AlmaLinux 8, CentOS, and Rocky Linux 8.
- It’s recommended that you use a fresh OS install to prevent any potential issues
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Varnish on AlmaLinux 8
Step 1. First, let’s start by ensuring your system is up-to-date.
sudo dnf update sudo dnf install epel-release sudo dnf install dnf-plugins-core
Step 2. Installing Varnish 7 on AlmaLinux 8.
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
Step 3. Configure Varnish Cache.
Now we need to configure Varnish to listen on port 80. You can do it by editing /usr/lib/systemd/system/varnish.service
file:
nano /usr/lib/systemd/system/varnish.service
Add the configuration to change the 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
Save and close the file then reload the systemd
daemon to apply the changes:
sudo systemctl daemon-reload sudo systemctl restart varnish
Step 4. Configuring Nginx to Work with Varnish Cache.
First, install the Nginx package with the following command below:
sudo dnf install nginx
Next, edit the Nginx main configuration file using the following command:
nano /etc/nginx/nginx.conf
Then changing the listening port to 8080:
..... server { listen 8080 default_server; listen [::]:8080 default_server; server_name _; root /usr/share/nginx/html; .....
Save and close the file when you are finished then start the Nginx service to apply the changes:
sudo systemctl restart nginx
Step 5. Configure Firewall.
By default AlmaLinux 8 includes a fully locked down firewall. You must open access to the HTTP service in the firewall to allow users to access websites or applications running over HTTP, and also reload the firewalld settings to apply the new changes:
sudo firewall-cmd --zone=public --permanent --add-service=http sudo firewall-cmd --reload
Step 6. Testing Varnish Cache.
Once successfully installed, Varnish cache is enabled and working with Nginx service with the curl
command below:
curl -I http://your-server-IP-address
Output:
HTTP/1.1 200 OK Server: nginx/1.14.1 Date: Mon, 19 Feb 2022 05:39:06 GMT Content-Type: text/html Content-Length: 3429 Last-Modified: Thu, 20 May 2021 18:36:03 GMT ETag: "60gdt6af-d65" X-Varnish: 2 Age: 0 Via: 1.1 varnish (Varnish/7.0) Accept-Ranges: bytes Connection: keep-alive
Congratulations! You have successfully installed Varnish. Thanks for using this tutorial for installing the Varnish HTTP Cache on your AlmaLinux 8 system. For additional help or useful information, we recommend you check the official Varnish website.