Are you looking to enhance the speed and performance of your website? Look no further than Varnish Cache, an open-source HTTP accelerator that can significantly improve the loading time of your web pages. In this comprehensive guide, we will walk you through the step-by-step process of installing and using Varnish Cache on Debian 11. By the end of this article, you will have a solid understanding of how Varnish Cache works, how to integrate it with Apache, and how to configure it for optimal performance. Let’s dive in!
Requirements
Before we begin, make sure you have the following:
- A server running Debian 11.
- Root access to the server.
Install and Configure Apache Web Server
To get started, we need to install the Apache web server on your Debian 11 server. Open your terminal and run the following command:
sudo apt install apache2 -y
After the installation is complete, we need to make a few configuration changes. First, let’s change the default port that Apache listens on. Open the Apache configuration file using the following command:
sudo nano /etc/apache2/ports.conf
Locate the line that says Listen 80
and replace it with Listen 8080
. Save the file and exit the editor.
Next, we need to update the default virtual host configuration file. Open the file using the following command:
sudo nano /etc/apache2/sites-available/000-default.conf
Look for the line <VirtualHost *:80>
and replace it with <VirtualHost *:8080>
. Save the file and exit the editor.
To apply the changes, restart the Apache service:
sudo systemctl restart apache2
Verify that Apache is now listening on port 8080 with the following command:
sudo ss -antpl | grep apache2
You should see the output confirming that Apache is listening on port 8080.
Install Varnish Server on Debian 11
By default, the latest version of Varnish Cache is not available in the Debian default repository. We need to add the Varnish repository to APT. Let’s start by installing the required dependencies:
sudo apt install debian-archive-keyring curl gnupg apt-transport-https -y
Next, add the Varnish GPG key:
curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey | gpg --dearmor -o /etc/apt/trusted.gpg.d/varnish.gpg
Now, create a Varnish source file:
sudo nano /etc/apt/sources.list.d/varnishcache_varnish70.list
Add the following line to the file:
deb https://packagecloud.io/varnishcache/varnish70/debian/ bullseye main deb-src https://packagecloud.io/varnishcache/varnish70/debian/ bullseye main
Save the file and close the editor.
Update the repository cache:
sudo apt update -y
Finally, install Varnish Cache:
sudo apt install varnish -y
Once the installation is complete, start the Varnish server:
sudo systemctl restart varnish
To verify the Varnish version, run the following command:
varnishd -V
You should see the Varnish version displayed.
Configure Varnish Server
Now that Varnish Cache is installed, we need to configure it to work with Apache. Open the default VCL (Varnish Configuration Language) file:
sudo nano /etc/varnish/default.vcl
In this file, you need to define the backend server. Look for the following lines:
backend default { .host = "127.0.0.1"; .port = "8080"; }
Make sure the .host
and .port
values match the IP address and port number of your Apache server. Save the file and exit the editor.
Configure Varnish to Work with Apache
To configure Varnish to work with Apache, we need to create a custom service configuration file. Create the necessary directory:
sudo mkdir /etc/systemd/system/varnish.service.d
Open the custom service configuration file:
sudo nano /etc/systemd/system/varnish.service.d/customport.conf
Add the following lines:
[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m
Save the file and exit the editor.
Reload the systemd daemon to apply the changes:
sudo systemctl daemon-reload
Restart the Varnish service:
sudo systemctl restart varnish
To check the status of the Varnish Cache, run the following command:
sudo systemctl status varnish
You should see the status indicating that Varnish is active and running.
Verify Varnish Cache
At this point, Varnish Cache is installed and running. We can now verify its functionality by making a request to our server using the CURL command:
curl -I http://localhost/
You should receive a response similar to the following:
HTTP/1.1 200 OK
Date: Fri, 30 Dec 2022 05:33:00 GMT
Server: Apache/2.4.54 (Debian)
Last-Modified: Fri, 30 Dec 2022 04:42:33 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/7.0)
ETag: W/"29cd-5f1043adffc4c-gzip"
Accept-Ranges: bytes
Connection: keep-alive
Congratulations! You have successfully installed and configured Varnish Cache with Apache on Debian 11. Varnish Cache will now act as a front-end server, caching and serving content to dramatically improve the performance of your website. If you have any questions or need further assistance, feel free to reach out. Happy caching!