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

来自个人维基
跳转到导航 跳转到搜索
Hovercool留言 | 贡献
无编辑摘要
Hovercool留言 | 贡献
无编辑摘要
第52行: 第52行:
done
done


echo&nbsp;&quot;[DONE]&nbsp;全部&nbsp;${#DOMAINS[@]}&nbsp;个域名证书更新完成&quot;</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><blockquote><p class="ds-markdown-paragraph"><span class="">⚠️ </span><strong><span class="">重要提示</span></strong><span class="">:</span><code>ossutil bucket-https</code><span class=""> 的具体参数可能因版本不同而有差异。建议先执行 </span><code>ossutil bucket-https --help</code><span class=""> 查看帮助,确认命令格式</span><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><span class="">。</span></p></blockquote><p class="ds-markdown-paragraph"><span class="">赋予脚本执行权限:</span></p><pre>chmod&nbsp;+x&nbsp;~/software/oss-key-deploy.sh</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="">6.1 手动测试脚本</span></h3><p class="ds-markdown-paragraph"><span class="">先手动执行一次,验证是否能成功推送到OSS:</span></p><pre>~/software/oss-key-deploy.sh</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>cat&nbsp;/var/log/cert_deploy.log</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><code>✅ 证书部署成功</code><span class="">,说明脚本正常。然后去阿里云OSS控制台的域名管理页面确认证书是否已更新。</span></p><h2><span class="">第七步:配置自动续期 + 自动部署</span></h2><p class="ds-markdown-paragraph"><span class="">acme.sh 支持 </span><code>--renew-hook</code><span class=""> 参数,可以在每次成功续期后自动执行自定义脚本</span><a href="https://deepwiki.com/acmesh-official/acme.sh/5.6-creating-custom-deployment-hooks" target="_blank" rel="noreferrer" style="cursor: default;"></a><span class="">。</span></p><h3><span class="">7.1 使用renew-hook重新申请证书</span></h3><pre>acme.sh&nbsp;--issue&nbsp;--dns&nbsp;dns_ali&nbsp;\
echo&nbsp;&quot;[DONE]&nbsp;全部&nbsp;${#DOMAINS[@]}&nbsp;个域名证书更新完成&quot;</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><blockquote><p class="ds-markdown-paragraph"><span class="">⚠️ </span><strong><span class="">重要提示</span></strong><span class="">:</span><code>ossutil bucket-https</code><span class=""> 的具体参数可能因版本不同而有差异。建议先执行 </span><code>ossutil bucket-https --help</code><span class=""> 查看帮助,确认命令格式</span><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><span class="">。</span></p></blockquote><p class="ds-markdown-paragraph"><span class="">赋予脚本执行权限:</span></p><pre>chmod&nbsp;+x&nbsp;~/software/oss-key-deploy.sh</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="">6.1 手动测试脚本</span></h3><p class="ds-markdown-paragraph"><span class="">先手动执行一次,验证是否能成功推送到OSS:</span></p><pre>~/software/oss-key-deploy.sh</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>cat&nbsp;/var/log/cert_deploy.log</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><code>✅ 证书部署成功</code><span class="">,说明脚本正常。然后去阿里云OSS控制台的域名管理页面确认证书是否已更新。</span></p><h2><span class="">第七步:配置自动续期 + 自动部署</span></h2><p class="ds-markdown-paragraph"><span class="">acme.sh 支持 </span><code>--<code>deploy</code>-hook</code><span class=""> 参数,可以在每次成功续期后自动执行自定义脚本</span><span class="">。</span></p><h3><span class="">7.1 使用<code>deploy</code>-hook重新申请证书</span></h3><pre>acme.sh&nbsp;--issue&nbsp;-d&nbsp;&#39;*.example.com&#39;&nbsp;\
&nbsp;&nbsp;-d&nbsp;&quot;example.com&quot;&nbsp;\
&nbsp;&nbsp;--dns&nbsp;dns_ali&nbsp;\
&nbsp;&nbsp;-d&nbsp;&quot;*.example.com&quot;&nbsp;\
&nbsp;&nbsp;--keylength&nbsp;ec-256&nbsp;\
&nbsp;&nbsp;--keylength&nbsp;ec-256&nbsp;\
&nbsp;&nbsp;--renew-hook&nbsp;&quot;~/oss-key-deploy.sh&quot;</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><ol start="1" class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">申请证书(如果已有证书则更新)</span></p></li><li><p class="ds-markdown-paragraph"><span class="">证书更新成功后,自动执行 </span><code>oss-key-deploy.sh</code><span class=""> 脚本</span></p></li></ol><h3><span class="">7.2 验证定时任务已配置</span></h3><p class="ds-markdown-paragraph"><span class="">acme.sh安装时会自动添加定时任务,每60天检查并续期证书</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><pre>crontab&nbsp;-l&nbsp;|&nbsp;grep&nbsp;acme</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>0&nbsp;0&nbsp;*&nbsp;*&nbsp;*&nbsp;&quot;/root/.acme.sh&quot;/acme.sh&nbsp;--cron&nbsp;--home&nbsp;&quot;/root/.acme.sh&quot;&nbsp;&gt;&nbsp;/dev/null</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><code>--renew-hook</code><span class=""> 脚本,将新证书推送到OSS。</span></p><p><br/></p><h2><span class="">验证与测试</span></h2><h3><span class="">1. 检查证书信息</span></h3><pre class="prettyprint">acme.sh&nbsp;--info&nbsp;-d&nbsp;example.com</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="">2. 强制测试续期</span></h3><pre>#强制更新证书(注意不要频繁调用,更新有次数限制)
&nbsp;&nbsp;--deploy-hook&nbsp;&quot;$HOME/software/oss-key-deploy.sh&quot;</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><ol start="1" class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">申请证书(如果已有证书则更新)</span></p></li><li><p class="ds-markdown-paragraph"><span class="">证书更新成功后,自动执行 </span><code>oss-key-deploy.sh</code><span class=""> 脚本</span></p></li></ol><h3><span class="">7.2 验证定时任务已配置</span></h3><p class="ds-markdown-paragraph"><span class="">acme.sh安装时会自动添加定时任务,每60天检查并续期证书</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><pre>crontab&nbsp;-l&nbsp;|&nbsp;grep&nbsp;acme</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>0&nbsp;0&nbsp;*&nbsp;*&nbsp;*&nbsp;&quot;/root/.acme.sh&quot;/acme.sh&nbsp;--cron&nbsp;--home&nbsp;&quot;/root/.acme.sh&quot;&nbsp;&gt;&nbsp;/dev/null</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><code>--deploy-hook</code><span class=""> 脚本,将新证书推送到OSS。</span></p><p><br/></p><h2><span class="">验证与测试</span></h2><h3><span class="">1. 检查证书信息</span></h3><pre class="prettyprint">acme.sh&nbsp;--info&nbsp;-d&nbsp;example.com</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="">2. 强制测试续期</span></h3><pre>#强制更新证书(注意不要频繁调用,更新有次数限制)
acme.sh&nbsp;--renew&nbsp;-d&nbsp;example.com&nbsp;--force&nbsp;--renew-hook&nbsp;&quot;~/oss-key-deploy.sh&quot;</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="">3. 浏览器访问验证</span></h3><p class="ds-markdown-paragraph"><span class="">用浏览器打开 </span><code>https://img.yourdomain.com/任意图片.jpg</code><span class="">,点击地址栏的锁图标,查看证书详情:</span></p><ul class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><strong><span class="">颁发者</span></strong><span class="">:Let&#39;s Encrypt(或ZeroSSL)</span></p></li><li><p class="ds-markdown-paragraph"><strong><span class="">有效期</span></strong><span class="">:应该是90天内</span></p></li><li><p class="ds-markdown-paragraph"><strong><span class="">使用者</span></strong><span class="">:包含 </span><code>*.yourdomain.com</code></p></li></ul><h3><span class="">4. 检查部署日志</span></h3><p><span class="d813de27">bash</span></p><pre>cat&nbsp;/var/log/cert_deploy.log</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><h2><span class="">常见问题与解决</span></h2><table><thead><tr class="firstRow"><th><span class="">问题</span></th><th><span class="">可能原因</span></th><th><span class="">解决方案</span></th></tr></thead><tbody><tr><td><span class="">申请证书失败,提示DNS错误</span></td><td><span class="">DNS解析延迟</span></td><td><span class="">添加 </span><code>--dnssleep 300</code><span class=""> 参数,等待5分钟再验证</span><a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a></td></tr><tr><td><code>ossutil bucket-https</code><span class=""> 报错</span></td><td><span class="">版本差异或参数错误</span></td><td><span class="">先执行 </span><code>ossutil bucket-https --help</code><span class=""> 查看正确格式</span></td></tr><tr><td><span class="">证书部署后浏览器不认</span></td><td><span class="">缓存问题</span></td><td><span class="">清除CDN缓存(如有),或等待5-10分钟</span></td></tr><tr><td><span class="">RAM用户权限不足</span></td><td><span class="">授权策略有遗漏</span></td><td><span class="">检查是否添加了 </span><code>AliyunDNSFullAccess</code><span class="">(DNS用)和OSS相关权限(ossutil用)</span></td></tr></tbody></table><h2><span class="">最终效果</span></h2><p class="ds-markdown-paragraph"><span class="">配置完成后,你什么都不用做。acme.sh的定时任务会自动:</span></p><ul class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">每60天检查证书有效期</span></p></li><li><p class="ds-markdown-paragraph"><span class="">到期前30天自动续期</span></p></li><li><p class="ds-markdown-paragraph"><span class="">续期成功后自动推送到OSS</span></p></li><li><p class="ds-markdown-paragraph"><span class="">你的网站图片始终保持HTTPS访问</span></p></li></ul><p class="ds-markdown-paragraph"><span class="">一次配置,终身免维护🎉</span></p><p><br/></p>
acme.sh&nbsp;--renew&nbsp;-d&nbsp;example.com&nbsp;--force&nbsp;--renew-hook&nbsp;&quot;~/oss-key-deploy.sh&quot;</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="">3. 浏览器访问验证</span></h3><p class="ds-markdown-paragraph"><span class="">用浏览器打开 </span><code>https://img.yourdomain.com/任意图片.jpg</code><span class="">,点击地址栏的锁图标,查看证书详情:</span></p><ul class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><strong><span class="">颁发者</span></strong><span class="">:Let&#39;s Encrypt(或ZeroSSL)</span></p></li><li><p class="ds-markdown-paragraph"><strong><span class="">有效期</span></strong><span class="">:应该是90天内</span></p></li><li><p class="ds-markdown-paragraph"><strong><span class="">使用者</span></strong><span class="">:包含 </span><code>*.yourdomain.com</code></p></li></ul><h3><span class="">4. 检查部署日志</span></h3><p><span class="d813de27">bash</span></p><pre>cat&nbsp;/var/log/cert_deploy.log</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><h2><span class="">常见问题与解决</span></h2><table><thead><tr class="firstRow"><th><span class="">问题</span></th><th><span class="">可能原因</span></th><th><span class="">解决方案</span></th></tr></thead><tbody><tr><td><span class="">申请证书失败,提示DNS错误</span></td><td><span class="">DNS解析延迟</span></td><td><span class="">添加 </span><code>--dnssleep 300</code><span class=""> 参数,等待5分钟再验证</span><a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a></td></tr><tr><td><code>ossutil bucket-https</code><span class=""> 报错</span></td><td><span class="">版本差异或参数错误</span></td><td><span class="">先执行 </span><code>ossutil bucket-https --help</code><span class=""> 查看正确格式</span></td></tr><tr><td><span class="">证书部署后浏览器不认</span></td><td><span class="">缓存问题</span></td><td><span class="">清除CDN缓存(如有),或等待5-10分钟</span></td></tr><tr><td><span class="">RAM用户权限不足</span></td><td><span class="">授权策略有遗漏</span></td><td><span class="">检查是否添加了 </span><code>AliyunDNSFullAccess</code><span class="">(DNS用)和OSS相关权限(ossutil用)</span></td></tr></tbody></table><h2><span class="">最终效果</span></h2><p class="ds-markdown-paragraph"><span class="">配置完成后,你什么都不用做。acme.sh的定时任务会自动:</span></p><ul class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">每60天检查证书有效期</span></p></li><li><p class="ds-markdown-paragraph"><span class="">到期前30天自动续期</span></p></li><li><p class="ds-markdown-paragraph"><span class="">续期成功后自动推送到OSS</span></p></li><li><p class="ds-markdown-paragraph"><span class="">你的网站图片始终保持HTTPS访问</span></p></li></ul><p class="ds-markdown-paragraph"><span class="">一次配置,终身免维护🎉</span></p><p><br/></p>

2026年5月29日 (五) 11:58的版本

本方案将指导你完成从零到全自动的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完全访问权限

  1. 在用户详情页,点击 权限管理添加权限

  2. 搜索框输入 AliyunDNSFullAccess,勾选这个策略

  3. 点击 确定

这个策略只授权操作云解析DNS的权限,无法访问你的OSS、ECS等其他资源<a href="https://cloud.tencent.com.cn/developer/article/2548435?policyId=1004" target="_blank" rel="noreferrer" style="cursor: default;"></a><a href="https://blog.csdn.net/weixin_29305337/article/details/158176455" target="_blank" rel="noreferrer" style="cursor: default;"></a>

第三步:配置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

<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会自动从 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" \  -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 exambucket
  2.  路径: ~/software/oss-key-deploy.sh

set -e

BUCKET="exambucket"
DOMAINS=("img.example.com" "music.example.com" "oss.example.com")

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

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

    python3 -c "
import json, sys
with open(sys.argv[1]) as f: cert = f.read()
with open(sys.argv[2]) as f: key  = f.read()
config = {
    'Cname': {
        'Domain': sys.argv[3],
        'CertificateConfiguration': {
            'Certificate': cert,
            'PrivateKey':  key,
            'Force':       True
        }
    }
}
with open(sys.argv[4], 'w') as f:
    json.dump(config, f, indent=2)
" "$CERT_PATH" "$CERT_KEY_PATH" "$DOMAIN" "$TMP_JSON"

    ossutil api put-cname --bucket "$BUCKET" --cname-configuration "file://${TMP_JSON}"
    rm -f "$TMP_JSON"
    echo "[OK] ${DOMAIN} 证书已更新"
done

echo "[DONE] 全部 ${#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>

⚠️ 重要提示ossutil bucket-https 的具体参数可能因版本不同而有差异。建议先执行 ossutil bucket-https --help 查看帮助,确认命令格式<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>

赋予脚本执行权限:

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 使用deploy-hook重新申请证书

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

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

  --deploy-hook "$HOME/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>

这条命令会:

  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 example.com --force --renew-hook "~/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>

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>

常见问题与解决

<thead></thead><tbody></tbody>
问题可能原因解决方案
申请证书失败,提示DNS错误DNS解析延迟添加 --dnssleep 300 参数,等待5分钟再验证<a href="https://developer.aliyun.com/article/1721022" target="_blank" rel="noreferrer" style="cursor: default;"></a>
ossutil bucket-https 报错版本差异或参数错误先执行 ossutil bucket-https --help 查看正确格式
证书部署后浏览器不认缓存问题清除CDN缓存(如有),或等待5-10分钟
RAM用户权限不足授权策略有遗漏检查是否添加了 AliyunDNSFullAccess(DNS用)和OSS相关权限(ossutil用)

最终效果

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

  • 每60天检查证书有效期

  • 到期前30天自动续期

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

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

一次配置,终身免维护🎉