How To Install Varnish on Debian 12
In this tutorial, we will show you how to install Varnish on Debian 12. Varnish is a web application accelerator designed to improve the performance of web servers by caching frequently accessed content. By serving cached content directly from memory, it drastically reduces server load, improving response times and, ultimately, enhancing user experience.
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 HTTP cache on a Debian 12 (Bookworm).
Prerequisites
- A server running one of the following operating systems: Debian 12 (Bookworm).
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- Make sure your Debian 12 system is connected to the internet. An active connection is essential for downloading the required packages and updates during the installation.
- 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 Debian 12 Bookworm
Step 1. The first step to a smooth Varnish installation is ensuring that your Debian 12 system is up to date. This prevents conflicts and ensures compatibility. Open your terminal and execute the following commands:
sudo apt update sudo apt upgrade
Step 2. Installing Essential Tools.
To install the necessary tools, run the following commands:
sudo apt install curl sudo apt install apt-transport-https
Step 3. Installing Varnish on Debian 12.
- Method 1: Installing Varnish from the Debian Repository
Install Varnish by running the following command:
sudo apt install varnish
Once the installation is complete, start the Varnish service by running the following command:
sudo systemctl start varnish
To check the status of the Varnish service, run the following command:
sudo systemctl status varnish
To verify your Varnish installation and check its version, use the following command:
varnishd -V
- Method 2: Installing Varnish from the Varnish Cache Repository
Add the Varnish Cache repository to your system by running the following command:
curl -s https://packagecloud.io/install/repositories/varnishcache/varnish74/script.deb.sh | sudo bash
Update the package list by running the following command:
sudo apt update
Now install Varnish by running the following command:
sudo apt install varnish=7.4.1-1~bookworm
Step 3. Configuring Varnish.
Varnish operates based on the Varnish Configuration Language (VCL). Understanding and configuring this language is key to optimizing your web server’s performance.
Let’s create a custom VCL configuration file to tailor Varnish to your specific needs. This is where you define your caching rules and backend server settings:
sudo nano /etc/varnish/default.vcl
In this file, you can define your backend server and caching rules. Here’s a basic example:
backend default { .host = "127.0.0.1"; .port = "8080"; } sub vcl_recv { # Define caching rules here } sub vcl_fetch { # Modify server responses here }
Remember to save your changes. This is just a starting point; you can customize the configuration further based on your requirements.
Once your custom VCL configuration is in place, you need to load it into Varnish. Use the following command:
sudo systemctl reload varnish
You can verify the VCL configuration by running the following command:
varnishd -C -f /etc/varnish/default.vcl
Step 4. Testing Varnish with a Simple Website.
Create a basic HTML file to test Varnish caching. Replace ‘your_website_content
‘ with your website’s content:
echo "your_website_content" | sudo tee /var/www/html/index.html
Now, use ‘curl’ to check if Varnish is serving the cached content:
curl -I http://localhost
Congratulations! You have successfully installed Varnish. Thanks for using this tutorial to install the latest version of the Varnish HTTP cache on Debian 12 Bookworm. For additional help or useful information, we recommend you check the official Varnish website.