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

来自个人维基
跳转到导航 跳转到搜索
Hovercool留言 | 贡献
无编辑摘要
Hovercool留言 | 贡献
无编辑摘要
第1行: 第1行:
<p class="ds-markdown-paragraph"><span class="">本方案将指导你完成从零到全自动的SSL证书配置,包括:</span></p><ol start="1" class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">在服务器上安装acme.sh</span></p></li><li><p class="ds-markdown-paragraph"><span class="">配置阿里云DNS API密钥(权限最小化)</span></p></li><li><p class="ds-markdown-paragraph"><span class="">申请泛域名证书(一次配置,覆盖所有子域名)</span></p></li><li><p class="ds-markdown-paragraph"><span class="">编写自动部署脚本,将证书推送到OSS</span></p></li><li><p class="ds-markdown-paragraph"><span class="">配置定时任务,实现全自动续期</span></p></li></ol><h2><span class="">第一步:在服务器上安装 acme.sh</span></h2><p class="ds-markdown-paragraph"><span class="">登录你的Linux服务器(使用SSH),执行以下命令:</span></p><pre class="prettyprint">#&nbsp;安装acme.sh(自动安装在&nbsp;~/.acme.sh/&nbsp;目录)
<p class="ds-markdown-paragraph"><span class="">本方案将指导你完成从零到全自动的SSL证书配置,包括:</span></p><ol start="1" class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">在服务器上安装acme.sh</span></p></li><li><p class="ds-markdown-paragraph"><span class="">配置阿里云DNS API密钥(权限最小化)</span></p></li><li><p class="ds-markdown-paragraph"><span class="">申请泛域名证书(一次配置,覆盖所有子域名)</span></p></li><li><p class="ds-markdown-paragraph"><span class="">编写自动部署脚本,将证书推送到OSS</span></p></li><li><p class="ds-markdown-paragraph"><span class="">配置定时任务,实现全自动续期</span></p></li></ol><h2><span class="">第一步:在服务器上安装 acme.sh</span></h2><p class="ds-markdown-paragraph"><span class="">登录你的Linux服务器(使用SSH),执行以下命令:</span></p><pre>#&nbsp;安装acme.sh(自动安装在&nbsp;~/.acme.sh/&nbsp;目录)
curl&nbsp;https://get.acme.sh&nbsp;|&nbsp;sh&nbsp;-s&nbsp;email=你的邮箱@example.com
curl&nbsp;https://get.acme.sh&nbsp;|&nbsp;sh&nbsp;-s&nbsp;email=你的邮箱@example.com


第9行: 第9行:
AccessKey&nbsp;Secret:&nbsp;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</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.3 授权DNS完全访问权限</span></h3><ol start="1" class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">在用户详情页,点击 </span><strong><span class="">权限管理</span></strong><span class=""> → </span><strong><span class="">添加权限</span></strong></p></li><li><p class="ds-markdown-paragraph"><span class="">搜索框输入 </span><code>AliyunDNSFullAccess</code><span class="">,勾选这个策略</span></p></li><li><p class="ds-markdown-paragraph"><span class="">点击 </span><strong><span class="">确定</span></strong></p></li></ol><blockquote><p class="ds-markdown-paragraph"><span class="">这个策略只授权操作云解析DNS的权限,无法访问你的OSS、ECS等其他资源</span><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><span class="">。</span></p></blockquote><h2><span class="">第三步:配置acme.sh的阿里云DNS API密钥</span></h2><p class="ds-markdown-paragraph"><span class="">将刚才获取的AccessKey配置到acme.sh中:</span></p><pre>#&nbsp;编辑acme.sh的配置文件
AccessKey&nbsp;Secret:&nbsp;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</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.3 授权DNS完全访问权限</span></h3><ol start="1" class=" list-paddingleft-2"><li><p class="ds-markdown-paragraph"><span class="">在用户详情页,点击 </span><strong><span class="">权限管理</span></strong><span class=""> → </span><strong><span class="">添加权限</span></strong></p></li><li><p class="ds-markdown-paragraph"><span class="">搜索框输入 </span><code>AliyunDNSFullAccess</code><span class="">,勾选这个策略</span></p></li><li><p class="ds-markdown-paragraph"><span class="">点击 </span><strong><span class="">确定</span></strong></p></li></ol><blockquote><p class="ds-markdown-paragraph"><span class="">这个策略只授权操作云解析DNS的权限,无法访问你的OSS、ECS等其他资源</span><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><span class="">。</span></p></blockquote><h2><span class="">第三步:配置acme.sh的阿里云DNS API密钥</span></h2><p class="ds-markdown-paragraph"><span class="">将刚才获取的AccessKey配置到acme.sh中:</span></p><pre>#&nbsp;编辑acme.sh的配置文件
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><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="">: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></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;-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;The&nbsp;intermediate&nbsp;CA&nbsp;cert&nbsp;is&nbsp;in:&nbsp;/root/.acme.sh/example.com/ca.cer[INFO]&nbsp;And&nbsp;the&nbsp;full&nbsp;chain&nbsp;certs&nbsp;is&nbsp;there:&nbsp;/root/.acme.sh/example.com/fullchain.cer</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="">第五步:安装ossutil工具(用于自动推送到OSS)</span></h2><h3><span class="">5.1 下载并安装ossutil</span></h3><pre>#&nbsp;下载ossutil
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><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="">: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></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;-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;The&nbsp;intermediate&nbsp;CA&nbsp;cert&nbsp;is&nbsp;in:&nbsp;/root/.acme.sh/example.com/ca.cer
[INFO]&nbsp;And&nbsp;the&nbsp;full&nbsp;chain&nbsp;certs&nbsp;is&nbsp;there:&nbsp;/root/.acme.sh/example.com/fullchain.cer</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="">第五步:安装ossutil工具(用于自动推送到OSS)</span></h2><h3><span class="">5.1 下载并安装ossutil</span></h3><pre>#&nbsp;下载ossutil
wget&nbsp;&nbsp;
wget&nbsp;&nbsp;
#&nbsp;添加执行权限
#&nbsp;添加执行权限

