基于 hugo 生成的个人静态网站,有多种上线方式可以选择(参考 Host and deploy with hugo)。如果你和我一样,打算将个人博客部署在自己的 VPS 服务器上,那么 VPS 服务器需要运行 HTTP 服务,可以是 nginx,也可以是 caddy。这里我选择 caddy,因为它的文档完整,安装配置简单。
1. 安装 caddy
caddy 支持多种安装方式,可以直接下载 caddy 二进制文件:
- from releases on GitHub (expand “Assets”)
- Refer to Verifying Asset Signatures for how to verify the asset signature
- from our download page
- by building from source (either with
goorxcaddy)
也可以通过操作系统各自的包管理器进行安装,通常这种方式会为 caddy 自动创建用户和用户组,还会为 caddy 配置 systemd service。
生产环境下,十分建议将 caddy 配置为 systemd service。
Debian, Ubuntu, Raspbian
# Debian, Ubuntu, Raspbian
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
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
chmod o+r /usr/share/keyrings/caddy-stable-archive-keyring.gpg
chmod o+r /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
Installing this package automatically starts and runs Caddy as a systemd service named
caddy. It also comes with an optionalcaddy-apiservice which is not enabled by default, but should be used if you primarily configure Caddy via its API instead of config files.
Fedora, RedHat, CentOS
# Fedora
dnf install dnf5-plugins
dnf copr enable @caddy/caddy
dnf install caddy
# RedHat, CentOS
dnf install dnf-plugins-core
dnf copr enable @caddy/caddy
dnf install caddy
This package comes with both of Caddy’s systemd service unit files, but does not enable them by default. Using the service is recommended. If you do, please read the service usage instructions.
2. 配置 caddy
caddy 支持两种配置方式:API 和配置文件。这里通过配置文件进行配置。
caddy 默认配置文件为 /etc/caddy/Caddyfiled,对于静态站点,其配置方式如下:
# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace ":80" below with your
# domain name.
:80 {
# Set this path to your site's directory.
# root * /usr/share/caddy ## 通过包管理器安装 caddy 时会生成默认网页,可验证服务是否正常运行
root * /var/www ## 将静态站点内容同步在该目录下,该目录的属主和属组需要正确配置
# Enable the static file server.
file_server
# Another common task is to set up a reverse proxy:
# reverse_proxy localhost:8080
# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000
}
# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile
在 VPS 服务器中为静态站点创建目录 /var/www,然后设置权限(该路径即是 caddy 配置文件中指定的路径):
## 创建静态站点目录
mkdir -p /var/www
## 默认创建的 caddy 用户具有 /usr/sbin/nologin 属性,
## 因此将目录属主设置为 ubuntu,便于通过 scp 命令向服务器同步静态站点;
## 用户属组设置为 caddy,这样通过 systemd 启动的 caddy 服务具有读权限,
## 否则浏览器打开网页时会报错 403;
chown -R ubuntu:caddy /var/www/
## 下面的命令会更新 /var/www 目录下文件和目录权限,
## 如果 /var/www 是新创建的目录下,则不需要执行;
find /var/www -type f -exec chmod 640 {} \;
find /var/www -type d -exec chmod 750 {} \;
然后将静态站点同步至服务器 /var/www 目录。
3. 运行 caddy 服务并验证
# 启动 caddy 服务
$ sudo systemctl start caddy
# 将 caddy 服务设置为开机自启动
$ sudo systemctl enable caddy
# 检查 caddy 服务运行状态
$ systemctl status caddy
● caddy.service - Caddy
Loaded: loaded (/***/caddy.service; enabled; preset: enabled)
Active: active (running); 6min ago
......
# caddy 配置文件更新后,重新加载 caddy 服务
$ sudo systemctl reload caddy
# 查看 caddy 运行日志
$ journalctl -u caddy --no-pager | less +G
caddy 启动后,在浏览器输入站点地址 http://$HOST_ADDR 检查站点是否正常运行。
如果站点不能正常显示,检查服务器防火墙、服务器提供商防火墙,检查是否开放 80 端口。
4. 其他
caddy 还可以提供 HTTPS 服务,详见 caddy https quick-start。
这里我仅配置了 caddy 的 HTTP 服务,用来接收来自服务器上另一个 HTTPS 服务的重定向流量。这样配置的好处是可以复用服务器 443 端口:特定的 HTTPS 请求直接由监听 443 端口的服务处理,其他的 HTTPS 请求统一重定向给监听在 80 端口 caddy 服务。