使用Let's Encrypt全自动更新阿里云oss:修订间差异

来自个人维基
跳转到导航 跳转到搜索
Hovercool留言 | 贡献
无编辑摘要
Hovercool留言 | 贡献
无编辑摘要
第46行: 第46行:
vim&nbsp;~/.acme.sh/account.conf</pre><p><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg></p><p class="ds-markdown-paragraph"><span class="">在文件末尾添加以下两行(替换成你自己的密钥):</span></p><pre>export&nbsp;Ali_Key=&quot;LTAI5txxxxxx&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;替换为你的AccessKey&nbsp;ID
vim&nbsp;~/.acme.sh/account.conf</pre><p><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg></p><p class="ds-markdown-paragraph"><span class="">在文件末尾添加以下两行(替换成你自己的密钥):</span></p><pre>export&nbsp;Ali_Key=&quot;LTAI5txxxxxx&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;替换为你的AccessKey&nbsp;ID
export&nbsp;Ali_Secret=&quot;xxxxxxxxxxxxxxxx&quot;&nbsp;&nbsp;&nbsp;#&nbsp;替换为你的AccessKey&nbsp;Secret</pre><p><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg></p><p class="ds-markdown-paragraph"><span class="">保存退出后,让配置生效:</span></p><pre>source&nbsp;~/.acme.sh/account.conf</pre><blockquote><p class="ds-markdown-paragraph"><span class="">📌 </span><strong><span class="">说明</span></strong><span class="">:acme.sh会自动从 </span><code>account.conf</code><span class=""> 读取这两个环境变量,后续所有操作无需再手动输入</span><a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a><a href="https://cloud.tencent.com.cn/developer/article/2548435?policyId=1004" target="_blank" rel="noreferrer" style="cursor: default;"></a><span class="">。</span></p><p class="ds-markdown-paragraph"><span class=""><br/></span></p></blockquote><h2><span class="">第四步:申请泛域名证书</span></h2><h3><span class="">4.1 选择CA机构(可选)</span></h3><p class="ds-markdown-paragraph"><span class="">acme.sh默认使用ZeroSSL,如果你偏爱Let&#39;s Encrypt,可以切换:</span></p><pre>acme.sh&nbsp;--set-default-ca&nbsp;--server&nbsp;letsencrypt</pre><p><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg></p><h3><span class="">4.2 执行申请命令</span></h3><p class="ds-markdown-paragraph"><span class="">假设你的域名是 </span><code>example.com</code><span class="">,图片使用的OSS域名是 </span><code>img.example.com</code><span class="">:</span></p><pre>acme.sh&nbsp;--issue&nbsp;--dns&nbsp;dns_ali&nbsp;\
export&nbsp;Ali_Secret=&quot;xxxxxxxxxxxxxxxx&quot;&nbsp;&nbsp;&nbsp;#&nbsp;替换为你的AccessKey&nbsp;Secret</pre><p><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg></p><p class="ds-markdown-paragraph"><span class="">保存退出后,让配置生效:</span></p><pre>source&nbsp;~/.acme.sh/account.conf</pre><blockquote><p class="ds-markdown-paragraph"><span class="">📌 </span><strong><span class="">说明</span></strong><span class="">:acme.sh会自动从 </span><code>account.conf</code><span class=""> 读取这两个环境变量,后续所有操作无需再手动输入</span><a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a><a href="https://cloud.tencent.com.cn/developer/article/2548435?policyId=1004" target="_blank" rel="noreferrer" style="cursor: default;"></a><span class="">。</span></p><p class="ds-markdown-paragraph"><span class=""><br/></span></p></blockquote><h2><span class="">第四步:申请泛域名证书</span></h2><h3><span class="">4.1 选择CA机构(可选)</span></h3><p class="ds-markdown-paragraph"><span class="">acme.sh默认使用ZeroSSL,如果你偏爱Let&#39;s Encrypt,可以切换:</span></p><pre>acme.sh&nbsp;--set-default-ca&nbsp;--server&nbsp;letsencrypt</pre><p><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg></p><h3><span class="">4.2 执行申请命令</span></h3><p class="ds-markdown-paragraph"><span class="">假设你的域名是 </span><code>example.com</code><span class="">,图片使用的OSS域名是 </span><code>img.example.com</code><span class="">:</span></p><pre>acme.sh&nbsp;--issue&nbsp;--dns&nbsp;dns_ali&nbsp;\
&nbsp;&nbsp;-d&nbsp;&quot;example.com&quot;&nbsp;\
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-d&nbsp;&quot;*.example.com&quot;&nbsp;\&nbsp;&nbsp;--keylength&nbsp;ec-256</pre><p><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg></p><p class="ds-markdown-paragraph"><span class="">命令说明</span><a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a><a href="https://itrustssl.cn/column/wiki/225.html" target="_blank" rel="noreferrer" style="cursor: default;"></a><span class="">:</span></p><table><thead><tr class="firstRow"><th><span class="">参数</span></th><th><span class="">含义</span></th></tr></thead><tbody><tr><td><code>--issue</code></td><td><span class="">申请新证书</span></td></tr><tr><td><code>--dns dns_ali</code></td><td><span class="">使用阿里云DNS API进行验证</span></td></tr><tr><td><code>-d &quot;example.com&quot;</code></td><td><span class="">申请主域名证书</span></td></tr><tr><td><code>-d &quot;*.example.com&quot;</code></td><td><span class="">申请泛域名证书(覆盖所有子域名)</span></td></tr><tr><td><code>--keylength ec-256</code></td><td><span class="">使用ECC加密(更安全、更快,推荐)</span></td></tr></tbody></table><blockquote><p class="ds-markdown-paragraph"><span class="">💡 </span><strong><span class="">注意</span></strong><span class="">:如果同时申请泛域名 </span><code>*.example.com</code><span class=""> 和主域名 </span><code>example.com</code><span class="">,建议两个都写上,否则访问 </span><code>https://example.com</code><span class=""> 时会提示证书不匹配</span><a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a><span class="">。</span></p></blockquote><h3><span class="">4.3 等待签发</span></h3><p class="ds-markdown-paragraph"><span class="">执行后,acme.sh会自动:</span></p><ol start="1" class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">调用阿里云DNS API,添加 </span><code>_acme-challenge.example.com</code><span class=""> 的TXT验证记录</span></p></li><li><p class="ds-markdown-paragraph"><span class="">等待CA机构验证(约10-30秒)</span></p></li><li><p class="ds-markdown-paragraph"><span class="">验证通过后,自动删除该TXT记录</span></p></li><li><p class="ds-markdown-paragraph"><span class="">证书签发完成</span></p></li></ol><p class="ds-markdown-paragraph"><span class="">看到类似以下输出表示成功:</span></p><pre>[INFO]&nbsp;Your&nbsp;cert&nbsp;is&nbsp;in:&nbsp;/root/.acme.sh/example.com/example.com.cer
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-d&nbsp;&quot;*.example.com&quot;&nbsp;\&nbsp;&nbsp;--keylength&nbsp;ec-256</pre><p><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg></p><p class="ds-markdown-paragraph"><span class="">命令说明</span><a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a><a href="https://itrustssl.cn/column/wiki/225.html" target="_blank" rel="noreferrer" style="cursor: default;"></a><span class="">:</span></p><table><thead><tr class="firstRow"><th><span class="">参数</span></th><th><span class="">含义</span></th></tr></thead><tbody><tr><td><code>--issue</code></td><td><span class="">申请新证书</span></td></tr><tr><td><code>--dns dns_ali</code></td><td><span class="">使用阿里云DNS API进行验证</span></td></tr><tr><td><code>-d &quot;example.com&quot;</code></td><td><span class="">申请主域名证书</span></td></tr><tr><td><code>-d &quot;*.example.com&quot;</code></td><td><span class="">申请泛域名证书(覆盖所有子域名)</span></td></tr><tr><td><code>--keylength ec-256</code></td><td><span class="">使用ECC加密(更安全、更快,推荐)</span></td></tr></tbody></table><blockquote><p class="ds-markdown-paragraph"><span class="">💡 </span><strong><span class="">注意</span></strong><span class="">:如果同时申请泛域名 </span><code>*.example.com</code><span class=""> 和主域名 </span><code>example.com</code><span class="">,建议两个都写上,否则访问 </span><code>https://example.com</code><span class=""> 时会提示证书不匹配</span><a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a><span class="">。</span></p></blockquote><h3><span class="">4.3 等待签发</span></h3><p class="ds-markdown-paragraph"><span class="">执行后,acme.sh会自动:</span></p><ol start="1" class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">调用阿里云DNS API,添加 </span><code>_acme-challenge.example.com</code><span class=""> 的TXT验证记录</span></p></li><li><p class="ds-markdown-paragraph"><span class="">等待CA机构验证(约10-30秒)</span></p></li><li><p class="ds-markdown-paragraph"><span class="">验证通过后,自动删除该TXT记录</span></p></li><li><p class="ds-markdown-paragraph"><span class="">证书签发完成</span></p></li></ol><p class="ds-markdown-paragraph"><span class="">看到类似以下输出表示成功:</span></p><pre>[INFO]&nbsp;Your&nbsp;cert&nbsp;is&nbsp;in:&nbsp;/root/.acme.sh/example.com/example.com.cer
[INFO]&nbsp;Your&nbsp;cert&nbsp;key&nbsp;is&nbsp;in:&nbsp;/root/.acme.sh/example.com/example.com.key
[INFO]&nbsp;Your&nbsp;cert&nbsp;key&nbsp;is&nbsp;in:&nbsp;/root/.acme.sh/example.com/example.com.key

