文章

Caddy安装配置

Caddy是一款基于Go语言编写的强大且可扩展的平台,可以给你的站点、服务和应用程序提供服务。

🔗官网
🔗GitHub
🔗中文文档

安装

1
2
3
4
5
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
1
2
3
dnf install 'dnf-command(copr)'
dnf copr enable @caddy/caddy
dnf install caddy
1
2
3
yum install yum-plugin-copr
yum copr enable @caddy/caddy
yum install caddy
1
docker pull caddy

HTTPS

Caddy默认对所有站点使用HTTPS,只要在配置中提供了主机名。本教程假设你希望通过HTTPS获得一个公共信任的站点(即不是“localhost”),因此我们将使用公共域名和外部端口。

先决条件:

  • 基本的终端/命令行技能
  • 对DNS的基本了解
  • 已注册的公共域名
  • 外部访问端口 80 和 443
  • PATH变量中包含caddy和curlS
1
2
curl "https://cloudflare-dns.com/dns-query?name=example.com&type=A" \
  -H "accept: application/dns-json"

Systemctl后台运行

caddy默认路径在/lib/systemd/system/caddy.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# caddy.service
#
# For using Caddy with a config file.
#
# Make sure the ExecStart and ExecReload commands are correct
# for your installation.
#
# See https://caddyserver.com/docs/install for instructions.
#
# WARNING: This service does not use the --resume flag, so if you
# use the API to make changes, they will be overwritten by the
# Caddyfile next time the service is restarted. If you intend to
# use Caddy's API to configure it, add the --resume flag to the
# `caddy run` command or use the caddy-api.service file instead.

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

[Service]
Type=notify
User=caddy # 如果遇到权限不够无法访问的问题,把这里改成root
Group=caddy # 如果遇到权限不够无法访问的问题,把这里改成root
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target
本文由作者按照 CC BY 4.0 进行授权