How to set caddy.HomeDir in Caddyfile

1. Caddy version (caddy version):

v2.1.1

2. How I run Caddy:

a. System environment:

Ubuntu 20.04

b. Command:

systemctl start caddy

c. Service/unit/compose file:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
User=caddy
Group=caddy
ReadWritePaths=/customPath/caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile or JSON config:

{
        email   admin@example.com
}
subdomain.example.com {
        root    * /var/www/subdomain
        file_server
        log {
                output file     /var/log/subdomain.log
                format single_field common_log
        }

        php_fastcgi unix//run/php/php7.4-fpm.sock


        header {
                Strict-Transport-Security               "max-age=15552000;"
                X-Frame-Options                         "SAMEORIGIN" always
                X-Permitted-Cross-Domain-Policies       "none" always
                X-Robots-Tag                            "none" always
                X-XSS-Protection                        "1; mode=block" always
                X-Content-Type-Options                  "nosniff"
        }

        redir /.well-known/carddav /remote.php/dav 301
        redir /.well-known/caldav /remote.php/dav 301
        redir /.well-known/webfinger /public.php?service=webfinger
        redir /.well-known/host-meta /public.php?service=host-meta
        redir /.well-known/host-meta\.json /public.php?service=host-meta-json

        # .htaccess / data / config / ... shouldn't be accessible from outside
        @forbidden {
                path    /.htaccess
                path    /data/*
                path    /config/*
                path    /db_structure
                path    /.xml
                path    /README
                path    /3rdparty/*
                path    /lib/*
                path    /templates/*
                path    /occ
                path    /console.php
        }

        respond @forbidden 404
}

3. The problem I’m having:

The HomeDir for caddy is wrong

$ caddy environ

caddy.HomeDir=/home/twinkybot
caddy.AppDataDir=/home/twinkybot/.local/share/caddy
caddy.AppConfigDir=/home/twinkybot/.config/caddy
caddy.ConfigAutosavePath=/home/twinkybot/.config/caddy/autosave.json

I want it to be /caddy, which I managed with
ReadWritePaths=/customPath/caddy in my service file.

But I want this to be in the Caddyfile. What do I need to configure?

Thanks.

The confusion here is that you’re running caddy environ as your current user, not as the user that Caddy will actually run with under systemd.

If you take a look at your Caddy logs with journalctl -u caddy, you’ll see the environment printed out when it starts.

If that’s still incorrect in your opinion, then you can either set the XDG environment variables as described below or with the storage global option.

storage file_system {
    root /your/different/path
}
sudo -u www-data caddy environ

Leads to same home directory.

journalctl -u caddy points to /var/lib/caddy and has old entries
journalctl -xe has current entries also pointing to /var/lib/caddy

Adding

{
        email   admin@example.com
        storage file_system {
                root /<customPath>/caddy
        }
}

Didn’t help.

I also found the documentation to XDG_CONFIG_HOME but I do not understand where to set this. Has this to be in the service file in my .zshrc or in the caddyfile. And if in the latter then how?

XDG_CONFIG_HOME is an environmental variable.

Set it however is required for the environment you run Caddy in.

(For systemd you will want to set it in your unit file: How to set environment variable in systemd service? - Server Fault)

Thanks, I added

Environment="XDG_CONFIG_HOME=/<customPath>/caddy"
Jul 27 10:15:32 caddy[13539]: caddy.HomeDir=/var/lib/caddy
Jul 27 10:15:32 caddy[13539]: caddy.AppDataDir=/var/lib/caddy/.local/share/caddy
Jul 27 10:15:32 caddy[13539]: caddy.AppConfigDir=/<customPath>/caddy/caddy
Jul 27 10:15:32 caddy[13539]: caddy.ConfigAutosavePath=/<customPath>/caddy/caddy/autosave.json

It worked partially, as you can see above.
What about HomdeDir and AppDataDir?

Thanks for your help :slight_smile:

See XDG_DATA_HOME and XDG_CONFIG_HOME, per docs:

Conventions — Caddy Documentation
Conventions — Caddy Documentation

:blush:
So much for attentive reading…

Thanks.

With both values set the HomeDir stays though at caddy.HomeDir=/var/lib/caddy and there is HOME=/var/lib/caddy

What will be / is stored in the HomeDir?

Nothing is directly stored there, that’s just the value of $HOME as Caddy sees it, i.e. the caddy user’s home directory. See the link below, there’s a block that shows the command typically used to create the user:

Thanks for your help. Now I set it up as I think I need it :smiley:
Let’s see how it goes.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.