Nginx配置acme自动更新ssl证书
Nginx配置acme自动更新ssl证书
参考了这些链接的内容
2024年3月份的时候亚信调整了ssl的规则,所以不再使用腾讯云签发的免费证书,改用acme.sh自动更新脚本 Let’s Encrypt官方 下面记录一下过程
安装
先安装。
# 安装并使用自己的邮箱
curl https://get.acme.sh | sh -s email=jerryhuang@outlook.jp
# 或者用wget
wget https://get.acme.sh | sh -s email=jerryhuang@outlook.jp
已安装的就更新
acme.sh --upgrade
添加自动更新
acme.sh --upgrade --auto-upgrade
配置腾讯云
我们使用dns的验证方式,需要添加一个子用户。
先转到访问管理选择策略
添加一个策略,用于更新域名dns。
{
"statement": [
{
"action": [
"dnspod:DescribeRecordFilterList",
"dnspod:DescribeRecordList",
"dnspod:CreateRecord",
"dnspod:DeleteRecord"
],
"effect": "allow",
"resource": [
"*"
]
}
],
"version": "2.0"
}
创建一个只编程方式访问的子用户,然后添加刚刚创建的策略。 记录保存 SecretId 和 SecretKey。
申请证书
将获取到的 SecretId 和 SecretKey 导入环境变量中,以便 acme.sh 调用。
export Tencent_SecretId="<Your SecretId>"
export Tencent_SecretKey="<Your SecretKey>"
记得这个不要泄露!
然后用came申请证书,例如:
acme.sh --issue --dns dns_tencent -d example.com -d *.example.com
出现网络问题的可以尝试添加 --server letsencrypt
acme.sh --server letsencrypt --issue --dns dns_tencent -d example.com -d *.example.com
运行后,acme.sh 将自动为您的域名申请证书,并将证书文件保存在~/.acme.sh/example.com/目录下,并且会自动为您的域名配置证书自动续期任务,无需手动续期。
现在默认生成ECDSA加密的证书,想生成兼容性更好的rsa需要使用
--keylength 2048
设置证书路径和自动重载Nginx
不要直接访问acme.sh的路径,得把文件复制输出到一个地方并且重启nginx
acme.sh --install-cert -d example.com \
--key-file /www/wwwroot/acme_cert/example.com.key \
--fullchain-file /www/wwwroot/acme_cert/example.cer \
--reloadcmd "/etc/init.d/nginx reload"
(一个小提醒, 这里用的是
service nginx force-reload, 不是service nginx reload, 据测试, reload 并不会重新加载证书, 所以用的 force-reload) 宝塔安装的可能要用/etc/init.d/nginx restart或/etc/init.d/nginx reloadNginx 的配置 ssl_certificate 使用 /etc/nginx/ssl/fullchain.cer ,而非/etc/nginx/ssl/<domain>.cer,否则 SSL Labs 的测试会报 Chain issues Incomplete 错误。 –install-cert命令可以携带很多参数, 来指定目标文件. 并且可以指定 reloadcmd, 当证书更新以后, reloadcmd会被自动调用,让服务器生效. 详细参数请参考: https://github.com/Neilpang/acme.sh#3-install-the-issued-cert-to-apachenginx-etc 值得注意的是, 这里指定的所有参数都会被自动记录下来, 并在将来证书自动更新以后, 被再次自动调用.
查看已安装证书信息
acme.sh --info -d example.com
遇到timeout之类的问题
遇到timeout需要使用网络代理来处理更新。 切换服务器的命令
杂项
关闭自动更新
acme.sh --upgrade --auto-upgrade 0
出错查看日志
acme.sh --issue ..... --debug
或者
acme.sh --issue ..... --debug 2
检查linux环境变量
echo $Tencent_SecretId
echo $Tencent_SecretKey