Varnish Cache is a powerful HTTP accelerator designed to enhance the performance of high-traffic, dynamic websites. It serves as an intermediary between your client and server, efficiently handling incoming requests before they reach your web server’s backend. By caching copies of web pages served by the web server, Varnish delivers cached content directly to users, significantly reducing backend load, which is ideal for high-traffic websites with extensive product catalogs.
This guide will walk you through the installation and configuration of Varnish Cache with Apache on Ubuntu 22.04.
Requirements
- A server running Ubuntu 22.04.
- Root access configured on the server.
Install and Configure Apache Web Server
Ensure that the Apache web server is installed on your server. If not, use the following command to install it:
apt install apache2 -y
Next, modify the Apache configuration file to change the default listening port from 80 to 8080:
nano /etc/apache2/ports.conf
Locate the line:
Listen 80
And replace it with:
Listen 8080
Save the changes and proceed to edit the default virtual host configuration file:
nano /etc/apache2/sites-available/000-default.conf
Locate the line:
<VirtualHost *:80>
And replace it with:
<VirtualHost *:8080>
After saving the file, restart Apache to apply the changes:
systemctl restart apache2
Verify that Apache is listening on port 8080:
ss -antpl | grep 8080
You should see an output similar to:
LISTEN 0 511 *:8080 *:* users:(("apache2",pid=2553,fd=4),("apache2",pid=2552,fd=4),("apache2",pid=2551,fd=4))
Install Varnish Cache
Since the latest version of Varnish isn’t available in Ubuntu’s default repositories, you’ll need to add the Varnish repository manually.
First, install the required dependencies:
apt install debian-archive-keyring curl gnupg apt-transport-https -y
Then, add the Varnish GPG key:
curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/varnish.gpg
Create a Varnish source list:
nano /etc/apt/sources.list.d/varnishcache_varnish70.list
Insert the following:
deb https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main deb-src https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main
Save and create a configuration preferences file:
nano /etc/apt/preferences.d/varnish
Add these lines:
Package: varnish Pin: origin packagecloud.io Pin-Priority: 900
Update the repository cache:
apt update
Now, install Varnish Cache:
apt install varnish -y
After installation, edit the default.vcl file to define your backend server:
nano /etc/varnish/default.vcl
Update the following lines to match your backend server details:
backend default { .host = "127.0.0.1"; .port = "8080"; }
Configure Varnish to Work with Apache
To integrate Varnish with Apache, create a custom service configuration file for Varnish:
mkdir /etc/systemd/system/varnish.service.d nano /etc/systemd/system/varnish.service.d/customport.conf
Add the following configuration:
[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 reload systemd to apply the changes:
systemctl daemon-reload
Restart the Varnish service:
systemctl restart varnish
Check the Varnish Cache status:
systemctl status varnish
The expected output should confirm that Varnish is active and running:
? varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled) Drop-In: /etc/systemd/system/varnish.service.d ??customport.conf Active: active (running) since Tue 2022-10-18 13:07:44 UTC; 14s ago Process: 4968 ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (c>) Main PID: 4969 (varnishd) Tasks: 217 Memory: 90.6M CPU: 595ms CGroup: /system.slice/varnish.service ??4969 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m ??4983 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m
Verify that Varnish is listening on port 80:
ss -antpl | grep :80
Expected output:
LISTEN 0 1024 0.0.0.0:80 0.0.0.0:* users:(("cache-main",pid=4983,fd=3),("varnishd",pid=4969,fd=3)) LISTEN 0 1024 [::]:80 [::]:* users:(("cache-main",pid=4983,fd=5),("varnishd",pid=4969,fd=5)) LISTEN 0 511 *:8080 *:* users:(("apache2",pid=4749,fd=4),("apache2",pid=4748,fd=4),("apache2",pid=4745,fd=4))
Verify Varnish Cache
To test Varnish Cache, execute the following CURL command:
curl -I http://localhost/
The output should indicate that the request passed through Varnish:
HTTP/1.1 200 OK Date: Tue, 18 Oct 2022 13:08:27 GMT Server: Apache/2.4.52 (Ubuntu) Last-Modified: Tue, 18 Oct 2022 13:03:09 GMT Vary: Accept-Encoding Content-Type: text/html X-Varnish: 2 Age: 0 Via: 1.1 varnish (Varnish/7.0) ETag: W/"29af-5eb4eb6b9e071-gzip" Accept-Ranges: bytes Content-Length: 10671 Connection: keep-alive
Conclusion
Congratulations! You have successfully installed Varnish Cache and configured it with Apache on Ubuntu 22.04. You can now utilize Varnish to accelerate your website’s load time significantly. If you have any questions, feel free to reach out!
FAQ
- Why do we need to change Apache’s default port?
- We configure Apache to listen on port 8080 to free up port 80 for Varnish. Varnish will listen on port 80 and act as a front-end to handle HTTP requests.
- What does the X-Varnish header indicate?
- The X-Varnish header is used by the Varnish cache to log requests. It helps in identifying the requests handled by the Varnish server.
- How can I test if Varnish is correctly caching my content?
- You can use the `curl -I http://localhost/` command. If the response contains the X-Varnish and Via headers, it indicates that Varnish handled the request.
- Can I specify a different backend server instead of localhost?
- Yes, you can modify the .host field in the default.vcl file to point to a different server by specifying its IP address or hostname.