How to Install and Configure Varnish Cache on Ubuntu 24.04
Varnish Cache is a fast tool that helps websites run better by storing (caching) web content and reducing the workload on servers. It works as a middleman between users and the web server, grabbing requests and providing saved copies of popular pages. This makes websites load faster, lowers waiting times, and uses fewer server resources.
1. Update Package List
Begin by updating the package lists to ensure you have the latest versions available.
apt update
2. Install Nginx
Install Nginx, which will serve as the web server.
apt install -y nginx
3. Install PHP-FPM
Install PHP-FPM to process PHP files. This is useful for dynamic content.
apt install php-fpm
4. Modify Nginx Configuration
Open the default Nginx configuration file and modify it as needed to work with PHP and to prepare for Varnish to act as a reverse proxy.
nano /etc/nginx/sites-available/default
To modify or replace the content in the default Nginx configuration file with a more comprehensive and secure configuration, consider the following:
server {
listen 80;
server_name Your_Server_IP;
# Root directory for your website
root /var/www/html; # Change to your actual web root directory
index index.php index.html index.htm;
# Handle PHP files
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Replace
Your_Server_IP
with IP address of your server.
5. Reload Nginx
After making changes to the configuration, reload Nginx to apply them.
systemctl reload nginx
6. Clone Varnish Demo App
Navigate to the web root directory and clone the Varnish demo application.
cd /var/www/html/
git clone https://github.com/yemiwebby/varnish-demo.git
7. Install Varnish
Install Varnish Cache.
apt install -y varnish
8. Start and Enable Varnish
Start the Varnish service and enable it to start automatically on boot.
systemctl start varnish
systemctl enable varnish
9. Verify Default Port
By default, Varnish listens on port 6081. To confirm, open your browser and visit your server's IP address with :6081 appended to it:
http://your_server_ip:6081
10. Configure Nginx
Nginx, your web server, runs on port 80. To allow Varnish to handle caching and rendering efficiently, it needs to be placed between your web app clients and the server. We will configure Varnish on port 80 and Nginx on port 8080.
Open your Nginx site configuration file:
nano /etc/nginx/sites-available/default
Change the listen directive from 80 to 8080:
server {
listen 8080 default_server;
listen [::]:8080 default_server;
root /var/www/html;
}
Reload Nginx Configuration to apply the changes
systemctl reload nginx
11. Configure Varnish to Listen on Port 80
Now, we’ll configure Varnish to listen on port 80.
Open the Varnish default configuration file:
nano /etc/default/varnish
Find the line that begins with DAEMON_OPTS
and change -a :6081
to -a :80
:
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
12. Configure Backend in Varnish:
Open the default VCL configuration file:
nano /etc/varnish/default.vcl
Ensure that the backend is set to port 8080, as this is where Nginx now serves:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
13. Restart Services
Restart Varnish:
systemctl restart varnish
Restart Nginx:
systemctl restart nginx
14. Verify Installation
Visit your server’s IP address in a web browser:
http://your_server_ip
Varnish Cache, when integrated with Nginx, can significantly enhance your web application’s performance by caching content and reducing server load. This installation guide outlines the essential steps to set up Varnish Cache alongside Nginx on Ubuntu 24.04, ensuring your web server can handle more requests efficiently.
CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME
for 10% off!
1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month
Available Locations: LAX | MIA | ATL | FRA | AMS