2026年5月29日 (五) 14:23的版本

本方案将指导你完成从零到全自动的SSL证书配置,包括:

  1. 在服务器上安装acme.sh

  2. 配置阿里云DNS API密钥(权限最小化)

  3. 申请泛域名证书(一次配置,覆盖所有子域名)

  4. 编写自动部署脚本,将证书推送到OSS

  5. 配置定时任务,实现全自动续期

第一步:在服务器上安装 acme.sh

登录你的Linux服务器(使用SSH),执行以下命令:

# 安装acme.sh(自动安装在 ~/.acme.sh/ 目录)
curl https://get.acme.sh | sh -s email=你的邮箱@example.com

# 重新加载环境变量,使acme.sh命令生效
source ~/.bashrc

# 验证安装成功
acme.sh --version


安装脚本会自动创建一个每日执行的定时任务(cron job),这是未来自动续期的心脏。可以用以下命令查看:

crontab -l | grep acme

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

第二步:创建阿里云RAM子账号并授权

⚠️ 安全警告:千万不要直接使用主账号的AccessKey!必须创建权限最小化的子账号<a href="https://blog.csdn.net/weixin_29305337/article/details/158176455" target="_blank" rel="noreferrer" style="cursor: default;"></a>

2.1 创建RAM用户

  1. 登录<a href="https://ram.console.aliyun.com/" target="_blank" rel="noreferrer">阿里云RAM控制台</a>

  2. 点击 身份管理 > 用户创建用户

  3. 填写:

    • 登录名称acme-dns(可自定义)

    • 访问方式:勾选 OpenAPI调用访问(只选这一个)

  4. 点击 确定

