acme.sh 是一个纯 Shell 实现的 ACME 协议客户端,支持自动申请和续期 SSL 证书,兼容 Let’s Encrypt、ZeroSSL、Buypass 等多个 CA。

目录


1. 安装与基础命令

命令 说明
curl https://get.acme.sh | sh 安装 acme.sh
acme.sh --version / -v 查看版本信息
acme.sh --upgrade 检查并更新脚本
acme.sh --uninstall 卸载 acme.sh
acme.sh --help / -h 显示帮助信息

安装注意事项:

  • 安装后需要重新加载 Shell 配置:source ~/.bashrc 或重启终端
  • 默认安装在 ~/.acme.sh/ 目录
  • 会自动创建 cron 任务用于自动续期

2. 证书申请命令

2.1 常用验证模式

命令 说明
acme.sh --issue -d example.com -w /var/www/html Webroot 模式(推荐,不中断服务)
acme.sh --issue --standalone -d example.com Standalone 模式(需占用 80 端口)
acme.sh --issue --alpn -d example.com TLS-ALPN 模式(需占用 443 端口)
acme.sh --issue --apache -d example.com Apache 模式(自动配置)
acme.sh --issue --nginx -d example.com Nginx 模式(自动配置)
acme.sh --issue --dns dns_cf -d example.com -d '*.example.com' DNS API 模式(支持泛域名)

2.2 常用参数

