首页 > 服务器管理 > Let's Encrypt SSL 证书申请完整指南
2025
12-19

Let's Encrypt SSL 证书申请完整指南

一、什么是 Let's Encrypt?

Let's Encrypt 是一个免费、自动化、开放的证书颁发机构(CA),提供免费SSL/TLS 证书,可用于网站 HTTPS 加密。

其特点包括: - 免费 - 自动化申请与续期 - 全球信任 -开源公益,以前SSL证书很贵,现在有了Let's Encrypt,完全免费,自动续期,没有理由不用。

5e90104eda488d94782a631b074a1996.png

二、Certbot安装与使用

1、准备和安装Certbot

(1)准备工作:

在 Linux(Ubuntu/Debian/CentOS)服务器上,你需要:

- 已解析到服务器的域名 

- 已安装 Nginx 或 Apache 

- 服务器能访问互联网

这样可以保障Let's Encrypt对你的域名权限校验

(2)安装Certbot

# Ubuntu/Debian
apt update
apt install certbot

# CentOS/RHEL
yum install epel-release
yum install certbot

2、获取证书(HTTP验证)

(1)独立模式(没有Web服务器时)

# 需要80端口空闲
certbot certonly --standalone -d example.com -d www.example.com

(2)Webroot模式(已有Web服务器)

# 指定网站根目录
certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com

(3)Nginx插件模式(自动配置)

# 安装插件
apt install python3-certbot-nginx

# 自动获取证书并配置Nginx
certbot --nginx -d example.com -d www.example.com

3、获取证书(DNS验证)

# 泛域名证书
certbot certonly --manual --preferred-challenges dns -d "*.example.com" -d example.com

执行后会提示添加DNS TXT记录:

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

添加记录后等待DNS生效,然后继续。

4、证书文件说明

证书存放在 /etc/letsencrypt/live/example.com/

文件说明用途
cert.pem域名证书-
chain.pem中间证书链-
fullchain.pem完整证书链Nginx ssl_certificate
privkey.pem私钥Nginx ssl_certificate_key

三、配置Web服务器使用SSL(Nginx)

基础配置

server {
    listen 443 ssl http2;
    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;
    
    # 其他配置...
}

# HTTP重定向到HTTPS
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

安全加固

# /etc/nginx/conf.d/ssl.conf

# SSL会话缓存
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;

# 现代加密套件
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;

# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;

# DH参数(可选,增强安全性)
# openssl dhparam -out /etc/nginx/dhparam.pem 2048
# ssl_dhparam /etc/nginx/dhparam.pem;

测试配置

# 测试Nginx配置
nginx -t

# 重载配置
nginx -s reload

# 测试SSL
curl -I https://example.com

四、自动续期配置

Let’s Encrypt证书有效期是90天,建议提前30天续期。

1、手动续期

# 续期所有证书
certbot renew

# 测试续期(不真正执行)
certbot renew --dry-run

2、自动续期

(1)Cron定时任务

# crontab -e
0 3 * * * certbot renew --quiet --post-hook "nginx -s reload"

(2)Systemd Timer(推荐)

Certbot安装后通常自带timer

# 查看timer状态
systemctl status certbot.timer

# 启用timer
systemctl enable certbot.timer
systemctl start certbot.timer

# 查看下次执行时间
systemctl list-timers | grep certbot

如未启用则先设置启用

# 启用certbot定时器
systemctl unmask certbot.timer
systemctl enable --now certbot.timer

# 测试自动续期
certbot renew --dry-run

3、续期钩子

# 续期成功后执行的命令
certbot renew --post-hook "systemctl reload nginx"

# 或在配置文件中设置
# /etc/letsencrypt/renewal/example.com.conf
[renewalparams]
post_hook = systemctl reload nginx

五、内网服务HTTPS

内网服务器没有公网IP和域名,如何配置HTTPS?

方案对比

方案优点缺点
自签名证书简单浏览器警告
内网CA企业级方案配置复杂
反向代理正规证书需要公网入口
组网+DNS验证正规证书需要域名

1、自签名证书(临时方案)

# 生成私钥
openssl genrsa -out server.key 2048

# 生成证书
openssl req -new -x509 -key server.key -out server.crt -days 365 \
  -subj "/CN=myserver.local"

# Nginx配置使用
ssl_certificate /path/to/server.crt;
ssl_certificate_key /path/to/server.key;

2、组网访问方案

如果使用组网软件(如星空组网)将内网服务器和客户端连接起来:

-内网服务器通过组网获得固定的虚拟IP

-用DNS验证方式获取Let’s Encrypt证书(需要有域名)

-将域名解析到组网的虚拟IP

-客户端通过域名+HTTPS访问

这样既有正规证书,又不需要公网暴露服务器。

六、总结

Let’s Encrypt + HTTPS配置要点:

1、选择验证方式:有80端口用HTTP验证,否则用DNS验证

2、选择客户端:Certbot最通用,acme.sh最轻量

3、配置自动续期:证书90天过期,必须自动续期

3、安全加固:使用TLS1.2+,现代加密套件,开启HSTS

4、内网服务:可以用组网+DNS验证获取正规证书

参考资料

Let’s Encrypt官方文档:https://letsencrypt.org/docs/

Certbot文档:https://certbot.eff.org/docs/

Mozilla SSL Configuration Generator:https://ssl-config.mozilla.org/


作者:admin
admin
TTF的家园-www.ttfde.top 个人博客以便写写东西,欢迎喜欢互联网的朋友一起交流!

本文》有 0 条评论

留下一个回复