背景
陆陆续续自己搭了好几个站点, 为了方便顺便买了域名. 由于使用的是阿里的ECS, 我找了下发现阿里的证书都是收费的, 居然没提供 Let’s Encrypt. 本着白嫖原则就研究了下怎么自己申请证书, 于是有了这篇, 姑且也算是给自己留个指导.
本文将主要介绍:
- 手动申请/更新
Let's Encrypt证书 - 由于站点比较多, 这里申请的是泛域名证书
域名验证
你要对域名申请/更新证书,至少得证明你拥有这个域名吧? Let's Encrypt 通过以下几种方式来验证你的域名所有权:
验证方式
HTTP-01
这是最常用的一种方式, 工作原理是, Let's Encrypt 会向你申请证书的特定域名发起一个 HTTP 请求, 请求一个特定路径的文件:http://<你的域名>/.well-known/acme-challenge/<一个特定的令牌字符串>
你需要在Web服务器的/.well-known/acme-challenge/服务路径下放置一个以该字符串命名的文件, 并确保 Let's Encrypt 可以访问到它, 从而验证你的域名所有权.
DNS-01
这是功能最强大、最灵活的方式,尤其是用于通配符证书. 工作原理是, Let's Encrypt 会要求你在该域名的DNS记录中添加一个记录名和值固定的 TXT 记录, 通过验证这条记录来验证域名的所有权.
这里就需要你能够登录你的 DNS 托管商并添加 TXT 记录了.
TLS-ALPN-01
这种方式配置相对比较复杂, 工作原理是, 在 TLS 握手阶段, 通过一个特殊的 ALPN 协议标识符来验证域名的所有权, 发生在 443 端口.
由于配置复杂, 所以我没有去进一步了解了, 感兴趣的话大家可以自己去研究下.
选型
从上面我们介绍的原理其实可以很明显的看出来, HTTP-01 方式由于需要在每个站点的服务器路径下都放置一个特定文件, 那对于我们这种站点比较多的情况, 显然不是最优解. 因此这里我们会选择 DNS-01 方式, 只需要登录 DNS 托管商添加 TXT 记录即可.
当然, 由于需要登录 DNS 托管商, 添加 TXT 记录, 像我托管在 cloudflare 就没法通过脚本来实现了, 这也是标题为什么讲 手动
申请证书
Let's Encrypt 官方推荐 certbot, 依赖 python 环境, 使用起来挺方便的. 只需要一行命令:
certbot certonly --manual --preferred-challenges=dns-01
certonly仅获取证书,不自动安装或配置到服务器--manual手动模式,需要用户交互完成验证(不会自动配置服务器或 DNS)--preferred-challenges=dns-01优先使用 DNS-01 ,要求通过添加 DNS TXT 记录验证域名所有权
直接在命令行运行:
(base) [root@iZwz967itrhu893u3cdzb0Z ~]# certbot certonly --manual --preferred-challenges=dns-01
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel): saitoler.cn *.saitoler.cn
输入你要申请的域名, 这里我申请了 saitoler.cn 和 *.saitoler.cn 两个泛域名.
请注意: 你直接申请 *.saitoler.cn 它是不包含 saitoler.cn 的, 所以需要两个都写上, 中间用空格或逗号隔开即可.
Please deploy a DNS TXT record under the name:
_acme-challenge.saitoler.cn.
with the following value:
H4kFI2I8T9.............J2czLH2KwNLdf3ZHik
Press Enter to Continue
Please deploy a DNS TXT record under the name:
_acme-challenge.saitoler.cn.
with the following value:
bOdAojyAZGeO..........Uu7I2r2UlHdfVej57tM
(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)
Before continuing, verify the TXT record has been deployed. Depending on the DNS
provider, this may take some time, from a few seconds to multiple minutes. You can
check if it has finished deploying with aid of online tools, such as the Google
Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.saitoler.cn.
Look for one or more bolded line(s) below the line ';ANSWER'. It should show the
value(s) you've just added.
Press Enter to Continue
按照给出的值, 在 DNS 托管商添加 TXT 记录, 这里我们是两个域名, 所以添加了两条 TXT 记录 – 对于 TXT 记录, 是允许添加不同值的两条记录的, 尽管 key 相同.
添加完成后, 可以使用 这个网站 查询下是否生效, 或自己用 dig 命令查询
dig -t TXT _acme-challenge.example.com
查询到上面记录生效后, 就可以继续回车了.
Press Enter to Continue
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/saitoler.cn/fullchain.pem
Key is saved at: /etc/letsencrypt/live/saitoler.cn/privkey.pem
This certificate expires on 2026-01-20.
These files will be updated when the certificate renews.
NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
出现如上的输出说明证书已经申请好了, 可以去配置 nginx 了.
更新证书
证书的有效期是 90 天, 为了防止过期, 我们需要定期更新证书.
但由于我们是泛域名证书, 每次必须要重新添加 TXT 记录, 所以更新的时候使用申请时相同的命令即可