Varnish is a free, and open-source web application accelerator used for caching website content in memory. It is designed for HTTP to speed up caching of heavy dynamic websites. It is capable of speeding up your website page loading time by a factor of 10x to 300x. This will helps you with your Search Engine Results Page and also improve the user experience on your website.
In this post, we will show you how to install Varnish 7 on AlmaLinux.
Prerequisites
- An AlmaLinux VPS with root access enabled or a user with sudo privileges.
Log in via SSH and Update your System
First, you will need to log in to your AlmaLinux VPS via SSH as the root user:
ssh root@IP_ADDRESS -p PORT_NUMBER
Next, run the following commands to upgrade all installed packages on your VPS:
dnf update -y
Once all the packages are updated, restart your system to apply the changes.
Add Varnish Repo
By default, the latest version of Varnish is not included in the AlmaLinux default repo. So you will need to disable the default Varnish repo. You can disable it with the following command:
dnf module disable varnish
You will get the following output:
AlmaLinux - AppStream 5.6 MB/s | 9.7 MB 00:01 AlmaLinux - BaseOS 8.8 MB/s | 6.8 MB 00:00 AlmaLinux - Extras 28 kB/s | 12 kB 00:00 Dependencies resolved. ============================================================================================================================================== Package Architecture Version Repository Size ============================================================================================================================================== Disabling modules: varnish Transaction Summary ============================================================================================================================================== Is this ok [y/N]: y Complete!
Next, install the EPEL repository and add the Varnish 7 repo with the following command:
dnf install epel-release -y curl -s https://packagecloud.io/install/repositories/varnishcache/varnish70/script.rpm.sh | bash -
Install Varnish 7
Now, you can run the following command to install the Varnish 7 on your server.
dnf install varnish -y
Once the Varnish is installed, you can verify the Varnish version using the following command:
rpm -qi varnish
You will get the Varnish package information in the following output:
Name : varnish Version : 7.0.2 Release : 1.el8 Architecture: x86_64 Install Date: Wednesday 06 April 2022 03:21:17 PM UTC Group : System Environment/Daemons Size : 8907085 License : BSD Signature : (none) Source RPM : varnish-7.0.2-1.el8.src.rpm Build Date : Wednesday 12 January 2022 02:25:34 PM UTC Build Host : 7fc509e75620 Relocations : (not relocatable) URL : https://www.varnish-cache.org/ Summary : High-performance HTTP accelerator Description : This is Varnish Cache, a high-performance HTTP accelerator.
Manage Varnish Service
By default, the Varnish service is managed by systemd. You can start the Varnish service with the following command:
systemctl start varnish
To enable the Varnish service, run the following command:
systemctl enable varnish
To check the status of the Varnish service, run the following command:
systemctl status varnish
You will get the following output:
● varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/usr/lib/systemd/system/varnish.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2022-04-06 15:21:40 UTC; 6s ago Process: 25005 ExecStart=/usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (> Main PID: 25006 (varnishd) Tasks: 217 Memory: 108.5M CGroup: /system.slice/varnish.service ├─25006 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m └─25017 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m Apr 06 15:21:39 rockylinux systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator... Apr 06 15:21:40 rockylinux varnishd[25006]: Version: varnish-7.0.2 revision 9b5f68e19ca0ab60010641e305fd12822f18d42c Apr 06 15:21:40 rockylinux varnishd[25006]: Platform: Linux,4.18.0-348.12.2.el8_5.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Apr 06 15:21:40 rockylinux varnishd[25006]: Child (25017) Started Apr 06 15:21:40 rockylinux varnishd[25006]: Child (25017) said Child starts Apr 06 15:21:40 rockylinux systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
Configure Varnish
By default, Varnish listens on port 6081. You can check it with the following command:
ss -antpl | grep varnish
You will get the following output:
LISTEN 0 128 0.0.0.0:6081 0.0.0.0:* users:(("cache-main",pid=25017,fd=6),("varnishd",pid=25006,fd=6)) LISTEN 0 128 [::]:6081 [::]:* users:(("cache-main",pid=25017,fd=7),("varnishd",pid=25006,fd=7))
Now, you will need to configure Varnish to listen on port 80. You can do it by editing the Varnish configuration file:
nano /usr/lib/systemd/system/varnish.service
Replace the port 6081 with 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:
systemctl daemon-reload
Next, you will also need to edit the Varnish VCL configuration file and define your Nginx backend.
nano /etc/varnish/default.vcl
Define your Nginx backend as shown below:
backend default { .host = "127.0.0.1"; .port = "8080"; }
Save and close the file when you are finished.
Note: Replace 127.0.0.1 with your Nginx web server IP.
Configure Varnish for Nginx
First, install the Nginx web server package with the following command:
dnf install nginx -y
Once the Nginx is installed, you will need to edit the Nginx configuration file and change the listen port from 80 to 8080:
nano /etc/nginx/nginx.conf
Change the default port from 80 to 8080:
listen 8080 default_server; listen [::]:8080 default_server;
Save and close the file then restart the Nginx service to apply the changes:
systemctl restart nginx
Finally, restart the Varnish service to apply the changes:
systemctl restart varnish
Verify Varnish
At this point, Varnish is installed and configured to work with Nginx. Now it’s time to verify it.
You can verify it using the curl command as shown below:
curl -I http://your-server-ip
If everything is fine, you will get the following output:
HTTP/1.1 200 OK Server: nginx/1.14.1 Date: Wed, 06 Apr 2022 15:25:48 GMT Content-Type: text/html Content-Length: 3429 Last-Modified: Thu, 10 Jun 2021 09:09:03 GMT ETag: "60c1d6af-d65" X-Varnish: 2 Age: 0 Via: 1.1 varnish (Varnish/7.0) Accept-Ranges: bytes Connection: keep-alive
Of course, if you are one of our Linux VPS hosting customers, you don’t have to install Varnish 7 on your AlmaLinux VPS – simply ask our admins, sit back, and relax. Our admins will install Varnish 7 on AlmaLinux for you immediately.
PS. If you liked this post about how to install Varnish 7 on AlmaLinux VPS, please share it with your friends on the social networks using the buttons below, or simply leave a comment in the comments section. Thanks.