使用 Certbot 申请免费的 SSL 证书

SSL(Secure Sockets Layer 安全套接字协议),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议。TLS 与 SSL 在传输层与应用层之间对网络连接进行加密,确保数据发送到正确的客户机和服务器,防止数据中途被窃取,维护数据的完整性,确保数据在传输过程中不被改变… —摘自《百度百科》

百度百科

Let’s Encrypt 是一个自动签发 HTTPS 证书的免费项目,Certbot 是 Let’s Encrypt 官方推荐的证书生成客户端工具。

基本操作

准备工作

将要签发证书的域名(如 you.domain.com) 解析到外网服务器,以 CentOS 8、Nginx 为例:

Certbot.jpg

①、操作步骤概览

②、单个域名、泛域名证书

单个域名的证书生成还是比较简单的,参考官方文档即可,下面主要介绍泛域名证书的生成步骤。

Certbot Nginx SSL

因为泛域名证书需要 DNS 验证,而 Certbot 提供的 DNS 插件都是基于国外 DNS 厂商的,如 Cloudflare、Google 等,阿里云、腾讯云的 DNS 插件可以在 GitHub 上找到

certbot-dns-aliyun

certbot-dns-tencentcloud

在使用 certbot-dns-aliyun 遇到证书一直生成失败:

1
2
3
4
5
6
7
8
9
10
Certbot failed to authenticate some domains (authenticator: certbot-dns-aliyun:dns-aliyun). The Certificate Authority reported these problems:
Domain: liaofuzhan.com
Type: dns
Detail: DNS problem: SERVFAIL looking up TXT for _acme-challenge.liaofuzhan.com - the domain's nameservers may be malfunctioning

Domain: liaofuzhan.com
Type: dns
Detail: DNS problem: SERVFAIL looking up TXT for _acme-challenge.liaofuzhan.com - the domain's nameservers may be malfunctioning

Hint: The Certificate Authority failed to verify the DNS TXT records created by --certbot-dns-aliyun:dns-aliyun. Ensure the above domains are hosted by this DNS provider, or try increasing --certbot-dns-aliyun:dns-aliyun-propagation-seconds (currently 30 seconds).

解决方案:在阿里云域名管理 - DNSSEC 设置中,去掉列表下的 DS 记录。

域名系统安全扩展(DNSSEC)是添加到域名的DNS域名系统确定源域名的可靠性数字签名,并有助于防止恶意活动缓存中毒、域欺骗和拦截中的攻击。

因为这 DNS 插件需要 AliyunDNSFullAccess 权限,还需要获取 AccessKey、AccessKeySecret,因担心安全问题所以还是按照官方的来吧。

安装 Snapd

以具有 sudo 权限的用户身份 SSH 连接到运行 HTTP 网站的服务器,选择对应的操作系统,如 CenOS:

1
$ sudo yum install snapd
1
$ sudo systemctl enable --now snapd.socket
1
$ sudo ln -s /var/lib/snapd/snap /snap

确保 Snapd 是最新版本:

1
$ sudo snap install core; sudo snap refresh core

安装 Certbot

删除 Certbot Auto 和任何 Certbot OS 软件包

如果使用操作系统软件包管理器(如 apt、dnf 或 yum )安装了任何 Certbot 软件包,则应在安装 Certbot snap 之前将其删除,以确保在运行命令 Certbot 时使用的是 Certbot Snap,而不是操作系统软件包管理器自带的,命令如下:

1
$ sudo apt-get remove certbot  sudo dnf remove certbot  sudo yum remove certbot

安装 Certbot

1
$ sudo snap install --classic certbot

创建软连接

1
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot

确认插件包含级别

1
$ sudo snap set certbot trust-plugin-with-root=ok

因为不使用 DNS 插件模式,所以安装 DNS 插件的步骤跳过

除了 Manual、DNS Plugins,其它模式还有 Webroot、Standalone 等,详情请参考官方文档

下面使用 Manual 模式,执行命令

1
$ sudo certbot certonly --manual --preferred-challenges dns -d *.liaofuzhan.com

或者

1
$ sudo certbot certonly --manual --preferred-challenges dns -d *.liaofuzhan.com --server https://acme-v02.api.letsencrypt.org/directory

根据提示,在域名解析设置里分别添加两条以 _acme-challenge 开头的 TXT 记录,期间可以间隔一段时间,以期 TXT 记录生效,可以使用如下命令进行验证:

1
$ dig liaofuzhan.com TXT

或者

1
$ nslookup -type=TXT liaofuzhan.com

证书自动续期

Let’s Encrypt 颁发的免费 SSL 证书有效期是 90 天,基于 Manual 模式的不支持自动续期:

1
2
3
4
5
Certificates created using --manual do not support automatic renewal unless combined with an authentication hook script via --manual-auth-hook to automatically set up the required HTTP and/or TXT challenges.

If you can use one of the other plugins which support autorenewal to create your certificate, doing so is highly recommended.

To manually renew a certificate using --manual without hooks, repeat the same certbot --manual command you used to create the certificate originally. As this will require you to copy and paste new HTTP files or DNS TXT records, the command cannot be automated with a cron job.

其它方式颁发的会自动续期:

1
2
3
The Certbot packages on your system come with a cron job or systemd timer that will renew your certificates automatically before they expire. You will not need to run Certbot again, unless you change your configuration.

You can test automatic renewal for your certificates by running this command:

续期方案:

1
2
3
4
5
6
7
8
9
10
11
# 手动测试,查看证书过期时间
$ certbot renew

# 忽略证书过期时间,直接重置证书时间
$ certbot renew --force-renewal

# 定时任务
$ crontab -e

# 编辑内容
$ 0 0 1 * * /usr/bin/certbot renew --force-renewal

定时每天检查,如果要过期则自动延期,也可参考官方的续期方案

Renewing certificates
Automated Renewals

测试自动续期

1
$ sudo certbot renew --dry-run

如果该命令完成且没有错误,那么证书将在后台自动续订。

查看证书

1
$ certbot certificates

使用 HTTPS 证书

以 Nginx 为例:

1
2
3
4
5
6
7
8
9
10
server {

listen 80;
listen 443 ssl;
server_name you.domain.com;
ssl_certificate /etc/letsencrypt/live/you.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/you.domain.com/privkey.pem;

# ...
}

注意:一定要开放 443 端口,在阿里云服务器 - ECS 安全组,添加访问规则,如:

1
2
3
4

授权策略 优先级 协议类型 端口范围 授权对象

允许 1 自定义 TCP 目的: 443/443 源: 0.0.0.0/0

本文标题:使用 Certbot 申请免费的 SSL 证书

文章作者:北宸

发布时间:2021年09月19日 - 16:45:01

最后更新:2023年08月19日 - 13:26:00

原始链接:https://www.liaofuzhan.com/posts/712036057.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------
🌞