Appearance
acme.sh 自动续签 SSL 证书实战
记录一次把服务器上多张即将过期的 Nginx SSL 证书,迁移到 acme.sh + ZeroSSL 自动申请 / 自动续期 / 自动部署的完整过程与踩坑。
说明:为便于对外分享,文中服务器 IP、域名、阿里云 AccessKey 均已脱敏,统一用
example.com、<SERVER_IP>、<AccessKeyId>等占位。
一、背景与目标
- 服务器(CentOS 7 + Nginx)上有多张 SSL 证书,其中几张已经过期,浏览器提示不安全。
- 证书集中存放在
/usr/local/nginx/conf/cert/下,命名规则为<域名>.pem(fullchain)+<域名>.key(私钥),由 Nginx 各server块引用。 - 目标:
- 一次性把已过期的证书重新签发;
- 此后自动续期 + 自动部署 + 自动 reload Nginx,不再人工干预。
二、为什么选 acme.sh
| 特性 | 说明 |
|---|---|
| 纯 Shell | 无额外依赖,体积极小,兼容各种 Linux |
| 自动续期 | 内置 cron 任务,到期自动续签 |
| 验证方式多 | HTTP-01(webroot)、DNS-01(各家 DNS API)、standalone 等 |
| 自动部署 | --install-cert 可直接把证书写入指定路径并执行 reload 命令 |
| 兼容多 CA | Let's Encrypt、ZeroSSL、Google 等 |
三、安装 acme.sh
标准安装是 curl https://get.acme.sh | sh,但该脚本会从 raw.githubusercontent.com 拉取主体,国内/受限网络常失败。改用 Gitee 镜像:
bash
git clone --depth 1 https://gitee.com/neilpang/acme.sh.git /opt/acme.sh
cd /opt/acme.sh
./acme.sh --install安装完成后:
- 主程序在
~/.acme.sh/acme.sh; - 自动写入 cron 任务(每天执行
acme.sh --cron); - 命令别名写入
~/.bashrc。
验证:
bash
~/.acme.sh/acme.sh --version四、初始化配置
bash
# 1) 选择默认 CA(本文用 ZeroSSL,原因见第五节)
~/.acme.sh/acme.sh --set-default-ca --server zerossl
# 2) 注册账号(ZeroSSL 需要邮箱)
~/.acme.sh/acme.sh --register-account -m admin@example.com若用 DNS-01 验证,还需提供 DNS 服务商 API 凭据(以阿里云为例):
bashexport Ali_Key="<AccessKeyId>" export Ali_Secret="<AccessKeySecret>"acme.sh 会自动把凭据保存到
account.conf,续期时无需再次导出。
五、签发方式选择与三大踩坑
5.1 坑一:DNS-01 需要 RAM 权限(Forbidden.RAM)
用阿里云 DNS API(--dns dns_ali)签发时,若 AccessKey 对应的是 RAM 子用户且未授予云解析权限,会报:
"Code":"Forbidden.RAM",
"Action":"alidns:DescribeDomainRecords"解决:在阿里云控制台给该子用户授予 AliyunDNSFullAccess(或包含 alidns:DescribeDomainRecords、alidns:AddDomainRecord、alidns:DeleteDomainRecord 等的最小权限策略)。
5.2 坑二:Let's Encrypt 多视角校验失败(networking error)
即使用 DNS-01 成功添加了 TXT 记录、本地校验也通过,Let's Encrypt 仍可能反复报:
During secondary validation: DNS problem: networking error looking up TXT/A for xxx原因:Let's Encrypt 引入了 multi-perspective validation(多视角校验),会从多个境外节点查询域名的权威 DNS。当域名权威 DNS 是某些对境外查询不稳定的服务商(如万网 *.hichina.com)时,部分校验节点连不上权威 DNS,于是报 networking error(注意:是"连不上",而非"记录不存在")。
解决:改用不做多视角校验的 CA —— ZeroSSL,一次通过。
排查技巧:加
--debug 2打印完整 API 交互;用公共 DoH 接口(如https://dns.alidns.com/resolve?name=example.com&type=NS)确认权威 DNS 与记录解析是否可达。
5.3 坑三:HTTP-01 被 Nginx 的全量 301 拦截
改用 HTTP-01(webroot)验证时,若 80 端口是"全量 301 跳转 HTTPS",校验请求 http://域名/.well-known/acme-challenge/xxx 会被重定向而失败。
解决:把 server 级 return 301 改为 location,并优先放行 challenge 路径:
nginx
server {
listen 80;
server_name example.com www.example.com;
# 放行 ACME 校验
location ^~ /.well-known/acme-challenge/ {
root /usr/local/nginx/html;
default_type "text/plain";
try_files $uri =404;
}
# 其余请求走 301
location / {
return 301 https://$host$request_uri;
}
}并创建 webroot 目录:
bash
mkdir -p /usr/local/nginx/html/.well-known/acme-challenge改 Nginx 配置前务必备份,改完先
nginx -t校验,再nginx -s reload。
六、签发与部署
bash
# 签发(webroot 方式;--keylength 2048 生成 RSA 证书,兼容原有 cipher 配置)
~/.acme.sh/acme.sh --issue --webroot /usr/local/nginx/html -d example.com --keylength 2048签发成功后部署(部署路径与 reload 命令会被记录,续期时自动复用):
bash
~/.acme.sh/acme.sh --install-cert -d example.com \
--key-file /usr/local/nginx/conf/cert/example.com.key \
--fullchain-file /usr/local/nginx/conf/cert/example.com.pem \
--reloadcmd "nginx -s reload"⚠️ 重要教训:如果签发实际失败(证书文件未生成),此时再执行
--install-cert,它会用不存在的源文件cat到目标路径,把目标.pem清空、.key覆盖,导致nginx -t直接失败!所以:install 前先校验证书确实生成(如
test -f ~/.acme.sh/<域名>/fullchain.cer),并且改动证书前先备份整个证书目录。
七、自动续期机制与"续期提前天数"的坑(重点)
7.1 机制
- 安装时写入 cron:
23 9 * * * "~/.acme.sh"/acme.sh --cron ...,即每天检查一次; - 每张证书的配置存在
~/.acme.sh/<域名>/<域名>.conf,其中Le_NextRenewTime表示"下次可续期时间"; - cron 运行到某证书时:若当前时间 <
Le_NextRenewTime就跳过,否则执行续期 + 重新部署 + reload。
7.2 坑:Le_RenewalDays 并非"到期前 N 天"
很多人以为 --days 7 就是"到期前 7 天续期"。实测(acme.sh v3.1.3)并非如此:
下次续期时间 = 证书创建时间 + (Le_RenewalDays - 1) 天也就是说它是按签发时间推算的。若把它设成 7,会变成每 6 天就续期一次(对 CA 而言是灾难)。
要实现"到期前 N 天续签":对有效期 90 天的证书,应设置
Le_RenewalDays = 90 - N + 1例如要"到期前 7 天" → Le_RenewalDays = 84(即签发后第 83 天 = 到期前 7 天)。同时把当前证书的 Le_NextRenewTime 直接修正为 证书到期时间 - 7 天:
bash
CONF=~/.acme.sh/example.com/example.com.conf
notafter=$(openssl x509 -in /usr/local/nginx/conf/cert/example.com.pem -noout -enddate | cut -d= -f2)
ep=$(date -u -d "$notafter" +%s)
ep_next=$(( ep - 7*86400 ))
sed -i "/^Le_RenewalDays=/d;/^Le_NextRenewTime=/d;/^Le_NextRenewTimeStr=/d" "$CONF"
{
echo "Le_RenewalDays='84'"
echo "Le_NextRenewTimeStr='$(date -u -d "@$ep_next" +%Y-%m-%dT%H:%M:%SZ)'"
echo "Le_NextRenewTime='$ep_next'"
} >> "$CONF"八、验证与运维
bash
# 查看下次续期时间
grep Le_NextRenewTimeStr ~/.acme.sh/<域名>/<域名>.conf
# 查看线上证书实际到期
echo | openssl s_client -connect 127.0.0.1:443 -servername <域名> 2>/dev/null | openssl x509 -noout -issuer -enddate
# 手动触发一次全量检查(未到期会自动 Skipped)
~/.acme.sh/acme.sh --cron
# 确认 crond 在运行
systemctl is-enabled crond && systemctl is-active crond九、踩坑清单(Checklist)
- 安装源:
raw.githubusercontent.com不通时,用 Gitee 镜像安装。 - DNS API 权限:RAM 子用户需显式授予云解析(Alidns)权限。
- CA 选择:权威 DNS 对境外不友好(如 hichina)时,Let's Encrypt 多视角校验易失败 → 换 ZeroSSL。
- HTTP-01:Nginx 全量 301 会拦截校验,必须放行
/.well-known/acme-challenge/。 - 危险操作:签发失败后切勿直接
--install-cert,会清空证书文件;改动前必备份。 - 续期天数:
Le_RenewalDays是"签发后 N-1 天",要"到期前 N 天"需换算成有效期 - N + 1。 - 改 Nginx:先备份、再
nginx -t、后reload。
十、经验总结
- acme.sh 是"申请 + 续期 + 部署"一站式的利器,配好之后基本可以"一劳永逸";
- 但它的续期时间语义容易误解,配置前务必实测验证,否则可能触发高频续期;
- 国内服务器要特别注意安装源与CA 的可达性;DNS 服务商对境外查询的稳定性会直接影响 Let's Encrypt 多视角校验能否通过;
- 任何证书/配置操作,先备份是底线。