This guide walks you through the installation and configuration of Varnish, a high-performance caching HTTP accelerator, on an Ubuntu VPS. Learn how to set up Varnish with web servers like Apache or Nginx, manage cache, and optimize your website for faster content delivery and improved server performance.
10 min
Edited:14-10-2024
Varnish is an open-source HTTP accelerator designed to cache and optimize content delivery. It acts as a reverse proxy, sitting between your web server and users, caching frequently accessed content to serve it quickly. This reduces the load on your backend server, speeds up response times, and improves the overall performance of your website. It’s especially beneficial for high-traffic websites that need to handle thousands of simultaneous requests efficiently.
Here’s a step-by-step guide to installing Varnish on an Ubuntu server. This guide assumes you have root or sudo privileges.
Before installing any new packages, ensure your system is up-to-date by running:
1. sudo apt update && sudo apt upgrade -y
Varnish is available in the official Ubuntu repositories, so installation is straightforward. Use the following command:
1. sudo apt install varnish -y
After installation, Varnish will listen on port 6081 by default. You’ll need to configure it to work with your web server, like Apache or Nginx. The primary configuration file is located at /etc/varnish/default.vcl.
To configure Varnish to work with Apache, follow these steps:
1. Edit the default.vcl file:
1. sudo nano /etc/varnish/default.vcl
2. Define your backend server (Apache) in the configuration. Replace 127.0.0.1 and 8080 with your web server's IP and port:
1. vcl 4.0;
2. backend default {
3. .host = "127.0.0.1";
4. .port = "8080";
5. }
3. Save and exit (Ctrl + X, then Y and Enter).
By default, Varnish listens on port 6081, but we want it to serve HTTP traffic on port 80 (the default HTTP port). To do this, edit the Varnish service configuration file:
1. sudo nano /etc/systemd/system/multi-user.target.wants/varnish.service
Find the ExecStart line and modify the listening port:
1. ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
This tells Varnish to listen on port 80 and allocate 256MB of memory for caching.
Save and exit the file.
Since Varnish is now handling HTTP requests on port 80, you need to modify Apache or Nginx to run on a different port (usually port 8080).
- For Apache:
Edit the Apache ports configuration:
1. sudo nano /etc/apache2/ports.conf
Change the Listen directive to port 8080:
1. Listen 8080
Then, edit your virtual host configuration file (typically located in /etc/apache2/sites-available/000-default.conf):
1. <VirtualHost *:8080>
2. ServerAdmin webmaster@localhost
3. DocumentRoot /var/www/html
4. ...
5. </VirtualHost>
Save the file and restart Apache:
1. sudo systemctl restart apache2
- For Nginx:
If you are using Nginx, open the server block configuration (typically found in /etc/nginx/sites-available/default):
1. sudo nano /etc/nginx/sites-available/default
Change the listen directive to port 8080:
1. server {
2. listen 8080;
3. server_name yourdomain.com;
4. root /var/www/html;
5. ...
6. }
Save the file and restart Nginx:
1. sudo systemctl restart nginx
Now start the Varnish service and enable it to run on boot:
1. sudo systemctl daemon-reload
2. sudo systemctl start varnish
3. sudo systemctl enable varnish
You can check if Varnish is running correctly by using the following command:
1. sudo systemctl status varnish
It should display that Varnish is active and running. You can also check if it’s caching content properly by using curl to inspect headers:
1. curl -I http://yourdomain.com
If Varnish is working, you should see the X-Varnish header, which shows the cache status (HIT or MISS).
Once Varnish is up and running, you can test its caching capabilities by sending HTTP requests to your site and examining the headers.
The first time you make a request, the content will be fetched from the backend server, and Varnish will store a copy in its cache.
1. curl -I http://yourdomain.com
The response header will show a MISS:
1. HTTP/1.1 200 OK
2. X-Varnish: 2
3. Age: 0
4. Via: 1.1 varnish (Varnish/6.0)
This indicates that Varnish fetched the content from the backend (Apache or Nginx) and cached it.
If you request the same content again, Varnish will serve it from its cache, resulting in a HIT:
1. curl -I http://yourdomain.com
This time, you should see:
1. HTTP/1.1 200 OK
2. X-Varnish: 2 5
3. Age: 10
4. Via: 1.1 varnish (Varnish/6.0)
The X-Varnish header shows that the content was served from the cache, and the Age header indicates how long the content has been cached.
If you want to purge specific content from the cache, you can use the varnishadm tool. For example, to purge the home page from the cache:
1. sudo varnishadm ban req.url ~ "/"
This forces Varnish to remove the cached version of the homepage, and the next request will be fetched from the backend.
Varnish is a powerful tool for optimizing web performance by caching frequently requested content. By following this guide, you can install Varnish on your Ubuntu VPS, configure it to work with Apache or Nginx, and start enjoying the benefits of faster load times and reduced server load. Whether you're running a high-traffic website or just looking to optimize your server, Varnish is a great solution to improve responsiveness and scalability.
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min