用免费 HTTPS(Caddy)在 VPS 上架设网站

用 Caddy 在 VPS 上部署静态站点或 SPA,自动签发 Let's Encrypt 证书。真实命令,7 分钟完成。

Caddy 会自动申请并续期 Let’s Encrypt 证书:无需 certbot 定时任务,也不用折腾配置。前提:域名的 A 记录已指向您的 VPS IP。

1. 安装 Caddy

ssh root@YOUR_SERVER_IP
apt update && apt install -y caddy
# for the latest version consider the official caddy.com repo

2. 上传网站文件

mkdir -p /var/www/mysite
# from your computer:
scp -r ./dist/* root@YOUR_SERVER_IP:/var/www/mysite/

3. 配置 Caddy

cat > /etc/caddy/Caddyfile <<'EOF'
mysite.com {
    root * /var/www/mysite
    encode gzip
    file_server
}
EOF
systemctl reload caddy

4. 完成:检查一下

打开 `https://mysite.com`:小锁图标已经出现。Caddy 已自行签发证书,并会自动续期。需要 SPA 路由?在站点块内添加 `try_files {path} /index.html`。

还没有服务器?[Core VPS 4(€12/月)]可以轻松托管多个网站。

相关内容

查看套餐指南常见问题