别再手动续证书!泛域名证书自动续签教程

举报
阿贤Linux 发表于 2026/05/09 19:02:05 2026/05/09
【摘要】 别再手动续证书!泛域名证书自动续签教程 0 前言阿里云免费证书现已调整政策,有效期改为 3 个月,给个人站点证书续签更换带来极大不便,本文专门针对该痛点给出解决方案。# 阿里云关于免费证书服务策略调整通知https://help.aliyun.com/zh/ssl-certificate/product-overview/notice-on-adjustment-of-service-po...

别再手动续证书!泛域名证书自动续签教程

0 前言

阿里云免费证书现已调整政策,有效期改为 3 个月,给个人站点证书续签更换带来极大不便,本文专门针对该痛点给出解决方案。

# 阿里云关于免费证书服务策略调整通知
https://help.aliyun.com/zh/ssl-certificate/product-overview/notice-on-adjustment-of-service-policies-for-free-certificates
# 阿里云个人测试证书说明
https://help.aliyun.com/zh/ssl-certificate/purchase-an-individual-test-certificate

1 名词解析

1.1 泛域名解析

泛域名解析是指利用 * 做主机记录,实现所有的子域名都指向同一个IP地址(记录值)。例如域名 example.com,设置泛解析*.example.com ,则该域名下所有的子域名(如a.example.comb.example.com等)都将指向与 *.example.com相同的IP地址。

# 阿里云官网解释
https://help.aliyun.com/zh/dns/pubz-domain-name-resolution-rules

1.2 acme. sh

acme. sh:一个纯用 Shell 编写的 ACME 协议客户端

# 项目地址
https://github.com/acmesh-official/acme.sh
# 中文文档
https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E

CA支持

