Changing the Varnish port in Ubuntu: A step-by-step guide for improved web service management

ubuntu@freshers_in

Changing the Varnish port in Ubuntu is a common task when you want to run multiple web applications or services on the same server, each requiring its own HTTP cache. In this detailed article, we’ll guide you through the process of changing the default Varnish port (port 6081) to a custom port of your choice. We’ll provide step-by-step instructions with explanations.

Prerequisites:

Before you begin, ensure you have the following:

  • An Ubuntu server with Varnish installed.
  • Administrative access to your server (e.g., through SSH).
  • Basic knowledge of the Linux command line.

Step 1: Determine the New Port

Decide on the new port number that you want to use for Varnish. For this example, let’s assume you want to change it to port 8080.

Step 2: Configure Varnish

Open the Varnish configuration file in a text editor. The default configuration file is usually located at /etc/varnish/default.vcl. You can use any text editor of your choice; here, we’ll use nano:

sudo nano /etc/varnish/default.vcl

In the configuration file, locate the section where the backend is defined. It typically looks like this:

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

Change the .port value to your new desired port (e.g., .port = “8080”). Make sure it matches the port you decided in Step 1.

Step 3: Update the Varnish Systemd Service File

Open the Varnish systemd service file in a text editor. The service file is usually located at /lib/systemd/system/varnish.service:

sudo nano /lib/systemd/system/varnish.service

Find the ExecStart line in the service file. It should look like this:

ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Update the port number in the ExecStart line to match the new port you chose in Step 1 (e.g., change -a :6081 to -a :8080):

ExecStart=/usr/sbin/varnishd -a :8080 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Step 4: Reload Varnish and Systemd

Reload the systemd manager to apply the changes to the Varnish service:

sudo systemctl daemon-reload

Restart Varnish to apply the new port configuration:

sudo systemctl restart varnish

Step 5: Verify the Configuration

To verify that Varnish is now running on the new port, you can use the netstat command:

netstat -tuln | grep -E 'LISTEN|varnish'

You should see an entry with the new port (e.g., :8080) indicating that Varnish is listening on the updated port.

By following these instructions, you can easily reconfigure Varnish to use a custom port, allowing you to run multiple web applications or services on the same server with their own Varnish caches.

Read more on Shell and Linux related articles

Author: user