本文不涉及自动签发、自动更换证书,只对 acme.sh 工具的简单用法做一个记录。
acme.sh 就是一个纯 Shell 编写的 ACME 客户端,用于自动申请、续期和部署免费证书。它轻量、无依赖(只需要系统自带的 Shell 与 curl 或 wget)、兼容性极强,是 Linux 服务器上最常用的证书自动化工具之一。
附:ACME(Automatic Certificate Management Environment)是自动化证书管理协议,由 Internet Engineering Task Force 制定,主流证书机构均支持该协议。
- 默认使用 ZeroSSL,也支持 Let's Encrypt、Buypass 等。
-
支持多种验证方式
验证的目的是向证书机构证明“你拥有这个域名的控制权”。acme.sh 提供四种主流验证模式,区别在于控制权证明的位置不同:
- Webroot:在网站根目录下生成
/.well-known/acme-challenge/文件。证书机构通过 HTTP 访问该文件,若内容匹配,则证明你控制该网站目录。适合 Nginx 和 Apache HTTP Server 已运行的场景。 - Standalone:acme.sh 临时启动一个 HTTP 服务直接监听 80 端口,向证书机构返回验证内容。无需依赖现有 Web 服务,但执行期间,需要有公网 IP,且端口必须空闲。
- DNS API:通过云 DNS 接口自动创建
_acme-challengeTXT 记录。证书机构查询 DNS 完成验证,支持*.example.com通配符证书,是企业级首选方案。 - DNS Alias:将多个业务域名的验证请求统一委托到一个专用 DNS 区域。例如把
_acme-challenge.a.comCNAME 到统一管理域名,由该域负责放置 TXT 记录。适合跨账号、多租户和集中式证书管理。
- Webroot:在网站根目录下生成
-
自动续期:安装后自动创建 cron 任务,每天检查并续期。
- 支持 ECC 证书:可签发更高性能的 ECDSA 证书。
安装
curl https://get.acme.sh | sh -s email=admin@example.com
安装完成后:
- 程序目录:
~/.acme.sh/ - 主命令:
~/.acme.sh/acme.sh -
自动写入 shell 配置文件
-> % grep acme ~/.zshrc . "/home/catroll/.acme.sh/acme.sh.env" -
自动创建 cron 定时任务
-> % crontab -l 10 21 * * * "/home/catroll/.acme.sh"/acme.sh --cron --home "/home/catroll/.acme.sh" > /dev/null -> % acme.sh | grep -Fi cron --uninstall Uninstall acme.sh, and uninstall the cron job. --install-cronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job. --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically. --cron Run cron job to renew all the certs. --set-notify Set the cron notification hook, level or mode. --no-cron Only valid for '--install' command, which means: do not install the default cron job.
重新加载环境变量:
source ~/.bashrc
验证安装:
-> % acme.sh --version
https://github.com/acmesh-official/acme.sh
v3.1.3
配置
-
建议显式切换到 Let's Encrypt:
acme.sh --set-default-ca --server letsencrypt这样可以避免使用默认 CA 导致的兼容性差异。
-
我目前字用过 DNS API 的方式,这个方法最简单。
webroot 需要有公网可以访问的 HTTP 服务器。
standalone 也需要公网可以访问的地址。我是通过环境变量配置 Aliyun DNS 的 AccessKey,执行一次命令之后会记录到配置文件(
~/.acme.sh/account.conf)中:export Ali_Key="xxxx" # Access Key ID export Ali_Secret="xxxx" # Secret Key
申请证书
acme.sh --issue --dns dns_ali -d d3v.cn -d "*.d3v.cn"
至于后续证书怎么使用,Nginx 如何配置 HTTPS 就不再这里讨论了。
-> % ll ~/.acme.sh
total 280K
drwx------ 9 catroll catroll 4.0K 2026-05-19 16:11:09 .
drwxr-x--- 60 catroll catroll 4.0K 2026-05-20 10:49:09 ..
-rw------- 1 catroll catroll 571 2026-05-20 10:31:21 account.conf
-rwxrwxr-x 1 catroll catroll 230K 2026-04-03 16:45:10 acme.sh
-rw------- 1 catroll catroll 94 2026-04-03 16:45:10 acme.sh.env
drwxrwxr-x 3 catroll catroll 4.0K 2026-04-03 16:45:57 ca
drwxr-xr-x 2 catroll catroll 4.0K 2026-04-04 09:59:20 d3v.cn_ecc
drwxrwxr-x 2 catroll catroll 4.0K 2026-04-03 16:45:11 deploy
drwxrwxr-x 2 catroll catroll 4.0K 2026-04-03 16:45:10 dnsapi
-rw-rw-r-- 1 catroll catroll 533 2026-05-19 16:11:51 http.header
drwxrwxr-x 2 catroll catroll 4.0K 2026-04-03 16:45:11 notify
-> % ll ~/.acme.sh/d3v.cn_ecc
total 36K
drwxr-xr-x 2 catroll catroll 4.0K 2026-04-04 09:59:20 .
drwx------ 9 catroll catroll 4.0K 2026-05-19 16:11:09 ..
-rw-r--r-- 1 catroll catroll 1.6K 2026-05-05 21:11:06 ca.cer # CA 中间证书
-rw-r--r-- 1 catroll catroll 1.3K 2026-05-05 21:11:06 d3v.cn.cer # 域名证书
-rw------- 1 catroll catroll 608 2026-05-05 21:11:06 d3v.cn.conf # 证书配置
-rw-r--r-- 1 catroll catroll 456 2026-05-05 21:10:04 d3v.cn.csr # 证书请求文件
-rw-r--r-- 1 catroll catroll 192 2026-05-05 21:10:04 d3v.cn.csr.conf # CSR 参数配置
-rw------- 1 catroll catroll 227 2026-04-04 09:59:19 d3v.cn.key # 私钥文件
-rw-r--r-- 1 catroll catroll 2.8K 2026-05-05 21:11:06 fullchain.cer # 完整证书链
我所知道的所有 TLS 应用场景下都是提供完整证书链文件,理论上域名证书应该也可以,peer node 自行补链,但没见到这样用的,可能会有兼容性问题吧。
查看证书信息:
openssl x509 -in /home/catroll/.acme.sh/d3v.cn_ecc/d3v.cn.cer -text -noout
# 只看域名、签发单位、证书有效期
openssl x509 -in /home/catroll/.acme.sh/d3v.cn_ecc/d3v.cn.cer -text -noout | grep -E "DNS:|Not |Issuer:"
Issuer: C=US, O=Let's Encrypt, CN=E8
Not Before: May 5 12:12:35 2026 GMT
Not After : Aug 3 12:12:34 2026 GMT
DNS:*.d3v.cn, DNS:d3v.cn
续期机制
证书有效期通常为 90 天,acme.sh 会每天运行一次:
- 检查剩余有效期
- 接近到期时自动续签
- 覆盖部署文件
- 执行 reload 命令
查看 cron:
crontab -l
手动测试:
acme.sh --renew -d example.com --force
常用命令
列出证书:
acme.sh --list
查看详情:
acme.sh --info -d example.com
升级程序:
acme.sh --upgrade
卸载:
acme.sh --uninstall
删除证书:
acme.sh --remove -d example.com