CA Status
[ZeroSSL. com CA](https:// github. com/acmesh-official/acme.sh/wiki/ZeroSSL.com-CA) Default
Letsencrypt .org CA ✅ Supported
[SSL. com CA](https:// github. com/acmesh-official/acme.sh/wiki/SSL.com-CA) ✅ Supported
[Google. com Public CA](https:// github .com/acmesh-official/acme.sh/wiki/Google-Public-CA) ✅ Supported
[Actalis .com CA](https:// github. com/acmesh-official/acme.sh/wiki/Actalis.com-CA) ✅ Supported
[Pebble strict Mode](https:// github. com/letsencrypt/pebble) ✅ Supported
Any [RFC8555](https:// tools .ietf .org/html/rfc8555)-compliant CA ✅ Supported

2 签发泛域名证书

2.1 安装 acme. sh

普通用户和 root 用户都可以安装使用。

[root@waluna ~]# yum install socat -y

# 安装 acme.sh
curl https://get.acme.sh | sh -s email=填写自己邮箱
# 例:
[root@waluna ~]# curl https://get.acme.sh | sh -s email=xan@waluna.top
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1032    0  1032    0     0   4134      0 --:--:-- --:--:-- --:--:--  4144
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  216k  100  216k    0     0   367k      0 --:--:-- --:--:-- --:--:--  367k
[Sun Nov 26 14:25:31 CST 2023] Installing from online archive.
[Sun Nov 26 14:25:31 CST 2023] Downloading https://github.com/acmesh-official/acme.sh/archive/master.tar.gz
[Sun Nov 26 14:25:32 CST 2023] Extracting master.tar.gz
[Sun Nov 26 14:25:32 CST 2023] Installing to /root/.acme.sh
[Sun Nov 26 14:25:32 CST 2023] Installed to /root/.acme.sh/acme.sh
[Sun Nov 26 14:25:32 CST 2023] Installing alias to '/root/.bashrc'
[Sun Nov 26 14:25:32 CST 2023] OK, Close and reopen your terminal to start using acme.sh
[Sun Nov 26 14:25:32 CST 2023] Installing alias to '/root/.cshrc'
[Sun Nov 26 14:25:32 CST 2023] Installing alias to '/root/.tcshrc'
[Sun Nov 26 14:25:32 CST 2023] Installing cron job
[Sun Nov 26 14:25:32 CST 2023] Good, bash is found, so change the shebang to use bash as preferred.
[Sun Nov 26 14:25:33 CST 2023] OK
[Sun Nov 26 14:25:33 CST 2023] Install success!
[root@waluna ~]# 

安装程序将执行3项操作:

  1. 创建并复制 acme.sh 到你的主目录($HOME):~/.acme.sh/。所有证书也会放在这个文件夹里。
  2. 创建别名:acme.sh=~/.acme.sh/acme.sh
  3. 创建每日 cron 任务,检查并续期认证。

2.2 DNS自动验证

# 阿里云DNS授权
https://ram.console.aliyun.com/users
# 腾讯云DNS授权
https://console.dnspod.cn/account/token

DNS 方式的真正强大之处在于可以使用域名解析商提供的 API 自动添加 TXT 记录,且在完成验证后删除对应的记录。

acme. sh 目前支持超过一百家的 DNS API。

以阿里云为例,生成阿里云的ID和Secret

点击创建用户

必须勾选使用永久 AccessKey 访问

在用户新建完成后,**及时保存AccessKey ID和AccessKey Secret。**下一步会用到

选择新建的dns用户,为该用户添加[AliyunDNSFullAccess(管理云解析(DNS)的权限)]权限。

2.3 签发通配符证书

Ali_KeyAli_Secret 将保存在 ~/.acme.sh/account.conf 中,并在需要时自动获取,无需手动再设置。

[root@waluna ~]# cd .acme.sh/

# 设置环境变量
[root@waluna .acme.sh]# export Ali_Key="上面保存的AccessKey ID"
[root@waluna .acme.sh]# export Ali_Secret="上面保存的AccessKey Secret"

# 生成证书
[root@waluna .acme.sh]# ./acme.sh --issue --dns dns_ali -d *.waluna.top -d waluna.top
[Sun Nov 26 15:13:12 CST 2023] Using CA: https://acme.zerossl.com/v2/DV90
[Sun Nov 26 15:13:12 CST 2023] Creating domain key
...省略...
[Sun Nov 26 15:16:55 CST 2023] Cert success.
-----BEGIN CERTIFICATE-----
MIIEBzCCA4ygAwIBAgIQBFszHZbcbAVpj9MAJBvRhDAKBggqhkjOPQQDAzBLMQsw
...省略...
f1CwLyaSsTSiNOzbvonuwiKAEgM1oSv43MyN
-----END CERTIFICATE-----
[Sun Nov 26 15:16:55 CST 2023] Your cert is in: /root/.acme.sh/*.waluna.top_ecc/*.waluna.top.cer
[Sun Nov 26 15:16:55 CST 2023] Your cert key is in: /root/.acme.sh/*.waluna.top_ecc/*.waluna.top.key
[Sun Nov 26 15:16:55 CST 2023] The intermediate CA cert is in: /root/.acme.sh/*.waluna.top_ecc/ca.cer
[Sun Nov 26 15:16:55 CST 2023] And the full chain certs is there: /root/.acme.sh/*.waluna.top_ecc/fullchain.cer
[root@waluna .acme.sh]# 

2.4 查看已申请出来证书

[root@waluna .acme.sh]# ll \*.waluna.top_ecc/
total 32
-rw-r--r-- 1 root root 2668 Nov 26 15:16 ca.cer
-rw-r--r-- 1 root root 4124 Nov 26 15:16 fullchain.cer
-rw-r--r-- 1 root root 1456 Nov 26 15:16 *.waluna.top.cer
-rw-r--r-- 1 root root  565 Nov 26 15:16 *.waluna.top.conf
-rw-r--r-- 1 root root  477 Nov 26 15:13 *.waluna.top.csr
-rw-r--r-- 1 root root  200 Nov 26 15:13 *.waluna.top.csr.conf
-rw------- 1 root root  227 Nov 26 15:13 *.waluna.top.key
[root@waluna .acme.sh]# 

2.5 将证书安装到 Apache/Nginx 中

默认情况下,证书每 60 天更新一次(可自定义)。更新证书后,Apache 或者 Nginx 服务会通过 reloadcmd 传递的命令自动重载配置。

# Apache 示例
acme.sh --install-cert -d example.com \
--cert-file      /path/to/certfile/in/apache/cert.pem  \
--key-file       /path/to/keyfile/in/apache/key.pem  \
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
--reloadcmd     "service apache2 force-reload"

# Nginx 示例
acme.sh --install-cert -d example.com \
--key-file       /path/to/keyfile/in/nginx/key.pem  \
--fullchain-file /path/to/fullchain/nginx/cert.pem \
--reloadcmd     "service nginx force-reload"

# 这里以nginx示例
[root@waluna .acme.sh]# ./acme.sh --install-cert -d *.waluna.top \
> --key-file  /usr/local/nginx/conf/cert/waluna.top.key \
> --fullchain-file /usr/local/nginx/conf/cert/waluna.top.pem \
> --reloadcmd  "systemctl restart nginx.service"
[Sun Nov 26 15:24:51 CST 2023] The domain '*.waluna.top' seems to have a ECC cert already, lets use ecc cert.
[Sun Nov 26 15:24:51 CST 2023] Installing key to: /usr/local/nginx/conf/cert/waluna.top.key
[Sun Nov 26 15:24:51 CST 2023] Installing full chain to: /usr/local/nginx/conf/cert/waluna.top.pem
[Sun Nov 26 15:24:51 CST 2023] Run reload cmd: systemctl restart nginx.service
[Sun Nov 26 15:24:51 CST 2023] Reload success
[root@waluna .acme.sh]# 

2.6 查看已安装证书信息

acme.sh --info -d example.com

# 示例
[root@waluna .acme.sh]# ./acme.sh --info -d *.waluna.top

# 会输出如下内容:
DOMAIN_CONF=/root/.acme.sh/example.com/example.com.conf
Le_Domain=example.com
Le_Alt=no
Le_Webroot=dns_ali
Le_PreHook=
Le_PostHook=
Le_RenewHook=
Le_API=https://acme-v02.api.letsencrypt.org/directory
Le_Keylength=
Le_OrderFinalize=https://acme-v02.api.letsencrypt.org/acme/finalize/23xxxx150/781xxxx4310
Le_LinkOrder=https://acme-v02.api.letsencrypt.org/acme/order/233xxx150/781xxxx4310
Le_LinkCert=https://acme-v02.api.letsencrypt.org/acme/cert/04cbd28xxxxxx349ecaea8d07
Le_CertCreateTime=1649358725
Le_CertCreateTimeStr=Thu Apr  7 19:12:05 UTC 2022
Le_NextRenewTimeStr=Mon Jun  6 19:12:05 UTC 2022
Le_NextRenewTime=1654456325
Le_RealCertPath=
Le_RealCACertPath=
Le_RealKeyPath=/etc/acme/example.com/privkey.pem
Le_ReloadCmd=service nginx force-reload
Le_RealFullChainPath=/etc/acme/example.com/chain.pem

3 证书更新

目前通过 acme. sh 生成的证书会在60天过期
安装 acme. sh 时会自动创建一个 cronjob,每天定期检查所有证书,如果证书需要更新会自动更新证书

# 1.手动更新
./acme.sh --renew -d example.com --force

# 2.自动更新
crontab -l
0 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

# 3.主程序自动更新
./acme.sh  --upgrade  --auto-upgrade

# 查看下次续签时间:
acme.sh --info -d example.com
# 看 Le_NextRenewTimeStr 字段

4 如何停止证书续订

acme.sh --remove -d example.com [--ecc]

# 示例
[root@waluna .acme.sh]# ./acme.sh --remove -d *.waluna.top -d waluna.top
[Sun Nov 26 15:10:37 CST 2023] The domain '*.waluna.top' seems to have a ECC cert already, lets use ecc cert.
[Sun Nov 26 15:10:37 CST 2023] *.waluna.top is removed, the key and cert files are in /root/.acme.sh/*.waluna.top_ecc
[Sun Nov 26 15:10:37 CST 2023] You can remove them by yourself.
[root@waluna .acme.sh]# 

# cert/密钥文件不会从磁盘中移除

# 手动删除证书
[root@waluna .acme.sh]# rm -rf \*.waluna.top_ecc/

关于我
全网可搜《阿贤Linux》
CSDN、知乎、哔哩哔哩、博客园、51CTO、掘金、思否、阿里云、腾讯云、华为云、今日头条、百家号、GitHub、个人博客
公众号:阿贤Linux
个人博客:blog. waluna.top


【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。