自动续订 SSL 证书
Nginx SSL证书部署 中,我们尝试部署安装免费的 SSL证书。但是三个月就会过期,每隔三个月就要替换操作一次,略显麻烦。本篇文章就介绍下自动续订 SSL 证书,永久使用的操作。原文地址见:Nginx 证书永久使用。
起因
之前某个周末想起来 Huan’s Garden 和 Huan’s Blog 证书都快过期了,于是登上阿里云捣鼓替换下证书。
换完之后,想着可不可以一步到位,这点小事情,能不能自动化搞定。于是记录并撰写了这篇文章。
注:以下数据已经过脱敏处理,邮箱以及域名记得自行替换。
操作步骤
1.安装并设置邮箱
// 听劝,记得用自己的邮箱
curl https://get.acme.sh | sh -s email=my@example.com
安装程序将执行 3 个操作:
- 创建
acme.sh
并复制到主目录,所有证书也将放置在~/.acme.sh/
此文件夹中。 - 为以下各项创建别名:
acme.sh=~/.acme.sh/acme.sh
- 创建每日 cron 作业以检查和更新证书(如果需要)。
2.修改默认 CA 机构
acme.sh --set-default-ca --server letsencrypt
3.颁发证书
acme.sh --issue -d a.test.com -d b.test.com --webroot /usr/local/temp/
4.安装证书
acme.sh --install-cert -d huan99.com \
--key-file /usr/local/nginx/cert/key.pem \
--fullchain-file /usr/local/nginx/cert/cert.pem \
--reloadcmd "service nginx force-reload"
报错排查
报错一:邮箱无效(听劝的这一步不会发生)
[Sun Mar 30 20:56:55 CST 2025] Account registration error: {
"type": "urn:ietf:params:acme:error:invalidContact",
"detail": "Error creating new account :: contact email has forbidden domain \"example.com\"",
"status": 400
}
[Sun Mar 30 20:56:55 CST 2025] Please add '--debug' or '--log' to see more information.
[Sun Mar 30 20:56:55 CST 2025] See: https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh
find -name *acme.sh
cd ./.acme.sh/
vi account.conf
//account.conf 中修改邮箱配置成有效的
报错二:文件目录设置出错
acme.sh --issue -d a.test.com -d b.test.com --webroot /usr/local/temp/
[Sun Mar 30 21:07:36 CST 2025] test.com: Invalid status. Verification error details: 8.8.9.9: Invalid response from https://test.com/.well-known/acme-challenge/……: 500
[Sun Mar 30 21:07:36 CST 2025] Please add '--debug' or '--log' to see more information.
[Sun Mar 30 21:07:36 CST 2025] See: https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh
确保 /usr/local/temp/目录可以被访问
//比如test目录可访问,则
acme.sh --issue -d a.test.com -d b.test.com --webroot /usr/local/test/
//成功提示如下
-----END CERTIFICATE-----
[Sun Mar 30 21:26:41 CST 2025] Your cert is in: /root/.acme.sh/test.com_ecc/test.com.cer
[Sun Mar 30 21:26:41 CST 2025] Your cert key is in: /root/.acme.sh/test.com_ecc/test.com.key
[Sun Mar 30 21:26:41 CST 2025] The intermediate CA cert is in: /root/.acme.sh/test.com_ecc/ca.cer
[Sun Mar 30 21:26:41 CST 2025] And the full-chain cert is in: /root/.acme.sh/test.com_ecc/fullchain.cer