acme.sh 自动申请并续签 SSL 证书的工具

摘要

acme.sh 介绍

  • acme.sh 实现了 acme 协议,可以从 ZeroSSL,Let’s Encrypt 等 CA 生成免费的证书。

  • 适用场景:shell脚本方式,几乎无依赖,极简,适合不想安装Python环境的人。

  • 特点:

    • 纯 Shell 脚本,单文件运行
    • 支持 100+ DNS API 自动续签(如 Cloudflare、阿里云、腾讯云等)
    • 支持通配符证书

acme.sh 安装

  • 需要先安装 crontab

1
2
3
4
5
6
7
8
# 安装 crontab
sudo dnf install cronie -y
# 启动 crond
sudo systemctl enable --now crond
# 查看 crond 状态
sudo systemctl status crond
# 查看 crontab 是否正常
crontab -l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 安装成功后会自动安装到 ~/.acme.sh/ 目录下,证书也会生成在该目录下
curl https://get.acme.sh | sh -s email=my@example.com
echo 'alias acme.sh=~/.acme.sh/acme.sh' >> ~/.bashrc
source ~/.bashrc

# 安装成功后会自动在 crontab 中添加了定时任务,每天检查一次证书是否到达下次更新日期,如果到期则自动更新
# 证书有效期默认为 90 天,还剩 30 天时会触发自动更新
crontab -l
# 输出类似于 以下内容
48 1 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

# 查看帮助
acme.sh -h
# 查看版本
acme.sh -v

升级 acme.sh

1
2
3
4
5
6
7
8
# 升级 acme.sh 到最新版
acme.sh --upgrade

# 开启自动升级
acme.sh --upgrade --auto-upgrade

# 可以随时关闭自动更新
acme.sh --upgrade --auto-upgrade 0

卸载 acme.sh

1
2
acme.sh --uninstall
rm -rf ~/.acme.sh

生成证书

  • acme.sh 实现了 acme 协议支持的所有验证协议。

  • 证书创建后会保存在 ~/.acme.sh/目录下,比如我的域名是 acme.hanqunfeng.com,则证书保存在 ~/.acme.sh/acme.hanqunfeng.com_ecc/目录下

  • 创建证书时一般有两种方式验证: HTTP 和 DNS 验证。

HTTP 验证

  • 只支持单个子域名的证书生成,不支持通配符域名

  • 需要提前将域名解析到本机的IP地址

直接签发

  • 只需要指定域名,并指定域名所在的网站根目录. acme.sh 会全自动的生成验证文件,并放到网站的根目录,验证完成后会聪明的删除验证文件,整个过程没有任何副作用。

1
2
3
4
5
# -d 指定域名,可以添加多个,--webroot 指定网站根目录
acme.sh --issue -d acme.hanqunfeng.com -d www.hanqunfeng.com --webroot /home/wwwroot/mydomain.com/

# --server 指定 acme.sh 使用的 CA 服务商为 Let's Encrypt,默认使用 ZeroSSL
acme.sh --issue -d acme.hanqunfeng.com -d www.hanqunfeng.com --webroot /home/wwwroot/mydomain.com/ --server letsencrypt

使用 Nginx/Apache 模式

  • 如果你用的 Nginx/Apache 服务器,或者反代,acme.sh 还可以智能的从 Nginx/Apache 的配置中自动完成验证,你不需要指定网站根目录

  • nginx 或 httpd 命令要在系统Path中

1
2
3
4
# -d 域名,可以添加多个, --nginx 告诉 acme.sh 使用 Nginx 模式
acme.sh --issue --nginx -d acme.hanqunfeng.com -d www.hanqunfeng.com
# -d 域名,可以添加多个, --apache 告诉 acme.sh 使用 Apache 模式
acme.sh --issue --apache -d acme.hanqunfeng.com -d www.hanqunfeng.com

使用独立服务模式

  • 如果服务器上没有运行任何 Web 服务,80 端口是空闲的,那么 acme.sh 还能假装自己是一个 WebServer,临时监听 80 端口,完成验证

  • 需要先安装 socat 命令,socat 常用于临时开启 TCP/UDP 监听端口

1
sudo dnf install socat -y
  • 生成证书,要求80端口空闲,否则会失败

