Caddy Installation

Build Caddy

  1. Download the lastest version of Caddy.
1
2
wget https://github.com/caddyserver/caddy/releases/download/v2.0.0-rc.3/caddy_2.0.0-rc.3_linux_amd64.tar.gz

  1. Unzip
1
tar zxvf caddy_2.0.0-rc.3_linux_amd64.tar.gz
  1. Move execution file to /usr/bin/
1
sudo mv caddy /usr/bin/

Install Caddy

  1. Create the group
1
sudo groupadd --system caddy
  1. Create a new user
1
2
3
4
5
6
7
sudo useradd --system \
--gid caddy \
--create-home \
--home-dir /var/lib/caddy \
--shell /usr/sbin/nologin \
--comment "Caddy web server" \
caddy

Log

  1. Create folder to save logs
1
sudo mkdir /var/log/caddy
  1. Modified folder authority
1
sudo chown -R caddy:caddy /var/log/caddy

Create caddyfile

Caddy reads its configuration from a file called Caddyfile, stored under /etc/caddy.

1
2
3
4
5
6
7
8
9
your_domain {
root * /var/www
encode gzip
file_server

log {
output file /var/log/caddy/service_access.log
}
}

Service

  1. /etc/systemd/system create a folder file caddy.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
User=caddy
Group=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

Caddy system command

  1. Reload the systemd daemon
1
sudo systemctl daemon-reload
  1. Automatic start caddy
1
sudo systemctl enable caddy
  1. Active caddy
1
sudo systemctl start caddy
  1. Caddy status
1
sudo systemctl status caddy
  1. Reload caddy
1
sudo systemctl reload caddy
  1. Stop caddy
1
sudo systemctl stop caddy

Functions

Log

Enables access logging; the log directive can still be used in v2, but all logs are structured, encoded as JSON, by default.

  • v1
1
log access.log
  • v2
1
2
3
4
log {
output file access.log
format single_field common_log
}

Reverse_proxy

  • v1
1
proxy / localhost:9005
  • v2
1
reverse_proxy localhost:9005

SSL certificate

Caddy root content is /var/lib/caddy/, where SSL certificate is at

1
/var/lib/caddy/.local/share/caddy/certificates

Force update SSL certificate, need delete SSL certificate, and restart Caddy

1
2
3
cd /var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory
rm -rf example.com
systemctl restart caddy