Troubleshooting Varnish Daemon Not Listening on Configured Port in Ubuntu 16.04 with Systemd


2 views

When setting up Varnish on Ubuntu 16.04, many users encounter issues where the Varnish daemon fails to listen on the configured port. This typically occurs due to conflicts between traditional init scripts and systemd configurations that were introduced in Ubuntu 15.04 and later.

The main issue stems from multiple configuration files attempting to control Varnish's startup parameters:

/etc/default/varnish
/etc/systemd/system/varnish.service
/lib/systemd/system/varnish.service

These files often contain conflicting ExecStart directives, causing the service to fail or behave unexpectedly.

First, ensure you have a clean installation:

sudo apt remove varnish
sudo apt-get purge varnish
sudo rm -f /etc/systemd/system/varnish.service*
sudo apt install varnish

The key configuration file is /lib/systemd/system/varnish.service. Here's the proper setup:

[Unit]
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/

[Service]
Type=simple
LimitNOFILE=131072
LimitMEMLOCK=82000
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
ExecReload=/usr/share/varnish/reload-vcl
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true

[Install]
WantedBy=multi-user.target

After making changes, reload systemd and restart Varnish:

sudo systemctl daemon-reload
sudo service varnish restart

Check the service status:

systemctl status varnish
netstat -tulnp | grep varnish

If issues persist, try running Varnish in debug mode:

sudo varnishd -d -f /etc/varnish/default.vcl

This will help identify any configuration errors in your VCL file.

Ensure all related files are properly configured:

  1. /etc/default/varnish - Should match your systemd parameters
  2. /etc/varnish/default.vcl - Should have valid backend definitions
  3. /etc/apache2/ports.conf - Should listen on alternative port (e.g., 8080)

Remember that Ubuntu 16.04's systemd implementation requires different configuration approaches than older versions. Always verify which configuration files are actually being used by checking:

systemctl cat varnish

After upgrading to Ubuntu 16.04, many users encounter issues with Varnish not listening on the configured port. The root cause often lies in the transition to systemd and conflicting configuration files.

Here are the critical files that need verification:

/etc/default/varnish:
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"
/etc/varnish/default.vcl (vcl 4.0):
backend default {
    .host = "www.varnish-cache.org";
    .port = "80";
}

The systemd service file often gets overlooked. Check these locations:

grep -R 'ExecStart=/usr/sbin/varnishd' /etc/
/etc/systemd/system/varnish.service
/etc/systemd/system/varnish.service.d/customexec.conf
/etc/systemd/system/multi-user.target.wants/varnish.service

When checking service status, you might see:

systemctl status varnish
● varnish.service - Varnish HTTP accelerator
   Loaded: loaded (/etc/systemd/system/varnish.service; enabled)
   Active: inactive (dead)

Here's the complete fix that resolved the issue:

sudo apt remove varnish
sudo apt-get purge varnish
# Manually remove any remaining files in /etc/systemd/system/*
sudo apt install varnish
sudo nano /lib/systemd/system/varnish.service
sudo nano /etc/varnish/default.vcl
sudo systemctl daemon-reload
sudo service varnish restart

The critical file is /lib/systemd/system/varnish.service. Many outdated tutorials point to wrong locations, so be cautious.

After implementing the solution, verify with:

netstat -tulnp | grep varnish
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1234/varnishd

And check the service status:

service --status-all | grep varnish
 [ + ]  varnish
 [ + ]  varnishlog
 [ + ]  varnishncsa