1
2
# --standalone 独立服务模式
acme.sh --issue --standalone -d acme.hanqunfeng.com -d www.hanqunfeng.com

DNS 验证

  • 如果你没有服务器,没有公网 IP,只需要 DNS 的解析记录即可完成验证。

  • 支持通配符域名

手动验证

  • 这需要你手动在域名上添加一条 TXT 解析记录,验证域名所有权。

  • 注意,如果使用手动验证,acme.sh 将无法自动更新证书,每次都需要手动添加解析来验证域名所有权。如果有自动更新证书的需求,请使用自动验证(DNS API)。

1
2
# 需要加上 --yes-I-know-dns-manual-mode-enough-go-ahead-please 选项
acme.sh --issue --dns -d acme.hanqunfeng.com -d www.hanqunfeng.com --yes-I-know-dns-manual-mode-enough-go-ahead-please
  • 然后,acme.sh 会生成相应的解析记录显示出来,你只需要在你的域名管理面板中添加这条 TXT 记录即可。

1
2
3
4
5
6
7
Add the following txt record:
Domain:_acme-challenge.acme.hanqunfeng.com
Txt value:mUWNg9kuQ9hwOkqYFQ_DFMQ4Eu0CEaxxxxxxxxxxx

Add the following txt record:
Domain:_acme-challenge.www.hanqunfeng.com
Txt value:vLwDR48eHcmcScOwHrDjaFZo-yw_f9xxxxxxxxxxx
  • 等待解析完成之后,执行以下命令重新生成证书:

1
2
# 注意这里现在用的是 --renew 参数
acme.sh --renew -d acme.hanqunfeng.com -d www.hanqunfeng.com

自动验证(DNS API)

  • DNS 方式的真正强大之处在于可以使用域名解析商提供的 API 自动添加 TXT 记录,且在完成验证后删除对应的记录。

  • acme.sh 目前支持超过一百家的 DNS API

  • 以阿里云为例,登录阿里云帐号,获取 AccessKey 和 SecretKey,并设置环境变量:

1
2
3
# 只需要命令行执行一次,运行生成证书命令时会保存在 ~/.acme.sh/account.conf 中,并在需要时自动获取,无需手动再设置
export Ali_Key="<key>"
export Ali_Secret="<secret>"
  • 生成证书

1
2
# --dns dns_ali 指定阿里云的DNS API
acme.sh --issue -d acme.hanqunfeng.com -d *.hanqunfeng.com --dns dns_ali

生成证书的其它说明

1
2
# 这里使用 Let's Encrypt 的 CA 服务商
acme.sh --issue -d acme.hanqunfeng.com -d *.hanqunfeng.com --dns dns_ali --server letsencrypt
  • 也可以设置全局默认的 CA 服务商,这样就不需要每次都指定 --server

1
acme.sh --set-default-ca --server letsencrypt

小贴士

  • acme.sh 官网说 默认的 CA 服务商 ZeroSSL 不是很稳定,有时可能会导致获取证书的时候一直出现:Pending,The CA is processing your order,please just wait., 此时只需要把 CA 服务器改成 Let’s Encrypt 即可,虽然更改以后还是有概率出现 pending,但基本 2-3 次即可成功。
  • 但是 Let’s Encrypt 获取的证书,不支持比较旧的设备,比如 Android 5.0 以下的设备,如果有这方面的需要还是推荐使用 ZeroSSL。
  • 常见根证书表
根证书(CN) Android 5.0 常见签发方
ISRG Root X1 ❌ 不兼容 Let’s Encrypt
DST Root CA X3 ✅ 兼容(2021年过期) Let’s Encrypt 老版本
GlobalSign Root R1 ✅ 兼容 GlobalSign
USERTrust RSA Certification Authority ✅ 兼容 ZeroSSL, Sectigo
Starfield Root CA - G2 ❌ 不兼容 GoDaddy 新版
Starfield Root CA - G1 ✅ 兼容 GoDaddy 老版
GTS Root R1/R3 ❌ 不兼容 Google Trust Services
Amazon Root CA 1 ❌ 不兼容 Amazon Trust
  • 查看已经生成的证书