2026年5月29日 (五) 09:38的版本

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

wget  

  1.  添加执行权限

chmod +x ossutil64

  1.  移动到系统路径
sudo mv ossutil64 /usr/local/bin/ossutil

<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

bash

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 ~/deploy_cert_to_oss.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.  ========== 请修改变量值 ==========

DOMAIN="example.com"                         # 你的主域名
BUCKET_NAME="your-bucket-name"               # OSS Bucket名称

  1.  列出所有需要更新证书的OSS自定义域名(用空格分隔)

OSS_DOMAINS="img.example.com music.example.com oss.example.com"

  1.  =================================
  1.  证书文件路径(泛域名证书,所有子域名共用)

CERT_PATH="/root/.acme.sh/${DOMAIN}/fullchain.cer"
KEY_PATH="/root/.acme.sh/${DOMAIN}/${DOMAIN}.key"

LOG_FILE="/var/log/cert_deploy.log"

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> ${LOG_FILE}
}

log "开始部署证书到OSS(共 $(echo ${OSS_DOMAINS} | wc -w) 个域名)"

for OSS_DOMAIN in ${OSS_DOMAINS}; do
    log "正在部署: ${OSS_DOMAIN}"
    
    ossutil bucket-https \
        --method put \
        --bucket ${BUCKET_NAME} \
        --cert ${CERT_PATH} \
        --key ${KEY_PATH} \
        --domain ${OSS_DOMAIN} \
        --force >> ${LOG_FILE} 2>&1
    
    if [ $? -eq 0 ]; then
        log "✅ ${OSS_DOMAIN} 证书部署成功"
    else
        log "❌ ${OSS_DOMAIN} 证书部署失败"
    fi
done

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>

⚠️ 重要提示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 ~/deploy_cert_to_oss.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:

~/deploy_cert_to_oss.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 支持 --renew-hook 参数,可以在每次成功续期后自动执行自定义脚本<a href="https://deepwiki.com/acmesh-official/acme.sh/5.6-creating-custom-deployment-hooks" target="_blank" rel="noreferrer" style="cursor: default;"></a>

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

acme.sh --issue --dns dns_ali \

  -d "example.com" \
  -d "*.example.com" \
  --keylength ec-256 \

  --renew-hook "~/deploy_cert_to_oss.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. 证书更新成功后,自动执行 deploy_cert_to_oss.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>

这个定时任务会在每天零点执行,检查所有证书是否需要续期。当续期发生时,会自动触发 --renew-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 "~/deploy_cert_to_oss.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访问

一次配置,终身免维护🎉