步骤一:获取 acme.sh
获取 acme.sh 主要是下载该项目的代码仓库,该仓库中包含了 acme.sh 脚本以及相关的依赖配置。
可以通过在线下载 zip 包,或者 git clone 的方式获取该仓库
git clone https://github.com/acmesh-official/acme.sh
或
git clone https://gitea.mydomain.com/acmesh-official/acme.sh
步骤二:安装 acme.sh
脚本安装会将相关工具安装到 ~/.acme.sh/ 目录中,后续生成的证书默认也会保存在该目录中。
普通用户和 root 用户都可以安装使用。
# 方式 1: 通过仓库的脚本安装
./acme.sh --install -m youremail@example.com
# 方式 2: 通过官网的域名安装
curl https://get.acme.sh | sh -s email=youremail@example.com
安装过程还创建了一个 alias 别名和添加 crontab 计划任务。
创建一个 shell 的 alias,例如 .bashrc,方便你的使用: alias acme.sh=~/.acme.sh/acme.sh
自动创建 cronjo b 计划, 每天 0:00 点自动检测所有的证书,如果快过期了,需要更新,则会自动更新证书。
安装过程不会污染已有的系统任何功能和文件,所有的修改都限制在安装目录中: ~/.acme.sh/
步骤三:选择 CA
acme.sh 脚本默认 CA 服务器是 ZeroSSL,在使用 ZeroSSL 时需要通过邮箱注册一下账号。
注册 ZeroSSL 账号
acme.sh --register-account -m youremail@example.com
有时可能会导致获取证书的时候一直出现:Pending,The CA is processing your order,please just wait. 这是因为证书签发在高峰期,需要排队等待。或者把 CA 服务器改成 Let’s Encrypt CA 签发,如果依旧提示 pending,可以多尝试几次
acme.sh --set-default-ca --server letsencrypt
步骤四:域名拥有权验证与证书签发
acme.sh 实现了 acme 协议支持的所有验证协议。一般有两种方式验证: HTTP 和 DNS 验证。 推荐使用 方案 3: 自动 DNS 验证 的验证方式。
域名验证与证书的签发过程输入日志和下文“证书续签”章节示例输出内容类似。
方案 1: 直接签发
直接签发 HTTP 验证的核心思路是在网站运行的服务器上,在网站的根目录中创建一个验证文件,通过域名+验证文件的方式进行请求判断来验证域名是否属于当前操作者。
验证完成后会删除该验证文件,对网站不会产生副作用。
acme.sh --issue \
-d yourdomain.com \
-d www.yourdomain.com \
--webroot /home/wwwroot/yourdomain.com/
方案 2: 手动 DNS 验证
如果你没有服务器,没有公网 IP,只需要 DNS 的解析记录也可完成验证。
这需要你手动在域名上添加一条 TXT 解析记录,验证域名所有权。
acme.sh --issue --dns \
-d example.com \
-d www.example.com \
-d cp.example.com
然后,acme.sh 会生成相应的解析记录显示出来,你只需要在你的域名管理面板中添加这条 TXT 记录即可。
等待解析完成之后,执行以下命令重新生成证书:
acme.sh --renew -d yourdomain.com
注意这里现在用的是 –renew 参数
方案 3: 自动 DNS 验证
DNS 方式的真正便捷之处在于可以使用域名解析商提供的 API 自动添加 TXT 记录,且在完成验证后删除对应的记录。
acme.sh 目前支持超过一百家的 DNS API。支持的 DNS 厂商和配置 KEY 名称:https://github.com/acmesh-official/acme.sh/wiki/dnsapi
藏云阁 mydomain.com 的域名托管在阿里云,这里使用阿里云的 DNS 解析服务。在阿里云 RAM 中创建一对 AKSK 密钥 https://ram.console.aliyun.com/users
然后将密钥保存在 account.conf 文件中
cat ~/.acme.sh/account.conf
Ali_Key=""
Ali_Secret=""
然后开始执行域名名验和申请
export TZ='Asia/Shanghai'
./acme.sh --issue --dns dns_ali \
-d example.com -d *.example.com
执行后就会自动执行域名的验证,证书的申请和签发
步骤五:复制证书
证书生成好以后,我们需要把证书复制给对应的 Apache、Nginx 或其他服务器去使用。
推荐使用 –install-cert 命令来把证书复制到目标文件,请勿直接使用 ~/.acme.sh/ 目录下的证书文件(由于证书需要部署到其他云场景,也可以直接复制使用),这里面的文件都是内部使用,而且目录结构将来可能会变化。
Apache 示例:
acme.sh --install-cert -d yourdoamin.com \
--cert-file /path/to/certfile/in/apache/cert.pem \
--key-file /path/to/keyfile/in/apache/key.pem \
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
--reloadcmd "service apache2 force-reload"
Nginx 示例:
acme.sh --install-cert -d yourdoamin.com \
--key-file /path/to/keyfile/in/nginx/key.pem \
--fullchain-file /path/to/fullchain/nginx/cert.pem \
--reloadcmd "service nginx reload"
Nginx 的配置项 ssl_certificate 需要使用 /etc/nginx/ssl/fullchain.cer,而非 /etc/nginx/ssl/
默认情况下,证书每 60 天更新一次(可自定义)。更新证书后,Apache 或者 Nginx 服务会通过 reloadcmd 传递的命令自动重载配置。
注意:reloadcmd 非常重要。证书会自动申请续签,但是如果没有正确的 reloadcmd 命令,证书可能无法被重新应用到 Apache 或者 Nginx,因为配置没有被重载。
查看已安装证书信息
可以通过 –info 选项查看证书信息
acme.sh --info -d yourdomain.com
会输出如下内容:
# acme.sh --info -d mydomain.com
[2025 年 05 月 12 日 星期一 09:16:12 CST] The domain 'mydomain.com' seems to already have an ECC cert, let's use it.
DOMAIN_CONF=/root/.acme.sh/mydomain.com_ecc/mydomain.com.conf
Le_Domain=mydomain.com
Le_Alt=*.mydomain.com
Le_Webroot=dns_ali
Le_PreHook=
Le_PostHook=
Le_RenewHook=
Le_API=https://acme.zerossl.com/v2/DV90
Le_Keylength=ec-256
Le_OrderFinalize=https://acme.zerossl.com/v2/DV90/order/9oSoWas2pzOqCn2r4x6dYA/finalize
Le_LinkOrder=https://acme.zerossl.com/v2/DV90/order/9oSoWas2pzOqCn2r4x6dYA
Le_LinkCert=https://acme.zerossl.com/v2/DV90/cert/DY2_aoSL66MxOZngG80o2Q
Le_CertCreateTime=1746954785
Le_CertCreateTimeStr=2025-05-11T09:13:05Z
Le_NextRenewTimeStr=2025-07-09T09:13:05Z
Le_NextRenewTime=1752052385
续签证书
证书有效期为 90 天。 acme.sh 可以每 60 天自动更新,无需任何操作。
也可以强制续签证书:
acme.sh --renew -d yourdomain.com --force
例如网站域名 mydomain.com 的续签
[root@localhost ~]# acme.sh --renew -d mydomain.com -d *.mydomain.com --force
[2025 年 05 月 11 日 星期日 17:11:15 CST] The domain 'mydomain.com' seems to already have an ECC cert, let's use it.
[2025 年 05 月 11 日 星期日 17:11:15 CST] Renewing: 'mydomain.com'
[2025 年 05 月 11 日 星期日 17:11:15 CST] Renewing using Le_API=https://acme.zerossl.com/v2/DV90
[2025 年 05 月 11 日 星期日 17:11:16 CST] Using CA: https://acme.zerossl.com/v2/DV90
[2025 年 05 月 11 日 星期日 17:11:16 CST] Multi domain='DNS:mydomain.com,DNS:*.mydomain.com'
[2025 年 05 月 11 日 星期日 17:11:34 CST] Getting webroot for domain='mydomain.com'
[2025 年 05 月 11 日 星期日 17:11:34 CST] Getting webroot for domain='*.mydomain.com'
[2025 年 05 月 11 日 星期日 17:11:34 CST] Adding TXT value: DcAgJJMBe****JIWzN5ISit6KS2ZdNPOhB_stxA for domain: _acme-challenge.mydomain.com
[2025 年 05 月 11 日 星期日 17:11:37 CST] The TXT record has been successfully added.
[2025 年 05 月 11 日 星期日 17:11:37 CST] Adding TXT value: uWuXNjS****m42C0Nxb_0NbUa-sKg3s3PPEKQbWwdw for domain: _acme-challenge.mydomain.com
[2025 年 05 月 11 日 星期日 17:11:40 CST] The TXT record has been successfully added.
[2025 年 05 月 11 日 星期日 17:11:40 CST] Let's check each DNS record now. Sleeping for 20 seconds first.
[2025 年 05 月 11 日 星期日 17:12:01 CST] You can use '--dnssleep' to disable public dns checks.
[2025 年 05 月 11 日 星期日 17:12:01 CST] See: https://github.com/acmesh-official/acme.sh/wiki/dnscheck
[2025 年 05 月 11 日 星期日 17:12:01 CST] Checking mydomain.com for _acme-challenge.mydomain.com
[2025 年 05 月 11 日 星期日 17:12:02 CST] Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: 35
[2025 年 05 月 11 日 星期日 17:12:12 CST] Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: 28
[2025 年 05 月 11 日 星期日 17:12:12 CST] Success for domain mydomain.com '_acme-challenge.mydomain.com'.
[2025 年 05 月 11 日 星期日 17:12:12 CST] Checking mydomain.com for _acme-challenge.mydomain.com
[2025 年 05 月 11 日 星期日 17:12:12 CST] Success for domain mydomain.com '_acme-challenge.mydomain.com'.
[2025 年 05 月 11 日 星期日 17:12:12 CST] All checks succeeded
[2025 年 05 月 11 日 星期日 17:12:12 CST] Verifying: mydomain.com
[2025 年 05 月 11 日 星期日 17:12:20 CST] Processing. The CA is processing your order, please wait. (1/30)
[2025 年 05 月 11 日 星期日 17:12:29 CST] Success
[2025 年 05 月 11 日 星期日 17:12:29 CST] Verifying: *.mydomain.com
[2025 年 05 月 11 日 星期日 17:12:32 CST] Processing. The CA is processing your order, please wait. (1/30)
[2025 年 05 月 11 日 星期日 17:12:36 CST] Success
[2025 年 05 月 11 日 星期日 17:12:36 CST] Removing DNS records.
[2025 年 05 月 11 日 星期日 17:12:36 CST] Removing txt: DcAgJJMBeXqOyubT***ISit6KS2ZdNPOhB_stxA for domain: _acme-challenge.mydomain.com
[2025 年 05 月 11 日 星期日 17:12:40 CST] Successfully removed
[2025 年 05 月 11 日 星期日 17:12:40 CST] Removing txt: uWuXNjSdpWRy***0Nxb_0NbUa-sKg3s3PPEKQbWwdw for domain: _acme-challenge.mydomain.com
[2025 年 05 月 11 日 星期日 17:12:43 CST] Successfully removed
[2025 年 05 月 11 日 星期日 17:12:43 CST] Verification finished, beginning signing.
[2025 年 05 月 11 日 星期日 17:12:43 CST] Let's finalize the order.
[2025 年 05 月 11 日 星期日 17:12:43 CST] Le_OrderFinalize='https://acme.zerossl.com/v2/DV90/order/9oSoWas****n2r4x6dYA/finalize'
[2025 年 05 月 11 日 星期日 17:12:47 CST] Order status is 'processing', let's sleep and retry.
[2025 年 05 月 11 日 星期日 17:12:47 CST] Sleeping for 15 seconds then retrying
[2025 年 05 月 11 日 星期日 17:13:03 CST] Polling order status: https://acme.zerossl.com/v2/DV90/order/9oSoWas2***2r4x6dYA
[2025 年 05 月 11 日 星期日 17:13:04 CST] Downloading cert.
[2025 年 05 月 11 日 星期日 17:13:04 CST] Le_LinkCert='https://acme.zerossl.com/v2/DV90/cert/DY2_aoSL6***ZngG80o2Q'
[2025 年 05 月 11 日 星期日 17:13:05 CST] Cert success.
-----BEGIN CERTIFICATE-----
MIIEDjCCA5OgAwIBAgIQDxDuiIsvck6TFvR6zUp3DAKBggqhkjOPQQDAzBLMQsw
CQYDVQQGEwJBVDEQMA4GA1UEChMHWmVyb1NTTDEqMCgGA1UEAxMhWmVyb1NTTCBF
******
wZ3A/avNZTIBIB/fxqyZA2NiJNopcEQioQX2Dwp94wIxAIc12NCpZzRQRbmOO1n1
mFVBAFS6iSdSXvzARyznUStB0sW9YuQtn55fN/gzmvMP+A==
-----END CERTIFICATE-----
[2025 年 05 月 11 日 星期日 17:13:05 CST] Your cert is in: /root/.acme.sh/mydomain.com_ecc/mydomain.com.cer
[2025 年 05 月 11 日 星期日 17:13:05 CST] Your cert key is in: /root/.acme.sh/mydomain.com_ecc/mydomain.com.key
[2025 年 05 月 11 日 星期日 17:13:05 CST] The intermediate CA cert is in: /root/.acme.sh/mydomain.com_ecc/ca.cer
[2025 年 05 月 11 日 星期日 17:13:05 CST] And the full-chain cert is in: /root/.acme.sh/mydomain.com_ecc/fullchain.cer
证书文件说明:
mydomain.com.cer:这就是签发的证书文件
mydomain.com.key:证书对应的私钥文件,在服务器上配置 https 网站时需要
ca.cer:CA 证书,mydomain.com.cer 基于该 ca.cer 签发的
fullchain.cer:证书链包含中间证书的签发过程,可用于检查证书的合法性