主题
acme.sh
介绍
acme.sh 是一个轻量级的、纯 POSIX Shell 编写的 ACME 客户端,用于自动化申请和更新 Let's Encrypt 证书。
实操流程
1. 环境准备
bash
sudo apt update -y
sudo apt install -y socat2. 安装 acme.sh
bash
curl https://get.acme.sh | sh
# 绑定邮箱(可选)
curl https://get.acme.sh | sh -s email=my@example.com安装成功以后,会在 ~/.acme.sh 目录下生成相关文件。
3. 生成证书(多种方式)
DNS 验证
DNS 方式的真正强大之处在于可以使用域名解析商提供的 API 自动添加 TXT 记录,且在完成验证后删除对应的记录。
acme.sh 所需的最低权限如下:
- the "Zone > DNS > Edit" permission;
- permission for the specific zones/domains within which you need to perform ACME DNS challenges;
- for the IP address of the host machine running acme.sh to be allowed by "Client IP Address Filtering" (if used); and
- for the token to be active (i.e.: unexpired).
示例
创建 User Token:进入 Cloudflare Profile -> User API Tokens。
创建 Account Token:进入 Cloudflare Dashboard -> Manage Account -> Account API Tokens。

获取 Token 后,需要通过设置环境变量 CF_Token 传递给 acme.sh。
同时还需指定单个区域的 Zone ID(CF_Zone_ID),或如需为多个域名签发证书,则需指定账户的 Account ID(CF_Account_ID)。Zone ID 和 Account ID 可在 Cloudflare 控制台任意域名的“Overview”页面右侧 API 区域找到。例如,你的 Cloudflare 账户下有 example.com,在“Account Home”页面点击 example.com 进入“Overview”,右侧 API 区域即可看到 Zone ID 和 Account ID。
例如:
bash
export CF_Token="zfNp-Xm0VhSaCNun7dkLzwnw0UN7FNjaMurUZ8vf"
export CF_Account_ID="763eac4f1bcebd8b5c95e9fc50d010b4"
acme.sh --issue --dns dns_cf -d example.com -d '*.example.com' --server letsencrypt或者:
bash
export CF_Token="zfNp-Xm0VhSaCNun7dkLzwnw0UN7FNjaMurUZ8vf"
export CF_Zone_ID="3cc91d809a6ff7a93eb48877bf0ec3ef"
acme.sh --issue --dns dns_cf -d example.com -d '*.example.com' --server letsencrypt如果你有多个 Cloudflare 账户且需要为不同账户下的多个域名签发证书,则需分别设置对应的环境变量并多次执行 --issue 命令。
4. 复制证书
Nginx
bash
acme.sh --install-cert -d example.com \
--key-file /path/to/keyfile/in/nginx/example.com.key \
--fullchain-file /path/to/fullchain/nginx/example.com.cert \
--reloadcmd "service nginx reload"