How To Install Varnish Cache on Ubuntu

Varnish Cache is a high-performance HTTP accelerator designed to cache and serve content faster, significantly improving web application speed. This guide will walk you through How To Install Varnish Cache on Ubuntu.

Steps To Install Varnish Cache on Ubuntu

Step 1: Update Your System

Keep your server up to date to ensure compatibility:

sudo apt update && sudo apt upgrade -y

Step 2: Install Varnish

Install Varnish from the default Ubuntu repositories:

sudo apt install varnish -y

Check its status:

sudo systemctl status varnish

Step 3: Configure Varnish to Listen on Port 80

By default, Varnish listens on port 6081. We need to change it to port 80.

1. Edit the Varnish systemd service file:

sudo nano /etc/systemd/system/multi-user.target.wants/varnish.service

2. Find the ExecStart line and update the port to :80:

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

3. Save and reload systemd:

sudo systemctl daemon-reexec
sudo systemctl restart varnish

Step 4: Set Up Backend Server

You can use Apache or Nginx as your backend. Below are examples for both:

🔧 Apache Backend (Port 8080)

1. Change Apache to listen on port 8080:

sudo nano /etc/apache2/ports.conf

Add:

Listen 8080

2. Update your VirtualHost config (e.g., /etc/apache2/sites-available/000-default.conf):

<VirtualHost *:8080>
    DocumentRoot /var/www/html
</VirtualHost>

3. Restart Apache:

sudo systemctl restart apache2

🔧 Nginx Backend (Port 8080)

1. Edit your Nginx site config (e.g., /etc/nginx/sites-available/default):

server {
    listen 8080 default_server;
    server_name _;
    root /var/www/html;

    location / {
        try_files $uri $uri/ =404;
    }
}

2. Restart Nginx:

sudo systemctl restart nginx

Step 5: Configure Varnish Backend

Now tell Varnish to forward requests to your web server (Nginx or Apache on port 8080):

sudo nano /etc/varnish/default.vcl

Update the backend block:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Restart Varnish:

sudo systemctl restart varnish

Step 6: Test Varnish

Verify Varnish is serving content with:

curl -I http://localhost

Look for the Via: 1.1 varnish header, which confirms that Varnish is active.

With Varnish Cache in front of your Apache or Nginx web server, your Ubuntu system is now optimized for speed and performance. This setup helps reduce server load and significantly improves response times—essential for high-traffic or resource-intensive applications.

Follow us for the more helpful posts!

We hope this is a useful post for you.

You can read more useful posts like How To Set Up Sort By Position In Magento 2.

5 1 vote
Article Rating

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x