Change varnish port with systemd on Fedora

Of course when you setup varnish, you likely don’t want to use the default port of varnish. In case you didn’t know, varnish runs on port 80 by default. This may not be the best port for your setup. So today, I will show you how to change the port.

I’m doing this on a Fedora 33 install but any Linux OS using systemd will be the same or similiar.

One thing I found out, is there is two ways to edit the config file. Using systemctl or using your preferred editor with the direct file path.

Systemctl method:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
systemctl edit --full varnish
systemctl edit --full varnish
systemctl edit --full varnish

Or the normal preferred editor method: (file path may differ from your OS if you’re not using Fedora)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
vi /lib/systemd/system/varnish.service
vi /lib/systemd/system/varnish.service
vi /lib/systemd/system/varnish.service

Then when open the file scroll down to this line:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

To change the port, all you need to do is edit the number 80 and change it to whatever port you want it to be.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ExecStart=/usr/sbin/varnishd -a :8081 -f /etc/varnish/default.vcl -s malloc,256m
ExecStart=/usr/sbin/varnishd -a :8081 -f /etc/varnish/default.vcl -s malloc,256m
ExecStart=/usr/sbin/varnishd -a :8081 -f /etc/varnish/default.vcl -s malloc,256m

In the above example, it will run on port 8081.

Once you’ve edited the file and saved it, restart varnish.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
systemctl restart varnish
systemctl restart varnish
systemctl restart varnish

Run status to make sure it’s running on the port after restart.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
systemctl status varnish
systemctl status varnish
systemctl status varnish

You should get a response like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
Loaded: loaded (/etc/systemd/system/varnish.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2021-01-19 21:02:59 UTC; 2 weeks 6 days ago
Main PID: 30050 (varnishd)
Tasks: 217
Memory: 114.7M
CPU: 9min 528ms
CGroup: /system.slice/varnish.service
├─30050 /usr/sbin/varnishd -a :8081 -f /etc/varnish/default.vcl -s malloc,256m
└─30069 /usr/sbin/varnishd -a :8081 -f /etc/varnish/default.vcl -s malloc,256m
● varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/etc/systemd/system/varnish.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2021-01-19 21:02:59 UTC; 2 weeks 6 days ago Main PID: 30050 (varnishd) Tasks: 217 Memory: 114.7M CPU: 9min 528ms CGroup: /system.slice/varnish.service ├─30050 /usr/sbin/varnishd -a :8081 -f /etc/varnish/default.vcl -s malloc,256m └─30069 /usr/sbin/varnishd -a :8081 -f /etc/varnish/default.vcl -s malloc,256m
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
     Loaded: loaded (/etc/systemd/system/varnish.service; enabled; vendor preset: disabled)
     Active: active (running) since Tue 2021-01-19 21:02:59 UTC; 2 weeks 6 days ago
   Main PID: 30050 (varnishd)
      Tasks: 217
     Memory: 114.7M
        CPU: 9min 528ms
     CGroup: /system.slice/varnish.service
             ├─30050 /usr/sbin/varnishd -a :8081 -f /etc/varnish/default.vcl -s malloc,256m
             └─30069 /usr/sbin/varnishd -a :8081 -f /etc/varnish/default.vcl -s malloc,256m

And you’re all set! Have an amazing day!

Leave a Reply