Nginx新增域名的免费Let’s Encrypt 证书添加

一个证书文件,支持了多个网站,间接实现了泛域名证书效果。

Nginx上的证书签发与自动续签(Linux的CertbotWindows的Win-ACME)做完成了,隔了一段时间后,要增加一个新域名站点。

这次是在Ubuntu Server 24.04环境里,操作很简单。

注册类似泛域名的证书

certbot -d x1.y -d x2.ycertbot -d x1.y,x2.y都是一个证书申请,包含有多个域名时,只有一个证书文件。一个证书文件中包含了多个域名,优缺点非常明显。

  • 优点是类似泛域名证书,运维变简单点了;
  • 缺点是这些域名证书绑在一些,未来部分域名迁移或调整,就不方便了。

可通过certbot certificates查看证书信息, certbot show_account 查看账号信息。

$ sudo certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: t725.cn
    Serial Number: xxxx
    Key Type: ECDSA
    Domains: t725.cn blog.t725.cn rp.t725.cn www.t725.cn
    Expiry Date: 2026-03-01 11:14:11+00:00 (VALID: 52 days)
    Certificate Path: /etc/letsencrypt/live/t725.cn/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/t725.cn/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

新增域名注册为独立证书

前提:新增域名已经可以公网访问,比如DNS记录、Nginx 的http访问正常。

先分别执行证书注册命令:

sudo certbot --nginx -d blog.t725.cn 
sudo certbot --nginx -d rp.t725.cn
sudo certbot --nginx -d t725.cn,www.t725.cn

再查看证书信息

$ sudo certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: blog.t725.cn
    Serial Number: xxx
    Key Type: ECDSA
    Domains: blog.t725.cn
    Expiry Date: 2026-04-08 05:04:19+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/blog.t725.cn/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/blog.t725.cn/privkey.pem
  Certificate Name: rp.t725.cn
    Serial Number: xxx
    Key Type: ECDSA
    Domains: rp.t725.cn
    Expiry Date: 2026-04-08 05:11:27+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/rp.t725.cn/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/rp.t725.cn/privkey.pem
  Certificate Name: t725.cn
    Serial Number: xxx
    Key Type: ECDSA
    Domains: t725.cn www.t725.cn
    Expiry Date: 2026-04-08 05:12:06+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/t725.cn/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/t725.cn/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

后做续签验证测试:sudo certbot renew --dry-run && sudo crontab -l

在证书上追加新域名

前提:新增域名已经可以公网访问,比如DNS记录、Nginx 的http访问正常。

在原有blog.t725.cn证书文件的基础上,增加rp.t725.cn;注意:证书还是blog.t725.cn文件,但这个证书里同时有二个域名。见下图

# --expand 参数是原有证书上,增加域名列表,此时原有域名也要加上,不能漏。成功后,检查所有站点nginx配置文件,会把原有的blog.t725.cn配置也重写了。
sudo certbot --nginx --expand -d blog.t725.cn,rp.t725.cn

更新证书上的域名(添加/撤销)

t725.cn的证书上有t725.cn rp.t725.cn www.t725.cn,现在删除rp.t725.cn

# 从证书删除 rp.t725.cn,可以在运行前后,均执行 $ sudo certbot certificates 看差异,本质是更新证书,因为前后SN与时间都变了。
$ sudo certbot --nginx --cert-name t725.cn -d t725.cn,www.t725.cn  
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
You are updating certificate t725.cn to include new domain(s):
(None)

You are also removing previously included domain(s):
- rp.t725.cn

Did you intend to make this change?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(U)pdate certificate/(C)ancel: u
Renewing an existing certificate for t725.cn and www.t725.cn

Successfully received certificate.

撤销证书

注意:sudo certbot certonly --nginx -d t725.cn,www.t725.cn是会重新注册新证书,并不是在原有证书上修改域名列表。

# revoke 撤销证书(提供 --cert-name 或 --cert-path),官方文档 https://letsencrypt.org/zh-cn/docs/revoking/
$ sudo certbot revoke --cert-name blog.t725.cn # 只支持主域名,不支持使用扩展域名
$ sudo certbot revoke --cert-path /etc/letsencrypt/live/blog.t725.cn/cert.pem

$ sudo certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
No certificates found.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

在注销证书后,调整Nginx配置,使用http可以访问,且nginx -t能成功;否则,执行重新注册证书命令会报错:

$ sudo certbot --nginx -d blog.t725.cn
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Error while running nginx -c /etc/nginx/nginx.conf -t.
  • 从nginx里注释ssl站点行内容:可考虑^的查找与替换,因为后面还得取消注释。
  • 在注册过程中,碰到提示 authorizations for these identifiers not found ,没有任何调整,过一阵子时间重新执行,注册成功。
$ sudo certbot --nginx -d rp.t725.cn
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for rp.t725.cn
An unexpected error occurred:
Error finalizing order :: authorizations for these identifiers not found: rp.t725.cn
Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.

发表回复