2.2 记录AccessKey

创建成功后,系统会生成 AccessKey IDAccessKey Secret立即保存这两串字符,关闭页面后就看不到了!

示例格式:

AccessKey ID:     LTAI5txxxxxx
AccessKey Secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

2.3 授权DNS完全访问权限

直接授权AliyunDNSFullAccess,或者自定义策略(DNS编辑权限,用于域名签名验证 - 最小权限原则,推荐):

{

  "Version": "1",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "alidns:AddDomainRecord",
        "alidns:DeleteDomainRecord",
        "alidns:DescribeDomainRecords",
        "alidns:DescribeSubDomainRecords",
        "alidns:GetTxtRecordForVerify"
      ],
      "Resource": "*"
    }
  ]

}


直接授权系统策略:AliyunOSSFullAccess(OSS 完全访问权限,包含 oss:PutCname)或 自定义策略:

{

    "Version": "1",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "oss:PutCname"
            ],
            "Resource": [
                "acs:oss:*:*:your-bucket-name"
            ]
        }
    ]

}


如果你用了阿里云数字证书管理服务(云盾)的免费证书,还需要额外加:

{

    "Effect": "Allow",
    "Action": [
        "yundun-cert:DescribeSSLCertificatePrivateKey",
        "yundun-cert:DescribeSSLCertificatePublicKeyDetail",
        "yundun-cert:CreateSSLCertificate"
    ],
    "Resource": "*"

}

