使用Let’s Encrypt获取和安装SSL/TLS证书的完整指南

Let’s Encrypt是一个免费、自动化的证书颁发机构(CA),通过其官方工具Certbot可以简化SSL/TLS证书的获取和安装流程。以下是详细的步骤指南:

一、准备工作

  1. 域名准备:确保您拥有要申请证书的域名,并且该域名的DNS记录已正确解析到您的服务器IP地址
  2. 服务器要求:需要root或sudo权限,并确保服务器开放了80(HTTP)和443(HTTPS)端口
  3. Web服务器:已安装Nginx或Apache等Web服务器软件

二、安装Certbot工具

Certbot是Let’s Encrypt官方推荐的客户端工具,安装方法因操作系统而异:

Ubuntu/Debian系统

1
2
3
4
sudo apt update
sudo apt install certbot python3-certbot-nginx # 用于Nginx
# 或
sudo apt install certbot python3-certbot-apache # 用于Apache

CentOS/RHEL系统

1
2
3
4
5
6
7
8
sudo yum install epel-release
sudo yum install certbot python3-certbot-nginx # 用于Nginx
# 或
sudo yum install certbot python3-certbot-apache # 用于Apache


## The requested nginx plugin does not appear to be installed 未安装插件
sudo yum install certbot-nginx

通过Snap安装(推荐获取最新版本)

1
2
3
4
sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

安装完成后验证版本:certbot --version

三、获取Let’s Encrypt证书

1. 基本证书申请命令

对于Nginx服务器:

1
sudo certbot --nginx -d example.com -d www.example.com

对于Apache服务器:

1
sudo certbot --apache -d example.com -d www.example.com

执行命令后会要求:

  1. 输入有效的电子邮件地址(用于证书到期提醒)
  2. 同意服务条款
  3. 选择是否将HTTP流量重定向到HTTPS(推荐选择"2"强制重定向)

2. 域名验证方式

Let’s Encrypt支持两种主要验证方式:

验证类型 说明 适用场景
HTTP验证 在网站根目录创建验证文件 标准Web服务器环境
DNS验证 添加TXT记录到域名DNS 无法开放80端口的服务器或泛域名证书

DNS验证示例(适用于泛域名证书):

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

执行后会提示添加特定的TXT记录到DNS解析

四、安装和配置证书

Nginx服务器配置

Certbot通常会自动修改Nginx配置,生成的证书文件位于:

1
2
3
4
5
/etc/letsencrypt/live/example.com/
├── cert.pem # 证书文件
├── chain.pem # 中间证书
├── fullchain.pem # 完整证书链
└── privkey.pem # 私钥文件

典型Nginx SSL配置示例:

1
2
3
4
5
6
7
8
9
server {
listen 443 ssl;
server_name example.com www.example.com;

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

# 其他配置...
}

配置完成后测试并重载Nginx:

1
sudo nginx -t && sudo systemctl reload nginx

Apache服务器配置

Apache的配置类似,Certbot会自动修改配置文件,证书路径与Nginx相同。典型配置包括:

1
2
3
4
5
6
7
8
<VirtualHost *:443>
ServerName example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
# 其他配置...
</VirtualHost>

配置完成后重启Apache:

1
sudo systemctl restart apache2

五、证书自动续期

Let’s Encrypt证书有效期为90天,设置自动续期非常重要:

  1. 测试续期命令是否正常工作:
1
sudo certbot renew --dry-run
  1. 添加定时任务(通常Certbot已自动配置):
1
sudo crontab -e

添加以下内容(每天凌晨2点检查续期):

1
0 2 * * * /usr/bin/certbot renew --quiet
  1. 对于Nginx/Apache,续期后可能需要重载服务:
1
2
3
0 2 * * * /usr/bin/certbot renew --quiet && systemctl reload nginx
# 或
0 2 * * * /usr/bin/certbot renew --quiet && systemctl reload apache2

六、常见问题解决

  1. 证书申请失败

    • 检查域名解析是否正确
    • 确保80/443端口开放
    • 验证服务器时间是否正确
  2. 续期失败

    • 检查Certbot日志:/var/log/letsencrypt/
    • 确保定时任务配置正确
    • 手动运行certbot renew查看具体错误
  3. 混合内容警告

    • 确保网页中所有资源(图片、CSS、JS)都使用HTTPS链接
    • 考虑添加Content Security Policy头
  4. 服务器配置错误

    • Nginx: nginx -t测试配置
    • Apache: apachectl configtest测试配置
    • 检查错误日志获取详细信息

七、高级用法

  1. 泛域名证书

    1
    certbot certonly --manual --preferred-challenges dns -d *.example.com

    需要手动添加DNS TXT记录验证

  2. IP地址证书(2025年7月新增功能):

    1
    certbot certonly --standalone -d 192.0.2.1

    需要验证IP地址所有权

  3. 多域名证书

    1
    certbot --nginx -d example.com -d api.example.com -d app.example.com
  4. 证书吊销

    1
    certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem

通过以上步骤,您可以轻松地为网站部署Let’s Encrypt免费SSL/TLS证书,实现安全的HTTPS连接。Certbot工具大大简化了证书的申请、安装和续期流程,使加密通信的部署变得更加便捷。

引用链接:
1.用Let‘s Encrypt给网站添加一个免费的SSL证书 - 24K纯学渣
2.使用Let’s Encrypt 获取免费SSL证书 - 博客园
3.自动化获取Let‘s Encrypt SSL证书的Shell脚本指南 - CSDN博客
4.Let’s Encrypt 免费SSL证书申请 - CSDN博客
5.免费申请 Let‘s Encrypt SSL 证书 - CSDN博客
6.申请Let’s Encrypt免费SSL证书 - 博客园
7.[教程] 如何为 Nginx/Apache 配置免费 Let‘s Encrypt SSL 证书 (Certbot 详解) - CSDN博客
8.使用acme.sh颁发TLS证书并安装到nginx/apache实现网站https访问 - CSDN博客
9.在Apache 上部署 Let’s Encrypt 证书与自动续期脚本 - 腾讯云
10.【SSL部署与优化​】​​如何为网站启用HTTPS:从Let‘s Encrypt免费证书到Nginx配置​​ - CSDN博客
11.定期更新Let‘s Encrypt SSL证书遇到的问题 - 24K纯学渣
12.[笔记] CentOS7 + Nginx 环境下,安装使用 Let‘s Encrypt 免费 SSL 证书 (自动续签) - 掘金开发者社区
13.使用Certbot 获取免费 HTTPS 证书:从零开始的 HTTPS 配置指南 - CSDN博客
14.HTTPS 证书自动化运维:使用Certbot来申请https证书实践指南 - 博客园
15.使用Certbot 申请和自动续签 Let’s Encrypt 的免费 SSL 证书 - CSDN博客
16.创建Let‘s Encrypt 证书 - CSDN博客
17.使用Let’s Encrypt和Certbot快速为网站启用HTTPS - 沿途赋能站
18.在Ubuntu上使用Certbot申请Let’s Encrypt SSL证书 - 随心不欲