Varnish is a powerful web accelerator that is used to cache static content such as images, CSS, and JavaScript. It acts as a reverse proxy and significantly improves the performance and speed of web applications. In this tutorial, we will guide you through the process of how to install and configure Varnish on Rocky Linux.
How to Install and Configure Varnish on Rocky Linux
Prerequisites
- A Rocky Linux server with root access or a user with sudo privileges
- Apache or Nginx web server installed on the server. You can check our guides on how to install Apache on Rocky Linux or how to install Nginx on Rocky Linux.
Install Varnish on Rocky Linux
First, update your system packages:
sudo dnf update -y
Next, install the EPEL repository to get access to Varnish packages:
sudo dnf install epel-release -y
Now, install Varnish using the following command:
sudo dnf install varnish -y
After the installation is complete, start and enable the Varnish service:
sudo systemctl start varnish
sudo systemctl enable varnish
Verify that Varnish is running:
sudo systemctl status varnish
Configure Varnish on Rocky Linux
By default, Varnish listens on port 6081. You need to change this to port 80, so it can handle incoming web requests. Open the Varnish configuration file /etc/varnish/varnish.params
:
sudo nano /etc/varnish/varnish.params
Locate the following line:
# VARNISH_LISTEN_PORT=6081
Uncomment it and change the port to 80:
VARNISH_LISTEN_PORT=80
Save and close the file.
Next, open the Varnish VCL configuration file /etc/varnish/default.vcl
:
sudo nano /etc/varnish/default.vcl
Locate the following lines:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
If you are using Apache as your web server, change the .port
value to 8080
. If you are using Nginx, change the value to 8080
. Save and close the file.
Configure your Web Server on Rocky Linux
For Apache:
Edit the Apache configuration file:
sudo nano /etc/httpd/conf/httpd.conf
Locate the following line:
Listen 80
Change it to:
Listen 8080
Save and close the file. Then, restart Apache and Varnish:
sudo systemctl restart httpd
sudo systemctl restart varnish
For Nginx:
Edit the Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
Locate the following line:
listen 80;
Change it to:
listen 8080;
Save and close the file. Then, restart Nginx and Varnish:
sudo systemctl restart nginx
sudo systemctl restart varnish
Test Varnish on Rocky Linux
To test if Varnish is working, you can use the curl
command with the -I
flag to display only the header information:
curl -I http://your_server_ip:80
You should see an output similar to the following, which indicates that Varnish is serving the content:
HTTP/1.1 200 OK
Date: Fri, 28 Apr 2023 12:00:00 GMT
Server: Varnish
X-Varnish: 123456
Content-Type: text/html; charset=UTF-8
X-Cache: HIT
Take note of the X-Varnish
and X-Cache
headers. If X-Cache
shows a “HIT,” Varnish is successfully caching the content. If it shows a “MISS,” the content is not being cached. You may need to reload the page a few times to see the “HIT” status, as Varnish caches content upon request.
Configure Varnish to start on boot
To ensure that Varnish starts automatically whenever your server reboots, enable it using the following command:
sudo systemctl enable varnish
Congratulations! You’ve successfully installed and configured Varnish on your Rocky Linux server. Varnish should now be improving the performance of your website by caching static content and serving it to users at a much faster rate.
Additional Tips
- Monitor Varnish performance: Use the Varnishstat tool to monitor your Varnish cache performance. This tool provides real-time statistics on cache hits, misses, and other essential metrics.
- Tune Varnish for optimal performance: Depending on your server resources and website requirements, you may need to adjust Varnish’s default settings. The Varnish documentation provides detailed information on various settings and configurations.
- Secure your Varnish setup: If you’re using Varnish in combination with HTTPS, consider implementing an SSL termination proxy like Nginx or HAProxy. This will help you secure the connection between your users and the Varnish cache server.
Conclusion
In this article, you learned how to install and configure Varnish on Rocky Linux. By caching and serving static content, Varnish can significantly enhance your website’s performance, providing a better experience for your users. For further information and advanced configurations, consult the official Varnish documentation.
Looking for more ways to optimize your Linux server? Check out our other tutorials: