Caddy Service not loading

When I run caddy -conf [caddyfile], the service runs fine. If I try to run Caddy as a service, I get the following error: “Error during parsing: Setting up DNS provider ‘namecheap’: namecheap: some credentials information are missing…” (as per journatctl). My environmental variables are set correctly as I can see them when running env, and it wouldn’t work otherwise when running manually via caddy -conf caddyfile .

I’m doing the DNS challenge in my caddyfile and don’t get this error when running manually. I’m not sure why it fails when I run it as a service. Below is my systemd service file:

[Unit]
Description=Caddy's service
ConditionFileIsExecutable=/usr/local/bin/caddy
After=network-online.target

[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/local/bin/caddy -conf /location/of/caddyfile

Restart=always
RestartSec=120

[Install]
WantedBy=multi-user.target

Anyone have any ideas why it’s failing when running as a service?

TIA

Hi @aC23, welcome to the Caddy community.

Your environment, as a user, is completely different to the environment Caddy runs in when systemd is supervising it. The supervised environment is set up from scratch as a clean running space.

In order to give Caddy the variables it needs, you need to either:

  1. Tell systemd exactly what variables you want

Do this by adding Environment="VARIABLE=VALUE" under the [Service] heading. Replace VARIABLE and VALUE appropriately and repeat for each variable required.

  1. Create an environment file and tell either systemd or Caddy where it is

An environment file is just any text file with VARIABLE=VALUE lines. You can put multiple in the file separated by newlines. You can tell systemd where it is by adding EnvironmentFile=/path/to/env/file under the [Service] heading, OR you can tell Caddy where it is with the -envfile /path/to/env/file flag (no need to do both).

FWIW, in Caddy 2, we have caddy run --environ which will print the environment (and a little more info too!) at the beginning while Caddy runs, and caddy environ which prints the environment and exits. Both of these can be more helpful in debugging Caddy than in v1.

2 Likes

Thank you @Whitestrake, that did it.

I will definitely consider v2, @matt, thanks for the suggestion.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.