使用 Certbot 为 Nginx 自动申请 HTTPS 证书并配置自动续签

1. 适用场景

本文适用于以下部署环境:

复制代码
Linux 服务器
Nginx
Django / Java / Node.js 等后端服务
域名已解析到服务器

示例域名统一使用:

复制代码
domain.com
www.domain.com

如果需要通配符域名部署证书,如*.domain.com,建议使用acme.sh,详细步骤参考acme自动续签


2. 申请证书前的检查

2.1 确认域名解析正确

复制代码
dig domain.com +short
dig www.domain.com +short
curl -4 ifconfig.me

如果三个结果都是同一个服务器公网 IP,说明域名解析正常。


2.2 确认 Nginx 配置正常

复制代码
nginx -t

如果 Nginx 配置本身有错误,Certbot 会失败。


2.3 确认 80 和 443 端口已放行

本机防火墙放行:

复制代码
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

如果是云服务器,还需要在云平台安全组中放行:

复制代码
TCP 80
TCP 443

3. 安装 Certbot

CentOS / RHEL 系统:

复制代码
dnf install -y epel-release
dnf install -y certbot python3-certbot-nginx

查看版本:

复制代码
certbot --version

4. 自动申请证书并配置 Nginx

执行:

复制代码
sudo certbot --nginx -d domain.com -d www.domain.com

过程中会要求输入邮箱、同意协议:

复制代码
Enter email address: 输入邮箱
Do you agree? Y
Share email with EFF? N

成功后,Certbot 会自动在 Nginx 配置中加入 HTTPS 证书配置,例如:

复制代码
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;

并自动配置 HTTP 跳转 HTTPS。

5. Nginx 配置示例

以 Django + uWSGI 为例:

复制代码
server {
    listen 443 ssl;
    server_name domain.com www.domain.com;

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location /static/ {
        alias /home/project/static/;
    }

    location /media/ {
        alias /home/project/media/;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/project.sock;
    }
}

server {
    listen 80;
    server_name domain.com www.domain.com;
    return 301 https://$host$request_uri;
}

修改后检查并重载 Nginx:

复制代码
nginx -t
systemctl reload nginx

6. 查看证书状态

复制代码
certbot certificates

可以看到证书域名、到期时间、证书路径等信息。


7. 测试 HTTPS 是否生效

复制代码
curl -I http://domain.com
curl -I https://domain.com
curl -I https://www.domain.com

正常情况下:

复制代码
http://domain.com 会跳转到 https://domain.com
https://domain.com 可以正常访问

8. 自动续签

Certbot 默认会配置自动续签任务。可以用下面命令测试:

复制代码
sudo certbot renew --dry-run

如果显示成功,说明自动续签流程正常。

查看系统定时任务:

复制代码
systemctl list-timers | grep certbot

如果希望证书续签后自动重新加载 Nginx,可以添加 hook:

复制代码
mkdir -p /etc/letsencrypt/renewal-hooks/deploy
vim /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

写入:

复制代码
#!/bin/bash
systemctl reload nginx

赋予执行权限:

复制代码
chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

9. 常见问题

9.1 Nginx 报证书文件不存在

错误类似:

复制代码
No such file or directory: /etc/letsencrypt/live/domain.com/fullchain.pem

原因是 Nginx 配置里提前写了证书路径,但证书还没有申请成功。

处理方法:先注释掉 HTTPS 证书配置,只保留 80 端口配置,等证书申请成功后再加回去。


9.2 Certbot 验证超时

错误类似:

复制代码
Timeout during connect

一般是以下原因:

复制代码
域名没有解析到当前服务器
80 端口没有放行
Nginx 没有监听 80 端口
云服务器安全组没有放行 80

排查:

复制代码
dig domain.com +short
curl -4 ifconfig.me
ss -lntp | grep ':80'
nginx -t

9.3 Certbot Python 依赖错误

如果出现类似:

复制代码
AttributeError: module 'lib' has no attribute 'X509_get_notAfter'

通常是系统 Certbot 和 pip 安装的 Python 依赖冲突。

可以检查:

复制代码
rpm -qa | grep -E "certbot|pyOpenSSL|cryptography"
python3 -m pip show pyOpenSSL cryptography certbot

如果发现 pyOpenSSLcryptography 来自 /usr/local/,建议卸载 pip 全局包:

复制代码
python3 -m pip uninstall -y pyOpenSSL cryptography

然后重新安装系统包:

复制代码
dnf reinstall -y python3-pyOpenSSL python3-cryptography certbot python3-certbot python3-certbot-nginx

10. 总结

常用流程如下:

复制代码
dig domain.com +short
dig www.domain.com +short
nginx -t
sudo certbot --nginx -d domain.com -d www.domain.com
certbot certificates
sudo certbot renew --dry-run
systemctl reload nginx

简单来说:

复制代码
先确认域名解析和 80 端口正常
再用 Certbot 申请证书
申请成功后检查 HTTPS
最后测试自动续签

对于普通网站,直接使用:

复制代码
sudo certbot --nginx -d domain.com -d www.domain.com

即可完成证书申请、Nginx 配置和后续自动续签。

相关推荐
Qimooidea1 小时前
祁木 CAD Translator 全套功能 & 官网入口汇总
运维·服务器
梦想的颜色1 小时前
Docker 容器化本地部署 Claude Code 完整硬核教程|隔离沙盒 + 国产模型直连 + 持久化 Skill
运维·容器·ai 工程化·沙盒隔离·docker 本地部署·claude code硬核实战·国内国产模型接入
小趴菜え2 小时前
升级Ubuntu20.04版本至Ubuntu22.04
linux·运维
渣渣盟2 小时前
Docker 运维常用命令手册(含扩容与实战)
运维·docker·容器
oscar9992 小时前
3.4 Nginx 负载均衡——动态再平衡的反人性纪律
nginx·github·负载均衡·财富源代码
小羊Yveesss2 小时前
2026年外贸建站服务器怎么选?访问速度、稳定性和海外收录怎么判断
大数据·运维·服务器
Darkwanderor3 小时前
Linux进程优先级操作
linux·运维·c语言·c++
小五传输3 小时前
内外网文件交换系统产品推荐 解决隔离网络文件交换5类难题
大数据·运维·安全
AOwhisky3 小时前
Linux(CentOS)系统管理入门笔记(第一期)——从 Multics 到主流发行版
linux·运维·笔记·centos·云计算
从零开始的代码生活_3 小时前
Linux epoll 多路转接详解
linux·运维·网络·后端·tcp/ip·计算机网络·php