第三步:配置acme.sh的阿里云DNS API密钥

将刚才获取的AccessKey配置到acme.sh中:

# 编辑acme.sh的配置文件
vim ~/.acme.sh/account.conf

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

在文件末尾添加以下两行(替换成你自己的密钥):

export Ali_Key="LTAI5txxxxxx"          # 替换为你的AccessKey ID
export Ali_Secret="xxxxxxxxxxxxxxxx"   # 替换为你的AccessKey Secret

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

保存退出后,让配置生效:

source ~/.acme.sh/account.conf

📌 说明:acme.sh会自动从 account.conf 读取这两个环境变量,后续所有操作无需再手动输入<a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a><a href="https://cloud.tencent.com.cn/developer/article/2548435?policyId=1004" target="_blank" rel="noreferrer" style="cursor: default;"></a>


第四步:申请泛域名证书

4.1 选择CA机构(可选)

acme.sh默认使用ZeroSSL,如果你偏爱Let's Encrypt,可以切换:

acme.sh --set-default-ca --server letsencrypt

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

4.2 执行申请命令

假设你的域名是 example.com,图片使用的OSS域名是 img.example.com

acme.sh --issue --dns dns_ali \
      -d "*.example.com" \  --keylength ec-256

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

命令说明<a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a><a href="https://itrustssl.cn/column/wiki/225.html" target="_blank" rel="noreferrer" style="cursor: default;"></a>

<thead></thead><tbody></tbody>
参数含义
--issue申请新证书
--dns dns_ali使用阿里云DNS API进行验证
-d "example.com"申请主域名证书
-d "*.example.com"申请泛域名证书(覆盖所有子域名)
--keylength ec-256使用ECC加密(更安全、更快,推荐)

💡 注意:如果同时申请泛域名 *.example.com 和主域名 example.com,建议两个都写上,否则访问 https://example.com 时会提示证书不匹配<a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a>

4.3 等待签发

执行后,acme.sh会自动:

  1. 调用阿里云DNS API,添加 _acme-challenge.example.com 的TXT验证记录

  2. 等待CA机构验证(约10-30秒)

  3. 验证通过后,自动删除该TXT记录

  4. 证书签发完成

看到类似以下输出表示成功:

[INFO] Your cert is in: /root/.acme.sh/example.com/example.com.cer

[INFO] Your cert key is in: /root/.acme.sh/example.com/example.com.key
[INFO] The intermediate CA cert is in: /root/.acme.sh/example.com/ca.cer

[INFO] And the full chain certs is there: /root/.acme.sh/example.com/fullchain.cer

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

第五步:安装ossutil工具(用于自动推送到OSS)

5.1 下载并安装ossutil

# 下载ossutil,以下是官网方法,不放心可以直接下载 https://gosspublic.alicdn.com/ossutil/1.7.18/ossutil64
sudo curl https://gosspublic.alicdn.com/ossutil/install.sh | sudo bash

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

5.2 配置ossutil

ossutil config

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

按提示输入:

  • endpoint:你的OSS Bucket所在地域,例如 oss-cn-hangzhou.aliyuncs.com

  • accessKeyID这里需要重新创建一个RAM子账号(和DNS那个分开,权限更小)

  • stsToken:可以不输入(临时授权用)

  • accessKeySecret:对应的密钥

📌 最佳实践:建议为ossutil单独创建一个RAM子账号,只授权OSS相关权限(如 AliyunOSSFullAccess),不要复用DNS的那个密钥。

配置文件会保存在 ~/.ossutilconfig<a href="https://www.alibabacloud.com/help/zh/oss/developer-reference/config?spm=a2c63.p38356.879954.17.65d33782KrI9A4" target="_blank" rel="noreferrer" style="cursor: default;"></a>

第六步:编写自动部署脚本

创建脚本文件:

vim ~/software/oss-key-deploy.sh

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

将以下内容复制进去(根据实际情况修改变量值):

#!/bin/bash
  1.  acme.sh deploy hook - 通配符证书 *.example.com → OSS your-bucket-name

set -e
set -x

