LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

手把手教你申请 Let''s Encrypt 免费 SSL 证书:从手动验证开始!

admin
2025年8月14日 21:6 本文热度 45

前言

在如今的互联网环境中,为网站配置 SSL 证书已成为安全的标配。本文使用 Let’s Encrypt 免费获取泛域名 SSL 证书,采用 certbot + DNS-01 的手动方式获取通配符证书,后续将推出自动续期篇,欢迎关注!

📌 什么是 Let’s Encrypt?

Let’s Encrypt 是一个免费的、自动化的、开放的证书颁发机构(CA),提供免费 SSL 证书。它的目标是让所有网站都能轻松实现 HTTPS 加密,保障用户数据的安全。通过 Let’s Encrypt,你可以为你的主域名、子域名,甚至泛域名配置安全证书,而且过程完全自动化,不需要手动申请和管理证书,官方推荐使用 Certbot

📌 什么是 Certbot?

Certbot 是一款免费的开源软件工具,用于自动使用 Let’s Encrypt 证书在手动管理的网站上启用 HTTPS

Certbot 是由电子前沿基金会 (EFF) 开发提供的免费工具,配合 Let's Encrypt,可以轻松为你的域名申请免费 SSL 证书。

✨准备工作

✅ 一台可以访问互联网的 Linux 服务器(CentOS / Ubuntu)
✅ 已解析并可控制 DNS 的域名(例如:example.com) ✅ 安装好 certbot

安装 Certbot 示例(CentOS):

yum install epel-release -y
yum install certbot -y

申请普通证书

如果只需要为特定域名申请证书(以 yourdomain.com 和 www.yourdomain.com为例):

certbot certonly --manual --preferred-challenges=dns-01 \
  --server https://acme-v02.api.letsencrypt.org/directory \
  --agree-tos -m your-email@example.com \
  -d yourdomain.com -d www.yourdomain.com

申请通配符证书(推荐)

如果希望一个证书覆盖所有子域名,可以申请通配符证书, 使用下面这条命令申请通配符证书(以 *.example.comexample.com 为例):

certbot certonly --manual --preferred-challenges=dns-01 \
  --server https://acme-v02.api.letsencrypt.org/directory \
  --agree-tos -m your-email@example.com \
  -d '*.example.com' -d example.com

📌 🎯命令详解:

  • --manual:使用手动交互模式获取证书,不依赖 Web 服务或 DNS 插件
  • --preferred-challenges=dns-01:指定dns方式验证,使用 DNS TXT 记录进行验证
  • --server https://acme-v02.api.letsencrypt.org/directory:指定验证的服务端API Endpoint(对于当前版本的certbot这个url已经是默认值)。
  • -d:填写你要申请的域名(支持多个)
  • 测试模式:可以添加--dry-run参数测试申请过程

输入Yes后,即可开始DNS-01的验证过程了。

🛠️ 手动到域名服务商添加 DNS TXT 验证记录

certbot交互程序会指示我们所需添加的DNS TXT记录,其中包括记录名(_acme-challenge) 和记录值(一个随机生成的字符串):

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:

qr4m0k3wYuGc5f8yYJhqanY-fUhlw546OSuxBEZYF70

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
主机记录:_acme-challenge.example.com
记录值:qr4m0k3wYuGc5f8yYJhqanY-fUhlw546OSuxBEZYF70

添加到 DNS 服务商控制台(如阿里云、腾讯云、Cloudflare)中:

主机记录
类型
_acme-challenge.example.com
TXT
qr4m0k3wYuGc5f8yYJhqanY-fUhlw546OSuxBEZYF70

开一个新窗口使用 dig 命令检查是否生效(生效后第一个窗口再回车):

dig _acme-challenge.example.com TXT

✅ 完成申请

当使用 Certbot 成功申请到 SSL 证书后,证书文件将默认保存在以下目录中:

/etc/letsencrypt/live/example.com/

/etc/letsencrypt/live/example.com/cert.pem
/etc/letsencrypt/live/example.com/chain.pem
/etc/letsencrypt/live/example.com/fullchain.pem
/etc/letsencrypt/live/example.com/privkey.pem
/etc/letsencrypt/live/example.com/README

具体包含以下几个文件:

  • cert.pem:这是你的 SSL 证书 文件。
  • privkey.pem:这是你的 私钥 文件,务必妥善保管,切勿泄露。
  • chain.pem:这是 中间证书链 文件,用于验证证书的完整性。
  • fullchain.pem:这是 完整证书链 文件,通常用于 Nginx 或 Apache 的 SSL 配置中。

🔍 如何配置证书?

以 Nginx 为例:

提示:为了避免证书路径错误,建议在 Nginx 或 Apache 配置中直接使用 fullchain.pem 和 privkey.pem 两个文件。

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

保存并测试重载 Nginx 配置:

nginx -t
nginx -s reload

使用 certbot 查看证书信息

🔎 查看所有已签发证书信息

certbot certificates

输出示例:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Cannot extract OCSP URI from /etc/letsencrypt/live/example.com/cert.pem

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: example.com
    Serial Number: 5a4c055a0176abaf3579e87f81e1270826d
    Key Type: RSA
    Domains: *.example.com example.com
    Expiry Date: 2025-10-16 06:09:46+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

🔎 查看指定证书的详细信息(以 example.com 为例)

openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -text -noout

🔎 查看证书包含哪些域名(SAN)

openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -text -noout | grep -A 1 "Subject Alternative Name"

输出示例:

X509v3 Subject Alternative Name: 
    DNS:*.example.com, DNS:example.com

🔎 查看证书有效期

  • ✅ 只看过期时间(到期日期)
openssl x509 -enddate -noout -in /etc/letsencrypt/live/example.com/cert.pem

输出示例:

notAfter=Oct 16 06:09:46 2025 GMT
  • ✅ 同时查看起始和过期时间
openssl x509 -startdate -enddate -noout -in /etc/letsencrypt/live/example.com/cert.pem

输出示例:

notBefore=Jul 18 06:09:47 2025 GMT
notAfter=Oct 16 06:09:46 2025 GMT

🔑 查看公钥摘要(用于比对)

openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -modulus | openssl md5

🖊️ 查看签发机构(CA信息)

openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -issuer

🔐 查看使用者(证书属于谁)

openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -subject

🧰 查看私钥是否匹配证书

判断 privkey.pem 是否与 cert.pem 一致(MD5摘要比对):

openssl rsa -noout -modulus -in /etc/letsencrypt/live/example.com/privkey.pem | openssl md5

openssl x509 -noout -modulus -in /etc/letsencrypt/live/example.com/cert.pem | openssl md5

✅ 两个输出一致则说明匹配。

🖥️ 通过浏览器或远程查看服务器证书

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates -subject -issuer -text

常见问题

📆 证书有效期多久?

默认有效期是 90 天,需要定期续期。

DNS记录多久生效?

通常几分钟到几小时不等,具体取决于DNS服务商。

可以为多个域名申请一个证书吗?

可以,使用多个-d参数即可。


🧭 总结

        通过本文你已经学会了如何使用 Certbot 工具手动申请免费 SSL 证书 ✅,申请SSL证书是一个相对简单的过程,特别是使用DNS验证方式。虽然需要手动添加DNS记录,但这种方式灵活性更高,适合各种部署场景。


阅读原文:原文链接


该文章在 2025/8/15 11:37:53 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved