| 1 | Official service files for systemd |
| 2 | ================================== |
| 3 | |
| 4 | This folder contains the officially-maintained systemd files that should be used as a basis for your own deployments. |
| 5 | |
| 6 | **⚠️ Always review your service file before using it! Change anything that you need to customize.** |
| 7 | |
| 8 | ## Instructions |
| 9 | |
| 10 | See our website for [installation instructions](https://caddyserver.com/docs/install). |
| 11 | |
| 12 | |
| 13 | ## Prerequisites |
| 14 | |
| 15 | Running Caddy as a systemd service requires the following: |
| 16 | |
| 17 | |
| 18 | Group named `caddy`: |
| 19 | |
| 20 | ```bash |
| 21 | $ groupadd --system caddy |
| 22 | ``` |
| 23 | |
| 24 | User named `caddy` with a writeable home folder: |
| 25 | |
| 26 | ```bash |
| 27 | $ useradd --system \ |
| 28 | --gid caddy \ |
| 29 | --create-home \ |
| 30 | --home-dir /var/lib/caddy \ |
| 31 | --shell /usr/sbin/nologin \ |
| 32 | --comment "Caddy web server" \ |
| 33 | caddy |
| 34 | ``` |
| 35 | |
| 36 | |
| 37 | ## Choosing a service file |
| 38 | |
| 39 | - **`caddy.service`** - Use this one if you configure Caddy with a file (for example, the Caddyfile, or a .json file). |
| 40 | - **`caddy-api.service`** - Use this one if you configure Caddy solely through its API. |
| 41 | |
| 42 | The two files are identical except for the ExecStart and ExecReload commands. |
| 43 | |
| 44 | The usual place to save the service file is: `/etc/systemd/system/caddy.service` |
| 45 | |
| 46 | ## Important |
| 47 | |
| 48 | Caddy receives all configuration through its [admin API](https://caddyserver.com/docs/api), even when the [command line interface (CLI)](https://caddyserver.com/docs/command-line) is used, which simply wraps up the API calls for you. |
| 49 | |
| 50 | Most users will use either config files and the CLI [mutually exclusively](https://caddyserver.com/docs/getting-started#api-vs-config-files) with the API because it is simpler to have only one source of truth. However, you may wish to provide Caddy an initial "bootstrapping" configuration with a config file, and use the API thereafter. |
| 51 | |
| 52 | **⚠️ If you provide an initial config file with the `--config` flag and then update the config using the API, you risk losing your changes if the service is restarted unless you have the `--resume` flag in your ExecStart command.** |
| 53 | |
| 54 | Without the `--resume` flag, the `--config` flag will overwrite any last-known configuration. |
| 55 | |
| 56 | However, it is totally safe and normal to use both the `--config` and `--resume` options together if you need to use both a config file and the API. Just be aware that if you update your config file and want to apply those changes, _stopping and starting the server is the wrong way to do this_. Restarting the service is orthogonal to config changes; this is a unique safety feature that guarantees durability and prevents data loss. If the config file has the latest changes, you should use the reload command instead. |
| 57 | |
| 58 | ## Explanation of directives |
| 59 | |
| 60 | To keep our unit files tidy, we haven't littered them with comments. So here we explain the [parameters we've chosen](https://github.com/caddyserver/dist/pull/1): |
| 61 | |
| 62 | - **`After=network.target network-online.target`** and **`Requires=network-online.target`** ensures that the network interfaces are online before Caddy starts. This is necessary because Caddy uses the network to obtain certificates and serve your site over TLS. Specifically, `network-online.target` is needed for the many users who bind listeners to specific network interfaces. |
| 63 | - **`TimeoutStopSec=5s`** will forcibly kill the caddy process if it cannot gracefully shut down within this time limit. We figure, if you're stopping the server anyway (as opposed to reloading -- two very distinct operations!) then stopping gracefully is less important than stopping at all. |
| 64 | - **`LimitNOFILE=1048576`** raises the file descriptor limit for the caddy process (`ulimit -n`). This is very important for busy sites, or for servers which need to keep connections open longer. This is the [maximum allowed value](https://stackoverflow.com/a/1213069/1048862) for some popular Linux distros. |
| 65 | - **`LimitNPROC=512`** raises the number of threads caddy is allowed to have (`ulimit -u`). Obviously, setting this too low for a highly concurrent server is a bad idea. |
| 66 | - **`PrivateTmp=true`** keeps /tmp and /var/tmp private, which are discarded after caddy stops. |
| 67 | - **`ProtectSystem=full`** allows writing to /var, which is crucial so that it can store certificates and other data for your site. |
| 68 | - **`AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE`** allows caddy to modify socket options and bind to low ports (< 1024) without running as root. |