受够了Nginx的臃肿,决定换Caddy。
Caddy是使用Go语言编写的一个优秀的WebServer,具有轻量、易于配置、高效、支持IPv6等特性。
之前拿Caddy只是玩玩而已,这次可是真的用于生产环境(滑稽)。就此整理一番。

安装

1
curl https://getcaddy.com | bash -s personal http.expires,http.git,http.minify

并将如下内容写入/etc/systemd/system/caddy.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit]
Description=Caddy HTTP/2 Web Server
Documentation=https://caddyserver.com/v1/docs
After=network-online.target
Requires=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/bin/caddy -conf=/etc/thisCaddy/Caddyfile -agree=true
ExecReload=/bin/kill -USR1 $MAINPID
ExecStop=/bin/kill -QUIT $MAINPID
RestartPreventExitStatus=23
Restart=always
User=root

[Install]
WantedBy=multi-user.target

然后我们再给一下目录权限:

1
2
3
mkdir /www
chown www-data:www-data -R /www
chmod -R 755 /www

添加Sury源。在/etc/apt/sources.list中添加如下内容:

1
deb https://packages.sury.org/php buster main

并导入GPG密钥:

1
2
3
wget https://packages.sury.org/php/apt.gpg
apt-key add apt.gpg
apt update

安装PHP7和Sqlite3:

1
apt install -y php7.0-cgi php7.0-fpm php7.0-curl php7.0-gd php7.0-mbstring php7.0-xml php7.0-sqlite3 sqlite3

Caddy最为优势之处就是其配置文件相当简单,详见官方文档。

这里贴上一个示例,将其保存至/etc/thisCaddy/Caddyfile即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
https://example.com:443 {
gzip
root /www/example.com/
tls webmaster@example.com
fastcgi / /run/php/php7.0-fpm.sock {
ext .php
split .php
index index.php
}
rewrite {
if {path} not_match ^\/admin
to {path} {path}/ /index.php?{query}
}
header / {
Strict-Transport-Security "max-age=63072000; includeSubDomains; preload;"
X-XSS-Protection "1; mode=block"
X-Content-Type-Options "nosniff"
-Server
}
}

同步一下服务器时间:

1
2
systemctl stop ntp &>/dev/null
ntpdate time.tencent.com

最后重启Caddy,加载配置:

1
2
3
systemctl daemon-reload
systemctl enable caddy
systemctl restart caddy

Done,Enjoy!