How to Install Varnish 7 for Nginx on Rocky Linux 8

Varnish cache is a free and open-source cache accelerator used to speed up your website by caching the content in memory. It is extremely fast and reduces the page load time, which helps with your SERPs and improves user experience. It is used by many organizations including Facebook, Twitter, and Wikipedia. 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.

In this tutorial, I will show you how to install Varnish 7 in front of Nginx on Rocky Linux 8.

Prerequisites

  • A server running Rocky Linux 8.
  • A root password is configured on the server.

Install Varnish 7

Before starting, you will need to disable the existing varnish modules from your system. You can disable it by running the following command:

dnf module disable varnish

You should see the following output:

==============================================================================================================================================
 Package                           Architecture                     Version                           Repository                         Size
==============================================================================================================================================
Disabling modules:
 varnish                                                                                                                                     

Transaction Summary
==============================================================================================================================================

Is this ok [y/N]: y
Complete!

Next, install the EPEL repository using the following command:

dnf install epel-release -y

Next, add Varnish Cache 7 repository to your system using the following command:

curl -s https://packagecloud.io/install/repositories/varnishcache/varnish70/script.rpm.sh | bash

Once the repository is added, install the Varnish 7 using the following command:

dnf install varnish -y

After the successful installation, verify the information about Varnish with the following command:

rpm -qi varnish

You should see the following output:

Name        : varnish
Version     : 7.0.2
Release     : 1.el8
Architecture: x86_64
Install Date: Sunday 13 February 2022 06:24:35 AM 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

After installing Varnish, start the Varnish service and enable it to start at system reboot using the following command:

systemctl start varnish
systemctl enable varnish

You can check the status of the Varnish with 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 Sun 2022-02-13 06:24:56 UTC; 6s ago
  Process: 2555 ExecStart=/usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (c>
 Main PID: 2556 (varnishd)
    Tasks: 217
   Memory: 104.5M
   CGroup: /system.slice/varnish.service
           ??2556 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m
           ??2567 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m

Feb 13 06:24:55 rockylinux systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Feb 13 06:24:55 rockylinux varnishd[2556]: Version: varnish-7.0.2 revision 9b5f68e19ca0ab60010641e305fd12822f18d42c
Feb 13 06:24:55 rockylinux varnishd[2556]: Platform: Linux,4.18.0-348.12.2.el8_5.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Feb 13 06:24:55 rockylinux varnishd[2556]: Child (2567) Started
Feb 13 06:24:56 rockylinux varnishd[2556]: Child (2567) said Child starts
Feb 13 06:24:56 rockylinux systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

Configure Varnish

By default, Varnish listens on port 6081. Here, we will use Varnish to accept incoming HTTP connections. So you will 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

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:

systemctl daemon-reload

Next, restart the Varnish service to apply the changes:

systemctl restart varnish

Next, verify the Varnish listening port using the following command:

ss -antpl | grep varnish

You should see the Varnish listening port in the following output:

LISTEN 0      10         127.0.0.1:46093      0.0.0.0:*    users:(("varnishd",pid=2819,fd=13))                            
LISTEN 0      128          0.0.0.0:80         0.0.0.0:*    users:(("cache-main",pid=2830,fd=6),("varnishd",pid=2819,fd=6))
LISTEN 0      128        127.0.0.1:8443       0.0.0.0:*    users:(("cache-main",pid=2830,fd=9),("varnishd",pid=2819,fd=9))
LISTEN 0      10             [::1]:33963         [::]:*    users:(("varnishd",pid=2819,fd=12))                            
LISTEN 0      128             [::]:80            [::]:*    users:(("cache-main",pid=2830,fd=7),("varnishd",pid=2819,fd=7))
LISTEN 0      128            [::1]:8443          [::]:*    users:(("cache-main",pid=2830,fd=8),("varnishd",pid=2819,fd=8))

Configure Nginx to work with Varnish

In this section, we will install Nginx and configure it to work with Varnish. First, install the Nginx package with the following command:

dnf install nginx -y

After installing Nginx, you will need to edit the Nginx configuration file and change the Nginx default listening port from 80 to 8080.

Edit the Nginx main configuration file using the following command:

nano /etc/nginx/nginx.conf

Find the following lines:

	listen       80 default_server;
        listen       [::]:80 default_server;

And, replaced them with the following lines:

	listen       8080 default_server;
        listen       [::]:8080 default_server;

Save and close the file when you are finished then start the Nginx service to apply the changes:

systemctl start nginx

You can also check the Nginx status using the following command:

systemctl status nginx

You will get the following output:

? nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-02-13 06:29:02 UTC; 8s ago
  Process: 5627 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 5626 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 5624 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 5629 (nginx)
    Tasks: 2 (limit: 11412)
   Memory: 6.7M
   CGroup: /system.slice/nginx.service
           ??5629 nginx: master process /usr/sbin/nginx
           ??5630 nginx: worker process

Feb 13 06:29:02 rockylinux systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 13 06:29:02 rockylinux nginx[5626]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 13 06:29:02 rockylinux nginx[5626]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 13 06:29:02 rockylinux systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 13 06:29:02 rockylinux systemd[1]: Started The nginx HTTP and reverse proxy server.

Verify Varnish Cache

At this point, Varnish is installed and configured with the Nginx web server. Now, it's time to test the Varnish cache.

Run the following command to verify the Varnish cache:

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: Sun, 13 Feb 2022 06:30:06 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

Conclusion

Congratulations! you have successfully installed Varnish with Nginx on Rocky Linux 8. You can now implement Varnish in the production environment to speed up your website performance. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

2 Comment(s)

Add comment

Comments

By: Milan at: 2022-02-22 13:31:09

SSL is now important for every website. How to add SSL for Varnish server?

By: Jake at: 2022-02-22 14:57:56

Varnish does not support SSL. That's why SSL termination happens in Nginx. Alternatively, you can use Hitch in conjunction with varnish to provide SSL termination.