一 腾讯云
本文将详细介绍如何使用 acme.sh 脚本,通过腾讯云的 DNS API 自动获取和续期 HTTPS 证书。我们将使用 git clone 的方式安装 acme.sh,并进行自定义配置。
另外,我的域名使用的是 IPv6 公网子域名(如 mnas.yys.zone),没有绑定固定服务器,而是通过 DDNS 解析到公网 IPv6。之前使用 Certbot 申请证书一直失败,而使用 acme.sh 成功 了。
✅ 成功平台:Windows的Linux 子系统 (Ubuntu 22.04)、Jenkins 容器(Debian 12)
❌ 失败平台:OpenResty 容器(Alpine Linux v3.21)
为什么要使用 acme.sh 和腾讯云 DNS API?
- 自动化: acme.sh 可以自动完成证书申请、验证和续期的整个过程,无需手动干预。
- 免费: acme.sh 支持 Let's Encrypt 和 ZeroSSL 等免费的证书颁发机构(CA)。
- DNS 验证: 通过 DNS API 进行验证,无需在服务器上暴露 80 端口,更安全、更灵活,尤其适用于内网服务器或没有公网 IP 的情况。
- 腾讯云集成: acme.sh 支持腾讯云的 DNSPod 服务,可以方便地管理 DNS 记录。
准备工作
- 腾讯云账号: 你需要一个腾讯云账号,并开通 DNSPod 服务。
- 域名: 你需要一个已经解析到腾讯云 DNSPod 的域名。
- 服务器: 你需要一台可以运行 shell 脚本的服务器(Linux、macOS 或 Windows Subsystem for Linux)。
- Git: 你的服务器上需要安装 Git。
步骤
apt install cron
1. 使用 Git 克隆 acme.sh
与官方提供的安装脚本不同,我们使用 Git 直接克隆 acme.sh 的仓库。这样可以更灵活地控制安装路径和更新。
git clone https://github.com/acmesh-official/acme.sh.git
cd acme.sh
2. 安装 acme.sh(自定义安装)
执行 acme.sh 仓库中的 acme.sh 脚本进行安装,并指定自定义的安装路径、配置路径和证书存储路径(根据自己路径而定):
命令1
/home/yys/.acme.sh/acme.sh --register-account -m 534640040@qq.com --server zerossl
命令2(实测可以忽略)
./acme.sh --install \ --home /home/yys/.acme.sh \ --config-home /home/yys/.acme.sh \ --cert-home /home/yys/.acme.sh/data \ --accountemail "你的邮箱@example.com"
--home: 指定 acme.sh 的安装目录。--config-home: 指定 acme.sh 的配置目录。--cert-home: 指定证书的存储目录。--accountemail: 指定你的邮箱地址,用于接收证书到期通知。/home/yys/请修改成你实际的用户目录
重要提示:
-
由于我们自定义了安装路径,以后每次运行
acme.sh命令时,都需要使用完整路径,例如/home/yys/.acme.sh/acme.sh。 -
或者,你可以将 acme.sh 的路径添加到你的
PATH环境变量中,这样就可以直接使用acme.sh命令了。 编辑你的~/.bashrc或~/.bash_profile文件(如果你使用的是其他 shell,如 zsh,则编辑~/.zshrc),添加以下一行:echo 'export PATH="$PATH:/home/yys/.acme.sh"' >> ~/.bashrc然后运行
source ~/.bashrc(或source ~/.zshrc)使更改生效。
3. 获取腾讯云 API 密钥
- 登录腾讯云控制台: 访问腾讯云官网并登录你的账号。
- 访问 API 密钥管理: 在控制台中搜索 "API 密钥" 或 "访问密钥",找到密钥管理页面。
- 创建密钥: 点击 "新建密钥",创建一个新的 SecretId 和 SecretKey。 重要提示: SecretKey 只会显示一次,请务必复制并妥善保存。
- (强烈推荐)配置子用户和权限: 为了安全起见,创建一个专门用于 DNS 验证的子用户,并仅授予该子用户
DNSPod的相关权限(QcloudDNSPodFullAccess或自定义更精细的策略)。 将 API 密钥关联到这个子用户。
4. 设置环境变量(腾讯云 API 密钥)
将你的腾讯云 SecretId 和 SecretKey 设置为环境变量。 推荐将以下两行添加到你的 ~/.bashrc 或 ~/.bash_profile 文件中:
echo 'export Tencent_SecretId="你的SecretId"' >> ~/.bashrc
echo 'export Tencent_SecretKey="你的SecretKey"' >> ~/.bashrc
然后运行 source ~/.bashrc 使更改生效。
5. 使用 acme.sh 申请证书
使用 acme.sh --issue 命令,指定 DNS 验证方式为 dns_tencent(与./acme.s/dnsapi/dns_tencent.sh 对应),以及你的域名:(注意:若ipv6域名,确保申请的主机能访问ipv6地址,容器用host)
# 如果你已经将 acme.sh 路径添加到 PATH,可以直接使用 acme.sh
/home/yys/.acme.sh/acme.sh --issue --dns dns_tencent -d 你的域名 -d '*.你的域名'
# 或者,如果你没有添加到 PATH
/home/yys/.acme.sh/acme.sh --issue --dns dns_tencent -d 你的域名 -d '*.你的域名'
申请单个域名证书,例如:
/home/yys/.acme.sh/acme.sh --issue --dns dns_tencent -d mnas.yys.zone
申请泛域名证书:
/home/yys/.acme.sh/acme.sh --issue --dns dns_tencent -d '*.yys.zone'
成功的结果(证书有效期90天):
-----END CERTIFICATE-----
[Thu Mar 13 20:11:53 CST 2025] Your cert is in: /home/yys/.acme.sh/data/mnas.yys.zone_ecc/mnas.yys.zone.cer
[Thu Mar 13 20:11:53 CST 2025] Your cert key is in: /home/yys/.acme.sh/data/mnas.yys.zone_ecc/mnas.yys.zone.key
[Thu Mar 13 20:11:53 CST 2025] The intermediate CA cert is in: /home/yys/.acme.sh/data/mnas.yys.zone_ecc/ca.cer
[Thu Mar 13 20:11:53 CST 2025] And the full-chain cert is in: /home/yys/.acme.sh/data/mnas.yys.zone_ecc/fullchain.cer
报错总结
root@de01a0dd40e0:/home/yys/acme.sh# /home/yys/.acme.sh/acme.sh --issue --dns dns_tencent -d mnas.yys.zone
[Thu Mar 13 21:07:36 CST 2025] Using CA: https://acme.zerossl.com/v2/DV90
[Thu Mar 13 21:07:36 CST 2025] Account key creation OK.
[Thu Mar 13 21:07:36 CST 2025] No EAB credentials found for ZeroSSL, let's obtain them
[Thu Mar 13 21:07:36 CST 2025] acme.sh is using ZeroSSL as default CA now.
[Thu Mar 13 21:07:36 CST 2025] Please update your account with an email address first.
[Thu Mar 13 21:07:36 CST 2025] acme.sh --register-account -m my@example.com
[Thu Mar 13 21:07:36 CST 2025] See: https://github.com/acmesh-official/acme.sh/wiki/ZeroSSL.com-CA
[Thu Mar 13 21:07:36 CST 2025] Please add '--debug' or '--log' to see more information.
[Thu Mar 13 21:07:36 CST 2025] See: https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh
这表明你之前尝试的注册可能没有成功,或者 acme.sh 的配置没有正确保存,重新注册
/home/yys/.acme.sh/acme.sh --register-account -m 534640040@qq.com --server zerossl
6. acme.sh 的自动 DNS 验证流程
acme.sh 会自动完成以下步骤:
- 连接到证书颁发机构(CA)。
- CA 要求在你的域名下添加一个特定的 TXT 记录。
- acme.sh 使用腾讯云 API 密钥,自动调用 DNSPod 的 API 添加 TXT 记录。
- 等待 DNS 记录生效。
- 通知 CA 进行验证。
- CA 验证通过后,颁发证书。
- acme.sh 下载证书并保存到
--cert-home指定的目录。 - acme.sh 自动删除 TXT 记录。
7. 证书文件
申请成功后,证书文件会保存在 /home/yys/.acme.sh/data/你的域名_ecc/ 目录下(根据你的 --cert-home 设置而定)。你会得到以下几个文件:
你的域名.cer: 你的域名证书。你的域名.key: 你的域名证书的私钥(妥善保管)。ca.cer: 中间 CA 证书。fullchain.cer: 完整的证书链(包含你的域名证书和中间 CA 证书)。
8. 自动续期
acme.sh 会自动创建一个 cron 任务,每天检查证书是否需要续期。续期过程与首次申请类似,也是自动完成的。
你也可以手动续期:
/home/yys/.acme.sh/acme.sh --renew -d 你的域名
9. 安装证书到你的服务器
你需要手动将 fullchain.cer 和 你的域名.key 复制到你的服务器上的相应位置,并配置你的 Web 服务器(例如 Nginx, Apache)以使用这些证书。
Nginx 配置示例:
server {
listen 443 ssl;
server_name 你的域名;
ssl_certificate /path/to/fullchain.cer;
ssl_certificate_key /path/to/你的域名.key;
# ... 其他配置 ...
}
然后重启openresty 或者nginx
10. 更新 acme.sh (Git 安装)
/home/yys/.acme.sh/acme.sh --upgrade
或者进入到/home/yys/.acme.sh目录,执行git pull
总结
通过本文,你学会了如何使用 git clone 安装的 acme.sh,结合腾讯云 DNS API,自动获取和管理 HTTPS 证书。这种方式提供了更大的灵活性,可以自定义安装路径和配置。
扩展:非ipv6(若上面方法都失效了)
/home/yys/.acme.sh/acme.sh --issue \
--dns dns_tencent \
-d yys.zone \
-d www.yys.zone \
--key-file /var/www/html/blog/ssl/yys.zone_ecc/yys.zone.key \
--fullchain-file /var/www/html/blog/ssl/yys.zone_ecc/fullchain.cer
二 cloudflare
参照(使用腾讯云 DNSPod 的流程),对于托管在 Cloudflare 上的域名 status.yysresume.work,核心区别在于 DNS 验证方式 从 dns_tencent 变为 dns_cf,以及环境变量需要改为 Cloudflare 的 API 信息。
以下是针对 Cloudflare 域名的具体操作步骤:
第一步:获取 Cloudflare API Key
你需要获取 Cloudflare 的 "Global API Key"(全局 API 密钥)和你的注册邮箱。
- 登录 Cloudflare 控制台。
- 点击右上角的用户头像,选择 My Profile (我的个人资料)。
- 在左侧菜单点击 API Tokens (API 令牌)。
- 找到 Global API Key,点击 "View"(查看),输入密码确获取密钥串。
第二步:设置环境变量
设置邮箱
./acme.sh --register-account -m yys.zone@outlook.com --server zerossl
在服务器终端中,导出 Cloudflare 的认证信息。
# 请将下面的 xxxx 替换为你刚才获取的 Global API Key
export CF_Key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# 请将下面的邮箱替换为你登录 Cloudflare 的邮箱
export CF_Email="yys.zone@outlook.com"
(可选:为了防止重启失效,建议像你文章里一样,把这两行加到 ~/.bashrc 文件末尾)
第三步:申请证书
使用 acme.sh 申请证书,指定 --dns dns_cf。
注意:虽然是三级域名 status.yysresume.work,你依然可以使用 DNS API 验证。
# 假设你的 acme.sh 安装路径和你文章中一致
./acme.sh --issue --dns dns_cf -d status.yysresume.work
执行过程说明:
- 脚本会自动调用 Cloudflare API 在
yysresume.work下添加一条_acme-challenge.status的 TXT 记录。 - 验证通过后,Cloudflare 会自动删除该记录。
- 证书生成成功。
第四步:安装/部署证书
证书申请成功后,生成的证书默认在 /home/yys/.acme.sh/data/status.yysresume.work_ecc/ 目录下。你需要将其安装到 Nginx/OpenResty 的指定目录(不要直接用 .acme.sh 目录下的文件,建议用 --install-cert 命令复制出来)。
# 假设你想把证书存放到 /home/yys/nginx/status.yysresume.work_ecc 目录下
# 1. 先创建目标目录
mkdir -p /home/yys/nginx/status.yysresume.work_ecc
# 2. 安装证书到该目录
/root/.acme.sh/acme.sh --install-cert -d status.yysresume.work \
--key-file /home/yys/nginx/status.yysresume.work_ecc/status.yysresume.work.key \
--fullchain-file /home/yys/nginx/status.yysresume.work_ecc/fullchain.cer \
--reloadcmd "docker restart nginx || service nginx force-reload"
(注意:--reloadcmd 请根据你实际运行的是 nginx 还是 openresty 修改,例如 systemctl reload openresty)
常见问题提示
-
关于 ZeroSSL 报错:
如果你之前遇到过文章中提到的No EAB credentials found for ZeroSSL错误,且未修复,建议在申请命令中显式指定 Let's Encrypt 作为 CA 机构(Cloudflare 代理本身也用 Let's Encrypt,兼容性好):/home/yys/.acme.sh/acme.sh --issue --server letsencrypt --dns dns_cf -d status.yysresume.work -
Cloudflare 的小云朵(代理):
使用acme.sh的 DNS API 申请证书时,无论你在 Cloudflare DNS 设置里是否开启了“小云朵”(橙色代理状态),都不影响证书的申请。但在配置 Nginx 时,请确保你的服务器防火墙允许 Cloudflare 的 IP 访问 443 端口。 -
Token 方式(进阶):
如果不想用 Global Key(权限太大),可以创建 API Token。如果使用 Token,环境变量需改为:export CF_Token="你的_API_Token" export CF_Account_ID="你的_Account_ID" # 在域名概览页右下角可以找到
评论区(0 条)
发表评论⏳ 加载编辑器…