参数 说明
-d / --domain 指定域名,可多次使用申请多域名证书
-w / --webroot 指定网站根目录
--keylength 指定密钥类型(ec-256ec-38420484096 等)
--staging 使用测试环境(避免触发频率限制)
--server 指定 CA 服务器(letsencryptzerosslbuypasssslcomgoogle 等)
--force 强制重新申请证书
--dns 指定 DNS API 提供商(如 dns_cfdns_alidns_dp
--renew-hook 设置续期成功后的钩子命令

2.3 模式选择建议

场景 推荐模式 原因
已有 Web 服务器运行 Webroot 不占用额外端口,不中断服务
无 Web 服务器 Standalone 独立运行,配置简单
申请泛域名证书 DNS API 支持 *.example.com
Apache/Nginx 环境 Apache/Nginx 模式 自动配置服务器
内网/无法暴露 80 端口 DNS API / TLS-ALPN 无需 HTTP 验证

3. 证书管理命令

命令 说明
acme.sh --list 列出所有证书
acme.sh --renew -d example.com 手动更新证书
acme.sh --renew -d example.com --force 强制更新证书
acme.sh --revoke -d example.com 吊销证书
acme.sh --remove -d example.com 删除证书(保留文件)
acme.sh --cron 手动执行自动续期检查

ECC 证书操作:

  • 上述命令加上 --ecc 参数即可管理 ECC 类型证书
  • 示例:acme.sh --list --ecc 列出所有 ECC 证书

4. 证书安装与部署

4.1 安装证书到指定路径

1
2
3
4
acme.sh --install-cert -d example.com \
--key-file /etc/nginx/ssl/example.com.key.pem \
--fullchain-file /etc/nginx/ssl/example.com.cert.pem \
--reloadcmd "nginx -s reload"

4.2 安装参数说明

参数 说明
--key-file 私钥文件路径
--cert-file 证书文件路径(不含中间证书)
--fullchain-file 完整证书链路径(推荐用于 Nginx)
--ca-file 中间证书路径
--reloadcmd 证书更新后执行的重载命令

4.3 自定义部署钩子

1
acme.sh --deploy -d example.com --deploy-hook custom_deploy

5. 账户管理命令

命令 说明
acme.sh --register-account -m email@example.com 注册账户(ZeroSSL 必需)
acme.sh --register-account -m email@example.com --server zerossl 指定 CA 注册账户
acme.sh --update-account --email new@example.com 更新账户邮箱
acme.sh --deactivate-account 注销账户

注意事项:

  • ZeroSSL 和 Google Trust Services 需要注册账户
  • Let’s Encrypt 默认不需要显式注册

6. 调试与测试

命令 说明
acme.sh --issue -d example.com --debug 2 调试模式(显示详细日志)
acme.sh --issue -d example.com --staging 使用测试服务器
acme.sh --cron --debug 调试自动续期任务

DNS 验证调试

1
2
3
4
5
# 检查 DNS TXT 记录是否生效
dig +short TXT _acme-challenge.example.com @8.8.8.8

# 使用指定 DNS 服务器检查
nslookup -q=txt _acme-challenge.example.com 8.8.8.8

7. 常用环境变量配置

Cloudflare DNS API

1
2
3
4
5
export CF_Key="your_api_key"
export CF_Email="your_email"

# 或使用 API Token(推荐,权限更小)
export CF_Token="your_api_token"

阿里云 DNS API

1
2
export Ali_Key="your_access_key"
export Ali_Secret="your_secret"

DNSPod (腾讯云)

1
2
export DP_Id="your_id"
export DP_Key="your_key"

其他常用 DNS 提供商

提供商 环境变量
GoDaddy GD_Key, GD_Secret
AWS Route53 AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
华为云 HUAWEICLOUD_AK, HUAWEICLOUD_SK
百度云 BAIDUCLOUD_APIKEY, BAIDUCLOUD_SECRET

永久配置: 将环境变量写入 ~/.acme.sh/account.conf,acme.sh 会自动读取。


8. 安全与权限

注意事项 说明
不要直接引用内部目录 证书文件存储在 ~/.acme.sh/ 下,目录结构可能变化,务必使用 --install-cert 复制到目标位置
权限设置 确保证书文件权限正确(通常 600 或 644),避免私钥泄露
API Key 安全 DNS API 的密钥不要硬编码在脚本中,建议使用环境变量或配置文件
账户备份 定期备份 ~/.acme.sh/ 目录,包含账户密钥和证书

9. 自动续期

注意事项 说明
自动续期已内置 acme.sh 安装时会自动添加 cron 任务,默认每天检查两次
手动测试续期 可用 acme.sh --cron 手动测试续期流程
续期后重载服务 务必配置 --reloadcmd 让 Web 服务器重新加载新证书
检查 cron 日志 确保护书续期失败时能收到通知

查看自动续期任务

1
2
3
4
5
# 查看 crontab
crontab -l | grep acme

# 查看 acme.sh 的定时任务
cat ~/.acme.sh/acme.sh.cron

10. CA 服务器选择

CA 特点 是否需要注册
Let’s Encrypt 默认 CA,免费,社区支持最好
ZeroSSL 免费,支持 ACME v2,国内访问较好
Buypass 部分证书有效期更长(180天)
SSL.com 提供免费 90 天证书
Google Trust Services Google 旗下,稳定性高

切换 CA

1
2
3
4
5
6
7
8
# 申请时指定 CA
acme.sh --issue -d example.com --server zerossl

# 设置默认 CA
acme.sh --set-default-ca --server zerossl

# 切换回 Let's Encrypt
acme.sh --set-default-ca --server letsencrypt

11. 实战示例

示例 1:Webroot 模式申请证书

1
2
3
4
5
6
7
8
# 申请证书
acme.sh --issue -d example.com -d www.example.com -w /var/www/html

# 安装到 Nginx
acme.sh --install-cert -d example.com \
--key-file /etc/nginx/ssl/example.com.key.pem \
--fullchain-file /etc/nginx/ssl/example.com.cert.pem \
--reloadcmd "nginx -s reload"

示例 2:DNS API 申请泛域名证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 配置 Cloudflare API
export CF_Token="your_api_token"

# 申请泛域名证书
acme.sh --issue --dns dns_cf \
-d example.com \
-d '*.example.com' \
--keylength ec-256

# 安装证书
acme.sh --install-cert -d example.com --ecc \
--key-file /etc/nginx/ssl/example.com.key.pem \
--fullchain-file /etc/nginx/ssl/example.com.cert.pem \
--reloadcmd "systemctl reload nginx"

示例 3:ECC 证书申请

1
2
3
4
5
6
7
8
9
# 申请 ECC P-256 证书(更高效)
acme.sh --issue -d example.com \
--standalone \
--keylength ec-256

# 申请 ECC P-384 证书(更高安全)
acme.sh --issue -d example.com \
--standalone \
--keylength ec-384

示例 4:多域名证书

1
2
3
4
5
6
7
# 一个证书包含多个域名
acme.sh --issue -d example.com \
-d www.example.com \
-d blog.example.com \
-d shop.example.com \
-w /var/www/html \
--keylength ec-256

12. 常见问题

Q1: 证书续期失败怎么办?

1
2
3
4
5
6
7
8
# 检查日志
acme.sh --cron --debug

# 强制续期
acme.sh --renew -d example.com --force

# 如果 DNS 验证失败,检查 DNS 记录是否生效
dig +short TXT _acme-challenge.example.com

Q2: 如何迁移到新服务器?

1
2
3
4
5
6
7
8
# 原服务器:备份
tar czvf acme-backup.tar.gz ~/.acme.sh/

# 新服务器:恢复
tar xzvf acme-backup.tar.gz -C ~/

# 重新安装并恢复
curl https://get.acme.sh | sh

Q3: 如何禁用自动续期某个证书?

1
2
3
4
5
6
# 停止自动续期(但不删除证书)
acme.sh --renew -d example.com --stop-renew-on-error

# 或者删除证书
cd ~/.acme.sh/
rm -rf example.com/

Q4: 续期后服务没有自动重载?

1
2
3
4
5
6
7
8
# 检查 reloadcmd 是否正确配置
acme.sh --list

# 重新配置安装
acme.sh --install-cert -d example.com \
--key-file /path/to/key.pem \
--fullchain-file /path/to/cert.pem \
--reloadcmd "systemctl reload nginx"

参考链接


⚠️ 重要提示:不要将证书文件直接指向 ~/.acme.sh/ 目录下的文件,这些文件仅供 acme.sh 内部使用,目录结构可能会变化。务必使用 --install-cert 命令将证书复制到目标位置。