Let's Encrypt 证书生成
简介
Let’s Encrypt
是一家免费、开放、自动化的证书颁发机构(CA),为公众的利益而运行。 它是一项由 Internet Security Research Group (ISRG) 提供的服务。
我们以尽可能对用户友好的方式免费提供为网站启用 HTTPS(SSL/TLS)所需的数字证书。 这是因为我们想要创建一个更安全,更尊重隐私的 Web 环境。
您可以通过下载我们的 年度报告 阅读我们最近一年的评论。
Let’s Encrypt的关键原则为:
- 免费: 任何拥有域名的人都可以使用
Let’s Encrypt
免费获取受信的证书。 - 自动化: 运行于服务器上的软件可以与
Let’s Encrypt
直接交互,以便轻松获取证书,安全地配置它,并自动进行续期。 - 安全:
Let’s Encrypt
将成为一个推动 TLS 安全最佳实践发展的的平台,无论是作为一个证书颁发机构(CA)还是通过帮助网站运营商正确地保护其服务器。 - 透明: 所有颁发或吊销的证书将被公开记录,供任何人查阅。
- 开放: 自动签发和续订协议 已经发布 作为其他人可以采用的开放标准。
- 乐于合作: 就像互联网底层协议本身一样,
Let’s Encrypt
是为了让整个互联网社区受益而做出的共同努力,它不受任何单一组织的控制。
使用指南
简单总结:
- 推荐使用 Certbot 工具,自动管理(前提是后后台管理权限)
- 使用集成了
Let's Encrypt
服务商,搭建网站 - 手动模式,同样是 Certoot ,找一台有权限的机器,生成证书然后上传。
重点学习之 Certbot✨
安装
https://certbot.eff.org/instructions 根据网站提供,选择的你网站类型、系统类型会给你提供安装方式
Docker 方式
sudo docker run -it --rm --name certbot \
-v "/etc/letsencrypt:/etc/letsencrypt" \
-v "/var/lib/letsencrypt:/var/lib/letsencrypt" \
certbot/certbot certonly
启动会执行 certonly,进入问答模式(交互模式),然后根据自己的需要选择内容, 如果您将 Certbot
与standalone
插件一起使用,则需要通过包含类似 -p 80:80
或 -p 443:443
之前命令行的内容来使其使用的端口从容器外部访问 certbot/certbot
Standalone 模式
Standalone 模式 没有测试成功
Saving debug log to /var/log/letsencrypt/letsencrypt.log
How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Spin up a temporary webserver (standalone) #会在本地启用一个简单的web服务器,使用80端口
2: Place files in webroot directory (webroot)
# webroot 插件时,Certbot 会通过在您 Web 服务器的根目录下放置验证文件来验证您对域名的控制权。这意味着您必须有一个正在运行的 Web 服务器,并且可以通过 HTTP 访问来验证文件。webroot 插件不会暂时占用端口,因为它直接利用现有的 Web 服务器进行验证。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): change_lee@outlook.com
# 填写你的邮箱,证书到期前会给你发邮件通知
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
# 这个就是服务说明,只能同意
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
# 这就是你要不要接入它们的广告和宣传
Account registered.
Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel): shuobude.ga www.shuobude.ga
Requesting a certificate for shuobude.ga and www.shuobude.ga
# 填写证书上显示的域名,多个域名使用 , / 空格分隔都可以,回车前一定要先把DNS的解析记录添加上,不然后就出现下面的报错 .
Certbot failed to authenticate some domains (authenticator: standalone). The Certificate Authority reported these problems:
Domain: shuobude.ga
Type: dns
Detail: DNS problem: NXDOMAIN looking up A for shuobude.ga - check that a DNS record exists for this domain; DNS problem: NXDOMAIN looking up AAAA for shuobude.ga - check that a DNS record exists for this domain
Domain: www.shuobude.ga
Type: dns
Detail: DNS problem: NXDOMAIN looking up A for www.shuobude.ga - check that a DNS record exists for this domain; DNS problem: NXDOMAIN looking up AAAA for www.shuobude.ga - check that a DNS record exists for this domain
Hint: The Certificate Authority failed to download the challenge files from the temporary standalone webserver started by Certbot on port 80. Ensure that the listed domains point to this machine and that it can accept inbound connections from the internet.
Some challenges have failed.
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.
测试环境,准备一个 nginx (让域名可以直接访问nginx),然后挂载nginx 的目录到docker 中
webroot模式
root@debian11:/var/www# docker run -it --rm --name certbot -v "/etc/letsencrypt:/etc/letsencrypt" -v "/var/lib/letsencrypt:/var/lib/letsencrypt" -v "/var/www/html:/var/www/html" certbot/certbot certonly
Saving debug log to /var/log/letsencrypt/letsencrypt.log
How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Runs an HTTP server locally which serves the necessary validation files under
the /.well-known/acme-challenge/ request path. Suitable if there is no HTTP
server already running. HTTP challenge only (wildcards not supported).
(standalone)
2: Saves the necessary validation files to a .well-known/acme-challenge/
directory within the nominated webroot path. A seperate HTTP server must be
running and serving files from the webroot path. HTTP challenge only (wildcards
not supported). (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 这里选择的是 2 ,使用 webroot 的方式
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel): test.frp.xuuuu.top
# 填写自己的域名,同样支持多个,使用 , / 空格分隔都可以
Requesting a certificate for test.frp.xuuuu.top
Input the webroot for test.frp.xuuuu.top: (Enter 'c' to cancel): /var/www/html/
# 指定 .well-known/acme-challenge/ 存放的目录,即网站的根目录
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/test.frp.xuuuu.top/fullchain.pem
Key is saved at: /etc/letsencrypt/live/test.frp.xuuuu.top/privkey.pem
This certificate expires on 2023-10-29. # 过期时间
These files will be updated when the certificate renews. # 选择你 renew 会更新这个文件
NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.
# - 证书到期前需要更新。 Certbot 可以在后台自动续订证书,但您可能需要采取措施才能启用该功能。 请参阅 https://certbot.org/renewal-setup 了解说明
# 简单来说就是设置一下定时任务,执行 certbot renew -q 。-q是安静模式,不输出内容
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
命令模式
# 获取证书并安装它
certbot run = certbot
# 仅证书
certbot certonly
# 示例,使用 -d 指定多个域名,也可以多次运行安装不同证书
certbot certonly -d example.com -d www.example.com
certbot certonly -d app.example.com -d api.example.com
Authenticators(身份验证器):
身份验证器是自动执行所需步骤的插件,以证明您控制了尝试为其请求证书的域名。始终需要身份验证器才能获取证书。
Installers(安装程序):
安装程序是插件,可以使用Certbot获得的证书自动修改Web服务器的配置,以通过HTTPS为您的网站提供服务。仅当您希望 Certbot 将证书安装到您的 Web 服务器时,才需要安装程序。
有些插件既是身份验证器又是安装程序,可以指定身份验证器和插件的不同组合,具体参考官方文档
# 使用参数指定验证器
--preferred-challenges
webroot
如果你是web服务器的管理员,并且您不希望在证书颁发过程中停止 Web 服务器,则可以使用 webroot 插件通过包含 certonly
和 --webroot
命令行来获取证书。此外,您需要指定 --webroot-path OR -w
包含 Web 服务器提供的文件的顶级目录(“Web 根目录”)。例如, --webroot-path /var/www/html
或者 --webroot-path /usr/share/nginx/html
是两个常见的 Web 根路径。
如果您要同时获得多个域的证书,则插件需要知道每个域的文件是从哪里提供的,这可能是每个域的单独目录。为多个域请求证书时,每个域将使用最近指定的 --webroot-path .因此,例如:
certbot certonly --webroot -w /var/www/example -d www.example.com -d example.com -w /var/www/other -d other.example.net -d another.other.example.net
www.example.com
,example.com
使用 /var/www/example
other.example.net
,another.other.example.net
使用 /var/www/other
Webroot 插件的工作原理是为您请求的每个域创建一个临时文件 ${webroot-path}/.well-known/acme-challenge 。然后,Let's Encrypt 验证服务器发出 HTTP 请求,以验证每个请求域的 DNS 是否解析为运行 certbot 的服务器。向 Web 服务器发出的示例请求如下所示:
66.133.109.36 - - [05/Jan/2016:20:11:24 -0500] "GET /.well-known/acme-challenge/HGr8U1IeTW4kY_Z6UIyaakzOkyQgPr_7ArlLgtZE8SX HTTP/1.1" 200 87 "-" "Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)"
有些特殊情况,查看官方文档,或者 google ,gpt 问一下。
Standalone
如果您不想使用(或当前没有)现有服务器软件,请使用独立模式获取证书。独立插件不依赖于在获取证书的计算机上运行的任何其他服务器软件。
要使用“独立”Web 服务器获取证书,您可以通过在命令行中包含和 --standalone
来 certonly
使用独立插件。此插件需要绑定到端口 80
才能执行域验证,因此您可能需要停止现有的 Web 服务器。
使用 Standalone 请关闭防火墙,或者放开 80 端口
默认情况下,Certbot 首先尝试使用 IPv6 绑定到所有接口的端口,然后使用 IPv4 绑定到该端口;只要至少有一个绑定成功,Certbot 就会继续。在大多数 Linux 系统上,IPv4 流量将路由到绑定的 IPv6 端口,预计第二次绑定期间会失败。
用 --<challenge-type>-address
显式告诉 Certbot 要绑定哪个接口(和协议)。
certbot-dns-cloudflare Certbot-DNS-Cloudflare 插件
https://certbot-dns-cloudflare.readthedocs.io/
手动模式✨
如果您想获取在目标网络服务器以外的计算机上运行的 certbot 证书或自己执行域验证步骤,则可以使用手动插件。虽然对 UI 隐藏,但您可以使用插件通过在命令行上指定 certonly 来 --manual
获取证书。这要求您将命令复制并粘贴到另一个终端会话中,该会话可能位于另一台计算机上。
手动插件可以使用 http
或 dns
校验。您可以使用该 --preferred-challenges
选项来选择您喜欢的校验方式。
- http: http 校验要求您将具有特定名称和特定内容的文件直接放置在 /.well-known/acme-challenge/ 包含您的 Web 服务器提供的文件的顶级目录(“Web 根目录”)的目录中。从本质上讲,它与webroot插件相同,但不是自动化的。
- dns: 使用 dns校验时, certbot 将要求您在域名下放置包含特定内容的 TXT DNS 记录,该域名由要为其颁发证书的主机名组成,前面加
_acme-challenge
_acme-challenge.example.com. 300 IN TXT "gfj9Xq...Rg85nM"
使用手动插件续订
使用 创建的 --manual 证书不支持自动续订,除非与身份验证挂钩脚本 via --manual-auth-hook 结合使用以自动设置所需的 HTTP 和/或 TXT 校验。
Let's Encrypt 的运作方式
简单总结:
- 你要有自己的域名,并且可以管理自己的域名
a. Let's Encrypt会验证你的域名的权限,会要求创建一个 txt 类型的dns记录 - 你要自己的站点,可以管理站点的内容
a. Let's Encrypt会要求你在固定的URL下放入一个静态资源来做验证
一般是1/2 其中一种方式。证书颁发和吊销一般我们都用不到,证书的有效期只有 90天,所以基本可以不用管,如果想长期使用就到期违续约就可以了