LOG_FILE="/var/log/cert_deploy.log"
exec >> "$LOG_FILE" 2>&1

BUCKET="your-bucket-name"
DOMAINS=("img.example.com" "music.example.com" "oss.example.com")

if [ ! -f "$CERT_PATH" ] || [ ! -f "$CERT_KEY_PATH" ]; then
    echo "[ERROR] 证书文件不存在: CERT_PATH=$CERT_PATH, CERT_KEY_PATH=$CERT_KEY_PATH"
    exit 1
fi

for DOMAIN in "${DOMAINS[@]}"; do
    TMP_XML=$(mktemp /tmp/oss-cname-XXXXXX.xml)

    python3 -c "
import sys
with open(sys.argv[1]) as f: cert = f.read()
with open(sys.argv[2]) as f: key  = f.read()
xml = '''<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<BucketCnameConfiguration>
  <Cname>
    <Domain>{domain}</Domain>
    <CertificateConfiguration>
      <Certificate>{cert}</Certificate>
      <PrivateKey>{key}</PrivateKey>
      <Force>true</Force>
    </CertificateConfiguration>
  </Cname>
</BucketCnameConfiguration>'''.format(domain=sys.argv[3], cert=cert, key=key)
with open(sys.argv[4], 'w') as f:
    f.write(xml)
" "$CERT_PATH" "$CERT_KEY_PATH" "$DOMAIN" "$TMP_XML"

    ossutil bucket-cname --method put --item certificate "oss://${BUCKET}" "$TMP_XML"
    rm -f "$TMP_XML"
    echo "[OK] $(date) ${DOMAIN} 证书已更新"
done

echo "[DONE] $(date) 全部 ${#DOMAINS[@]} 个域名证书更新完成"

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

赋予脚本执行权限:

chmod +x ~/software/oss-key-deploy.sh

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

6.1 手动测试脚本

先手动执行一次,验证是否能成功推送到OSS:

~/software/oss-key-deploy.sh

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

检查日志:

cat /var/log/cert_deploy.log

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

如果看到 ✅ 证书部署成功,说明脚本正常。然后去阿里云OSS控制台的域名管理页面确认证书是否已更新。

第七步:配置自动续期 + 自动部署

acme.sh 支持 --deploy-hook 参数,可以在每次成功续期后自动执行自定义脚本

7.1 使用renew-hook重新申请证书

acme.sh --issue -d '*.example.com' \

  --dns dns_ali \
  --keylength ec-256 \

  --renew-hook "$HOME/software/oss-key-deploy.sh"

这条命令会:

  1. 申请证书(如果已有证书则更新)

  2. 证书更新成功后,自动执行 oss-key-deploy.sh 脚本

7.2 验证定时任务已配置

acme.sh安装时会自动添加定时任务,每60天检查并续期证书<a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a><a href="https://cloud.tencent.com.cn/developer/article/2548435?policyId=1004" target="_blank" rel="noreferrer" style="cursor: default;"></a>

crontab -l | grep acme

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

应该能看到类似这样的输出:

0 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

这个定时任务会在每天零点执行,检查所有证书是否需要续期。当续期发生时,会自动触发 --deploy-hook 脚本,将新证书推送到OSS。


验证与测试

1. 检查证书信息

acme.sh --info -d *.example.com

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

2. 强制测试续期

#强制更新证书(注意不要频繁调用,更新有次数限制)
acme.sh --renew -d '*.wordstaste.com' --force

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

3. 浏览器访问验证

用浏览器打开 https://img.yourdomain.com/任意图片.jpg,点击地址栏的锁图标,查看证书详情:

  • 颁发者:Let's Encrypt(或ZeroSSL)

  • 有效期:应该是90天内

  • 使用者:包含 *.yourdomain.com


4. 检查部署日志

bash

cat /var/log/cert_deploy.log

<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _33882ae"></svg><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewbox="0 0 12 12" fill="none" class="_9bc997d _28d7e84"></svg>

最终效果

配置完成后,你什么都不用做。acme.sh的定时任务会自动:

  • 每60天检查证书有效期

  • 到期前30天自动续期

  • 续期成功后自动推送到OSS

  • 你的网站图片始终保持HTTPS访问

一次配置,终身免维护🎉