Atlantic.Net Blog

How to Install Varnish Cache with Nginx on Fedora

Varnish is a popular, free, open-source, caching solution used to speed up your web server. It saves the frequently accessed content in memory and serves it from the cache instead of being processed by the web server. Varnish is a web application accelerator and is also used as caching HTTP reverse proxy. It offers very useful features such as Gzip compression, private CDN, HTTP streaming pass & fetch, and more.

In this tutorial, we will show you how to install Varnish Cache with Apache on Fedora.

Step 1 – Install Varnish Cache

By default, the Varnish Cache is included in the Fedora default repo. You can install it easily using the following command.

dnf install varnish -y

After installing Varnish, start and enable the Varnish service using the following command.

systemctl start varnish
systemctl enable varnish

You can verify the Varnish status with the following command.

systemctl status varnish

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 Sat 2023-05-20 05:04:41 EDT; 6s ago
    Process: 9365 ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
   Main PID: 9366 (varnishd)
      Tasks: 217
     Memory: 90.5M
        CPU: 750ms
     CGroup: /system.slice/varnish.service
             ├─9366 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m
             └─9386 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m

To verify the Varnish version, run the following command.

varnishd -V

Output.

varnishd (varnish-6.5.2 revision e7233b0ad2639043341819d19a8d2e418e94ce1b)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2020 Varnish Software

By default, Varnish listens on port 6081. You can check it with the following command.

netstat -tunlp | grep 6081

Output.

tcp        0      0 0.0.0.0:6081            0.0.0.0:*               LISTEN      9366/varnishd       
tcp6       0      0 :::6081                 :::*                    LISTEN      9366/varnishd       

Step 2 – Install Apache Web Server

You can install the Apache server with the following command.

dnf install httpd -y

After installing the Apache server, start and enable the Apache service with the following command.

systemctl start httpd
systemctl enable httpd

Step 3 – Change Apache Default Port

Next, you will need to change the Apache default port from 80 to 8080. You can change it with the following command.

nano /etc/httpd/conf/httpd.conf

Change the following line from 80 to 8080.

Listen 8080

Save and close the file, then restart the Apache service to apply the changes.

systemctl restart httpd

Now, verify the Apache listening port using the following command.

ss -antpl | grep httpd

Output.

LISTEN 0      511                *:8080             *:*    users:(("httpd",pid=9801,fd=4),("httpd",pid=9800,fd=4),("httpd",pid=9799,fd=4),("httpd",pid=9797,fd=4))

Step 4 – Change Varnish Default Port

Next, you will also need to edit the Varnish systemd file and change the Varnish port to 80.

systemctl edit --full varnish

Change the following line as shown below:

ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,1g  

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 

Step 5 – Verify Varnish

At this point, Varnish is installed and configured to listen on port 80. Now, when you make any web request, it will pass via the Varnish server.

You can verify the Varnish cache with the following command.

curl -I http://localhost

If everything is fine, you will see the following output.

HTTP/1.1 403 Forbidden
Date: Sat, 20 May 2023 09:09:44 GMT
Server: Apache/2.4.53 (Fedora) 
Last-Modified: Fri, 26 Mar 2021 17:49:58 GMT
ETag: "211a-5be742910bd80"
Accept-Ranges: bytes
Content-Length: 8474
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.5)
Connection: keep-alive

Conclusion

In this post, we will show you how to install Varnish with Apache on Fedora. You can now implement a Varnish cache on your web server to speed up the web page delivery. You can now deploy Varnish Cache on VPS hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year