Certbot:申请公网HTTPS证书
Module:
Categories:
Pigsty 自带了 Certbot 工具,并默认于 Infra 节点上安装启用。
这意味着你可以直接通过 certbot
命令行工具,为你的 Nginx 服务器与公网域名申请真正的 Let’Encrypt 免费 HTTPS 证书,而不是使用 Pigsty 自签名的 HTTPS 证书。
为了做到这一点,你需要:
- 确定哪些域名需要证书
- 将这些域名指向您的服务器
- 使用 Certbot 申请证书
- 将Nginx配置文件纳入管理
- 配置更新证书的定时任务
- 申请证书的一些注意事项
以下是如何去做的详细说明:
确定哪些域名需要证书
首先,您需要决定哪些 “上游服务” 需要真正的公网证书
infra_portal:
home : { domain: h.pigsty.cc }
grafana : { domain: g.pigsty.cc ,endpoint: "${admin_ip}:3000" ,websocket: true }
prometheus : { domain: p.pigsty.cc ,endpoint: "${admin_ip}:9090" }
alertmanager : { domain: a.pigsty.cc ,endpoint: "${admin_ip}:9093" }
blackbox : { endpoint: "${admin_ip}:9115" }
loki : { endpoint: "${admin_ip}:3100" }
minio : { domain: m.pigsty.cc ,endpoint: "${admin_ip}:9001" ,scheme: https ,websocket: true }
web : { domain: pigsty.cc ,path: "/www/web.cc" }
repo : { domain: repo.pigsty.cc ,path: "/www/repo" }
例如在 infra_portal
中,假设我们要对外暴露以下五项服务:
- Grafana 可视化监控面板的
g.pigsty.cc
域名 - Prometheus 时序数据库的
p.pigsty.cc
域名 - AlertManager 告警面板的
a.pigsty.cc
域名 - Pigsty 文档站的
pigsty.cc
域名,指向本地文档目录 - Pigsty 软件仓库的
repo.pigsty.cc
域名,指向软件仓库
这里的例子里特意没有选择为 home
主页申请真的 Let’s Encrypt 证书,原因见最后一节。
将这些域名指向您的服务器
接下来,您需要将上面选定的域名指向您服务器的 公网IP地址。
例如,Pigsty CC 站点的 IP 地址是 47.83.172.23
,则可在域名注册商(如阿里云DNS控制台)上设置以下域名解析 A 记录:
47.83.172.23 pigsty.cc
47.83.172.23 g.pigsty.cc
47.83.172.23 p.pigsty.cc
47.83.172.23 a.pigsty.cc
47.83.172.23 repo.pigsty.cc
修改完之后,可以使用
使用 Certbot 申请证书
第一次申请的时候,certbot
会提示你输入邮箱,并是否同意协议,按提示输入即可。
$ certbot --nginx -d pigsty.cc -d repo.pigsty.cc -d g.pigsty.cc -d p.pigsty.cc -d a.pigsty.cc
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): rh@vonng.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. You must agree in
order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N
Account registered.
Requesting a certificate for pigsty.cc and 4 more domains
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/pigsty.cc/fullchain.pem
Key is saved at: /etc/letsencrypt/live/pigsty.cc/privkey.pem
This certificate expires on 2025-05-18.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
Deploying certificate
Successfully deployed certificate for pigsty.cc to /etc/nginx/conf.d/web.conf
Successfully deployed certificate for repo.pigsty.cc to /etc/nginx/conf.d/repo.conf
Successfully deployed certificate for g.pigsty.cc to /etc/nginx/conf.d/grafana.conf
Successfully deployed certificate for p.pigsty.cc to /etc/nginx/conf.d/prometheus.conf
Successfully deployed certificate for a.pigsty.cc to /etc/nginx/conf.d/alertmanager.conf
Congratulations! You have successfully enabled HTTPS on https://pigsty.cc, https://repo.pigsty.cc, https://g.pigsty.cc, https://p.pigsty.cc, and https://a.pigsty.cc
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
首次申请之后,以后申请就可以省略这些步骤,直接使用这些命令了。
更简单的办法是直接以非交互式的方法调用,直接使用以下命令,传入你的邮箱和要注册的域名。
certbot --nginx --agree-tos --email rh@vonng.com -n -d supa.pigsty.cc
将Nginx配置文件纳入管理
使用 certbot
签发证书后,默认会修改 Nginx 的配置文件,将 HTTP 服务器重定向到 HTTPS 服务器,而这可能并非你想要的。
你可以通过修改 Pigsty 配置文件中的 infra_portal
参数,将 Certbot 已经 成功签发证书的域名配置到 Nginx 的配置文件中。
infra_portal:
home : { domain: h.pigsty.cc }
grafana : { domain: g.pigsty.cc ,endpoint: "${admin_ip}:3000" ,websocket: true , certbot: pigsty.cc }
prometheus : { domain: p.pigsty.cc ,endpoint: "${admin_ip}:9090" , certbot: pigsty.cc }
alertmanager : { domain: a.pigsty.cc ,endpoint: "${admin_ip}:9093" , certbot: pigsty.cc }
blackbox : { endpoint: "${admin_ip}:9115" }
loki : { endpoint: "${admin_ip}:3100" }
minio : { domain: m.pigsty.cc ,endpoint: "${admin_ip}:9001" ,scheme: https ,websocket: true }
web : { domain: pigsty.cc ,path: "/www/web.cc" , certbot: pigsty.cc }
repo : { domain: repo.pigsty.cc ,path: "/www/repo" , certbot: pigsty.cc }
这里,修改签发证书的服务器定义项,添加 certbot: <domain-name>
,这里的 <domain-name>
指的是 certbot 签发的文件名。
通常与 domain
一样,但如果你同时申请多个域名证书,certbot 会将其合并为一个证书,比如这里合并为两个文件:
Certificate is saved at: /etc/letsencrypt/live/pigsty.cc/fullchain.pem
Key is saved at: /etc/letsencrypt/live/pigsty.cc/privkey.pem
因此将证书中间的 pigsty.cc
抽出来填入 certbot
,然后重新运行:
./infra.yml -t nginx_config,nginx_launch
即可让 Pigsty 重新生成 Nginx 配置文件,回退 Certbot 对配置进行的其他修改,只保留申请的证书。
以后需要续期更新证书的时候就不需要重复这个过程了,直接使用 certbot renew
即可。
配置更新证书的定时任务
默认情况下,申请的证书有效期为三个月,所以如果在证书有效期到期之前,你应该使用 certbot renew
对证书进行续期。
如果你需要更新证书,执行以下命令即可。
certbot renew
在真正执行之前,你可以使用 DryRun 模式来测试续期是否正常:
certbot renew --dry-run
如果你修改过 Nginx 配置文件,请务必确保 certbot 的修改不会影响你的配置文件。
你可以将这个命令配置为 crontab ,在每个月的第一天凌晨执行续期并打印日志。