1
2
3
$ acme.sh --list
Main_Domain KeyLength SAN_Domains CA Created Renew
acme.hanqunfeng.com "ec-256" no ZeroSSL.com 2025-07-17T03:31:30Z 2025-09-14T03:31:30Z
  • 如果生成证书时失败,可以通过添加 --debug 参数查看详细错误信息

1
2
3
acme.sh --issue -d acme.hanqunfeng.com -d *.hanqunfeng.com --dns dns_ali --server letsencrypt --debug
# --debug 2 输出更为详细的信息
acme.sh --issue -d acme.hanqunfeng.com -d *.hanqunfeng.com --dns dns_ali --server letsencrypt --debug 2
  • 目前证书每 60 天自动更新,你无需任何操作。但是你也可以强制续签证书:

1
2
# 注意这里要将 --issue 改为 --renew,--issue 只有第一次生成正式时才会使用。同时加上 --force,未到更新时间强制重新生成证书。
acme.sh --renew -d acme.hanqunfeng.com -d *.hanqunfeng.com --dns dns_ali --server letsencrypt --force

生成证书后自动部署和更新

  • 上面无论是 http 还是 dns 模式,生成证书后,都会在 ~/.acme.sh/acme.hanqunfeng.com_ecc/ 目录下生成以下文件:

1
2
3
4
5
6
7
├── acme.hanqunfeng.com.cer
├── acme.hanqunfeng.com.conf
├── acme.hanqunfeng.com.csr
├── acme.hanqunfeng.com.csr.conf
├── acme.hanqunfeng.com.key
├── ca.cer
└── fullchain.cer
  • 我们可以手动将证书拷贝到真正使用证书的目录下,如果我们使用nginx或者apache,可以让 acme.sh 帮我们自动将证书拷贝到正确的目录下,并重启服务使证书生效,我们只需要通过如下命令进行设置即可,只需要运行一次命令,后续会通过 crontab 进行自动更新证书并完成部署。

  • Nginx

1
2
3
4
5
acme.sh --install-cert \
-d acme.hanqunfeng.com \ # 生成证书时指定的域名
--key-file /path/to/keyfile/in/nginx/key.pem \ # nginx 中配置的 key 文件路径
--fullchain-file /path/to/fullchain/nginx/cert.pem \ # nginx 中配置的证书文件路径
--reloadcmd "systemctl reload nginx" # nginx 重载命令,也可以使用 nginx -s reload
  • 文件对应关系

acme.sh 文件 含义 你配置的目标文件
acme.hanqunfeng.com.key 私钥 (Private Key) /path/to/keyfile/in/nginx/key.pem
fullchain.cer 证书 + 中间证书链 (Fullchain) /path/to/fullchain/nginx/cert.pem
  • Apache

1
2
3
4
5
6
acme.sh --install-cert \
-d acme.hanqunfeng.com \
--cert-file /path/to/certfile/in/apache/cert.pem \ # apache 证书文件
--key-file /path/to/keyfile/in/apache/key.pem \ # apache 密钥文件
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \ # apache 全链文件
--reloadcmd "systemctl reload httpd" # apache 重载命令
  • 文件对应关系

acme.sh 文件 含义 你配置的目标文件
acme.hanqunfeng.com.key 私钥 (Private Key) /path/to/keyfile/in/apache/key.pem
acme.hanqunfeng.com.cer 仅域名证书 (Certificate) /path/to/certfile/in/apache/cert.pem
fullchain.cer 证书 + 中间证书链 (Fullchain) /path/to/fullchain/certfile/apache/fullchain.pem

停止自动更新并删除证书

1
2
3
4
# 停止自动更新
acme.sh --remove -d acme.hanqunfeng.com --ecc
# 删除证书
rm -rf ~/.acme.sh/acme.hanqunfeng.com_ecc

其它ssl自动续签工具

工具 推荐场景 官网地址 依赖
Certbot 传统服务器、Nginx/Apache https://certbot.eff.org/ Python
Lego 静态二进制、K8s https://github.com/go-acme/lego 无依赖
cert-manager K8s 集群 https://cert-manager.io/ K8s CRD
Caddy 简单站点自动HTTPS https://caddyserver.com/ 无需单独工具
Traefik 微服务网关 https://traefik.io/ Docker/K8s