2016-09-23 17:50:39 -05:00
|
|
|
# systemd Service Unit for Caddy
|
2016-03-17 06:37:15 -05:00
|
|
|
|
2016-05-20 17:29:18 -05:00
|
|
|
Please do not hesitate to ask on
|
|
|
|
[caddyserver/support](https://gitter.im/caddyserver/support)
|
2016-09-23 17:50:39 -05:00
|
|
|
if you have any questions. Feel free to prepend to your question
|
|
|
|
the username of whoever touched the file most recently, for example
|
|
|
|
`@wmark re systemd: …`.
|
2016-05-20 17:29:18 -05:00
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
The provided file should work with systemd version 219 or later. It might work with earlier versions.
|
2017-04-26 11:45:57 -05:00
|
|
|
The easiest way to check your systemd version is to run `systemctl --version`.
|
2016-03-17 06:37:15 -05:00
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
## Instructions
|
2016-03-17 06:37:15 -05:00
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
We will assume the following:
|
|
|
|
|
|
|
|
* that you want to run caddy as user `www-data` and group `www-data`, with UID and GID 33
|
|
|
|
* you are working from a non-root user account that can use 'sudo' to execute commands as root
|
|
|
|
|
|
|
|
Adjust as necessary or according to your preferences.
|
|
|
|
|
|
|
|
First, put the caddy binary in the system wide binary directory and give it
|
|
|
|
appropriate ownership and permissions:
|
2016-05-11 13:48:47 -05:00
|
|
|
|
|
|
|
```bash
|
2016-09-23 17:50:39 -05:00
|
|
|
sudo cp /path/to/caddy /usr/local/bin
|
|
|
|
sudo chown root:root /usr/local/bin/caddy
|
|
|
|
sudo chmod 755 /usr/local/bin/caddy
|
|
|
|
```
|
|
|
|
|
|
|
|
Give the caddy binary the ability to bind to privileged ports (e.g. 80, 443) as a non-root user:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/caddy
|
|
|
|
```
|
|
|
|
|
|
|
|
Set up the user, group, and directories that will be needed:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo groupadd -g 33 www-data
|
|
|
|
sudo useradd \
|
2016-05-11 13:48:47 -05:00
|
|
|
-g www-data --no-user-group \
|
|
|
|
--home-dir /var/www --no-create-home \
|
|
|
|
--shell /usr/sbin/nologin \
|
|
|
|
--system --uid 33 www-data
|
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
sudo mkdir /etc/caddy
|
2018-06-19 10:15:38 -05:00
|
|
|
sudo chown -R root:root /etc/caddy
|
2016-09-23 17:50:39 -05:00
|
|
|
sudo mkdir /etc/ssl/caddy
|
2018-01-15 20:15:17 -05:00
|
|
|
sudo chown -R root:www-data /etc/ssl/caddy
|
2016-09-23 17:50:39 -05:00
|
|
|
sudo chmod 0770 /etc/ssl/caddy
|
|
|
|
```
|
|
|
|
|
|
|
|
Place your caddy configuration file ("Caddyfile") in the proper directory
|
|
|
|
and give it appropriate ownership and permissions:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo cp /path/to/Caddyfile /etc/caddy/
|
2018-06-19 10:15:38 -05:00
|
|
|
sudo chown root:root /etc/caddy/Caddyfile
|
|
|
|
sudo chmod 644 /etc/caddy/Caddyfile
|
2016-09-23 17:50:39 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
Create the home directory for the server and give it appropriate ownership
|
|
|
|
and permissions:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo mkdir /var/www
|
|
|
|
sudo chown www-data:www-data /var/www
|
|
|
|
sudo chmod 555 /var/www
|
2016-05-11 13:48:47 -05:00
|
|
|
```
|
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
Let's assume you have the contents of your website in a directory called 'example.com'.
|
|
|
|
Put your website into place for it to be served by caddy:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo cp -R example.com /var/www/
|
|
|
|
sudo chown -R www-data:www-data /var/www/example.com
|
|
|
|
sudo chmod -R 555 /var/www/example.com
|
2016-03-17 06:37:15 -05:00
|
|
|
```
|
|
|
|
|
2017-01-10 16:03:50 -05:00
|
|
|
You'll need to explicitly configure caddy to serve the site from this location by adding
|
2016-09-23 17:50:39 -05:00
|
|
|
the following to your Caddyfile if you haven't already:
|
|
|
|
|
|
|
|
```
|
|
|
|
example.com {
|
|
|
|
root /var/www/example.com
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Install the systemd service unit configuration file, reload the systemd daemon,
|
|
|
|
and start caddy:
|
2016-05-11 12:09:54 -05:00
|
|
|
|
|
|
|
```bash
|
2018-02-03 01:53:40 -05:00
|
|
|
wget https://raw.githubusercontent.com/mholt/caddy/master/dist/init/linux-systemd/caddy.service
|
2016-09-23 17:50:39 -05:00
|
|
|
sudo cp caddy.service /etc/systemd/system/
|
|
|
|
sudo chown root:root /etc/systemd/system/caddy.service
|
2017-03-11 22:43:54 -05:00
|
|
|
sudo chmod 644 /etc/systemd/system/caddy.service
|
2016-09-23 17:50:39 -05:00
|
|
|
sudo systemctl daemon-reload
|
|
|
|
sudo systemctl start caddy.service
|
|
|
|
```
|
2016-05-11 12:09:54 -05:00
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
Have the caddy service start automatically on boot if you like:
|
2016-05-20 17:29:18 -05:00
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
```bash
|
|
|
|
sudo systemctl enable caddy.service
|
|
|
|
```
|
|
|
|
|
|
|
|
If caddy doesn't seem to start properly you can view the log data to help figure out what the problem is:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
journalctl --boot -u caddy.service
|
2016-05-11 12:09:54 -05:00
|
|
|
```
|
2016-03-17 06:37:15 -05:00
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
Use `log stdout` and `errors stderr` in your Caddyfile to fully utilize systemd journaling.
|
|
|
|
|
|
|
|
If your GNU/Linux distribution does not use *journald* with *systemd* then check any logfiles in `/var/log`.
|
2016-03-17 06:37:15 -05:00
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
If you want to follow the latest logs from caddy you can do so like this:
|
2016-05-20 17:29:18 -05:00
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
```bash
|
|
|
|
journalctl -f -u caddy.service
|
|
|
|
```
|
2016-05-11 13:48:47 -05:00
|
|
|
|
2016-09-23 17:50:39 -05:00
|
|
|
You can make other certificates and private key files accessible to the `www-data` user with the following command:
|
2016-05-11 13:48:47 -05:00
|
|
|
|
|
|
|
```bash
|
|
|
|
setfacl -m user:www-data:r-- /etc/ssl/private/my.key
|
|
|
|
```
|