一、Let's Encrypt 简介
Let's Encrypt 是一个由非营利性组织 互联网安全研究小组(ISRG)提供的免费、自动化和开放的证书颁发机构(CA)。
简单的说,借助 Let's Encrypt 颁发的证书可以为我们的网站免费启用 HTTPS(SSL/TLS) 。
Let's Encrypt免费证书的签发/续签都是脚本自动化的,官方提供了几种证书的申请方式方法,打开链接 https://letsencrypt.org/zh-cn/getting-started/ 快速浏览。
官方推荐使用 Certbot 客户端来签发证书,这种方式可参考文档自行尝试,不做评价。
也可以使用第三方客户端 acme.sh 申请,据了解这种方式可能是目前 Let's Encrypt 免费证书客户端最简单、最智能的 shell 脚本,可以自动发布和续订 Let's Encrypt 中的免费证书。
二、通配符证书
什么是通配符证书
域名通配符证书类似 DNS 解析的泛域名概念,通配符证书就是证书中可以包含一个通配符。主域名签发的通配符证书可以在所有子域名中使用,比如 .example.com
、bbs.example.com
。
申请通配符证书
Let’s Encrypt 上的证书申请是通过 ACME 协议来完成的。ACME 协议规范化了证书申请、更新、撤销等流程,实现了 Let’s Encrypt CA 自动化操作。解决了传统的 CA 机构是人工手动处理证书申请、证书更新、证书撤销的效率和成本问题。
ACME v2 是 ACME 协议的更新版本,通配符证书只能通过 ACME v2 获得。要使用 ACME v2 协议申请通配符证书,只需一个支持该协议的客户端就可以了,官方推荐的客户端是 Certbot。
三、什么是 Certbot?
Certbot 是一个开源的自动化工具,用于获取和续订由 Let's Encrypt 提供的免费 SSL/TLS 证书。
Let's Encrypt 是一个由互联网安全研究小组(ISRG)运营的证书颁发机构(CA),它提供了一个自动化的流程来生成和更新证书,使得网站管理员可以轻松地为他们的站点启用 HTTPS 加密。
Certbot 的主要特点包括:
自动化:它可以自动验证域名所有权,并申请证书。
免费:它使用的是 Let's Encrypt 提供的免费证书。
兼容性:支持多种 web 服务器,如 Apache、Nginx 等。
易用性:提供了命令行界面,使得安装和使用变得简单。
续订:自动处理证书的续订,确保网站的 HTTPS 连接始终保持有效。
Let's Encrypt 颁发的证书有效期为 90 天,Certbot 会自动配置证书的续期任务,确保证书不会过期。
四、安装 Certbot?
1. 在 Ubuntu/Debian 系统上安装 Certbot
使用 APT 安装:
sudo apt update
sudo apt install certbot
安装完成后,Certbot 就可以用了。
安装 Snap 版本:
Snap 是 Certbot 官方推荐的安装方式,尤其是针对长期支持的 Ubuntu 版本。
sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot # 这一步是为了确保 certbot 命令能全局使用
2. 在 CentOS/RHEL 系统上安装 Certbot
安装 EPEL 仓库(适用于 CentOS 7 及以下):
sudo yum install epel-release
sudo yum install certbot
3. 在 macOS 上安装 Certbot
macOS 上可以使用 Homebrew 安装 Certbot:
brew install certbot
更多内容我们可以在 Certbot 网站 https://certbot.eff.org/ 查看各个系统平台的安装方法:
安装完成后,使用以下命令查看 certbot 安装的版本:
certbot --version
四、证书申请和续签
安装好certbot 后就可以使用以下命令来申请证书了,
客户在申请 Let’s Encrypt 证书的时候,需要校验域名的所有权,证明操作者有权利为该域名申请证书,目前支持三种验证方式:
dns-01:给域名添加一个 DNS TXT 记录。
http-01:在域名对应的 Web 服务器下放置一个 HTTP well-known URL 资源文件。
tls-sni-01:在域名对应的 Web 服务器下放置一个 HTTPS well-known URL 资源文
注意 *.example.com 为你自己的域名,需要修改:
certbot certonly -d *.example.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
参数 | 说明 |
---|
certonly | 表示安装模式,Certbot 有安装模式和验证模式两种类型的插件。 |
--manual | 表示手动安装插件,Certbot 有很多插件,不同的插件都可以申请证书,用户可以根据需要自行选择 |
-d | 为那些主机申请证书,如果是通配符,输入 *.you.cn(可以替换为你自己的一级域名) |
--preferred-challenges dns | 使用 DNS 方式校验域名所有权 |
--server | Let's Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定。 |
执行以上命令后,填写信息:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter'c' to cancel): xxx@qq.com.# 这里输入你的邮箱
----------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. You must agree in
order to registerwith the ACME server.Do you agree?
----------------------------------------
(Y)es/(N)o: Y # 输入 Y
----------------------------------------
Would you be willing, once your first certificate is successfully issued, to
share your email address with the ElectronicFrontierFoundation, 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 # 输入 Y
Account registered.
Requesting a certificate for*.example.com
----------------------------------------
Please deploy a DNS TXT record under the name:
xxxxxxx.example.com.# 这里需要设置域名解析,需要到域名后台填写信息,参考下图
with the following value:
aIwqY00CZtziVwr-xxxxxxxxxxxxxx # 这里是域名解析的内容,参考下图
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
AdminToolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.example.com.
Lookfor one or more bolded line(s) below the line ';ANSWER'.It should show the
value(s) you've just added.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Press Enter to Continue # 参考下图设置完域名解析后,并且确认txt记录解析正常后,按回车就可以生成了,记住一定要先解析设置完成后再回车,然后生成的证书信息如下:
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem
This certificate expires on 2024-12-21.
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
辅助图:
设置域名解析用于验证证书:
添加完成后,先输入以下命令确认 TXT 记录是否生效:
dig -t txt _acme-challenge.example.com @8.8.8.8 ... ;;OPT PSEUDOSECTION: ...
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;_acme-challenge.xxx.com. IN TXT
;; ANSWER SECTION:
_acme-challenge.example.com. 599 IN TXT "xxUHdwoZ6IaU_ab87h67rvbU2yJgdRyRe9zEA3jw"
...
确认生效后,回车继续执行.
到了这一步后,恭喜您,证书申请成功。证书和密钥保存在下列目录:
$ tree /etc/letsencrypt/live/example.com . |- cert.pem |- chain.pem |- fullchain.pem |- privkey.pem
使用以下命令查看证书内容:
openssl x509 -in cert.pem -noout -text
Let's Encrypt 颁发的证书有效期为 90 天,可以使用以下命令进行续签证书:
certbot certonly --force-renewal --manual -d '*.example.com' \
--preferred-challenges dns \
--server https://acme-v02.api.letsencrypt.org/directory
执行以上续签命令后,就会让我们更新下 DNS 解析记录:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for*.example.com
----------------------------------------
Please deploy a DNS TXT record under the name:
_acme-challenge.example.com.
with the following value:
ckxo1wGXbP1CtNQ3ZRfvHxxxxxx # 这里会显示你要更改的 DNS 解析记录值,设置好就可以完成更新了
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
AdminToolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.jysahre.com.
Lookfor one or more bolded line(s) below the line ';ANSWER'.It should show the
value(s) you've just added.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
五、nginx配置ssl证书
server {
server_name xxx.com;
listen 443 http2 ssl;
ssl on;
ssl_certificate /etc/cert/xxx.com/fullchain.pem;
ssl_certificate_key /etc/cert/xxx.com/privkey.pem;
ssl_trusted_certificate /etc/cert/xxx.com/chain.pem;
location / {
proxy_pass http://127.0.0.1:6666;
}
}
定时更新证书:
crontab -e
0 */12 * * * certbot renew --quiet --renew-hook "/etc/init.d/nginx reload"
六、取消证书
certbot revoke
certbot delete
阅读原文:原文链接
该文章在 2025/8/15 11:41:29 编辑过