本文将详细介绍如何使用 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 记录。

准备工作

  1. 腾讯云账号: 你需要一个腾讯云账号,并开通 DNSPod 服务。
  2. 域名: 你需要一个已经解析到腾讯云 DNSPod 的域名。
  3. 服务器: 你需要一台可以运行 shell 脚本的服务器(Linux、macOS 或 Windows Subsystem for Linux)。
  4. 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 证书。这种方式提供了更大的灵活性,可以自定义安装路径和配置。