HTTPS 证书自动化部署与运维手册

HTTPS 证书自动化部署与运维手册 (多系统版)

文档版本: v2.0

适用场景: Linux 服务器 (CentOS/Ubuntu/Debian) + Nginx

核心工具: Let's Encrypt (Certbot)


1. 不同系统的安装方式

虽然 Certbot 的使用命令是通用的,但在不同 Linux 发行版上的安装命令有所区别。

A. Alibaba Cloud Linux 3 / CentOS 8 / RHEL 8

使用 dnf 包管理器(不需要额外配置源)。

Bash

sudo dnf install certbot python3-certbot-nginx -y

B. CentOS 7

CentOS 7 比较老旧,需要先启用 EPEL 源,且使用 yum

Bash

# 1. 安装 EPEL 源
sudo yum install epel-release -y

# 2. 安装 Certbot 和 Nginx 插件
sudo yum install certbot python3-certbot-nginx -y

C. Ubuntu / Debian

使用 apt 包管理器。

Bash

sudo apt update
sudo apt install certbot python3-certbot-nginx -y

注意:Certbot 官方也推荐使用 snap 安装(跨平台通用),但上述原生命令更适合轻量级服务器,无需安装 Snap 守护进程。


2. 两种配置模式详解

在申请证书时,Certbot 提供了两种核心模式。理解它们的区别非常重要。

模式一:Cert Only (只申请,手动改配置) —— 推荐生产环境使用

这是我们在之前对话中使用的模式。

  • 命令certbot certonly --nginx
  • 行为
    1. 验证域名所有权。
    2. 下载证书文件到 /etc/letsencrypt/live/
    3. 停止操作。它不会触碰你的 Nginx 配置文件。
  • 优点:非常安全,不会破坏你现有的复杂的 proxy_pass 或逻辑。
  • 缺点:你需要自己去 Nginx 配置文件里填 ssl_certificate 的路径。

模式二:Auto Install (申请并自动修改配置)

这是 Certbot 的“全自动”功能,适合新手或全新空白的服务器。

  • 命令certbot --nginx (注意:去掉了 certonly)
  • 行为
    1. 申请证书。
    2. 自动读取你的 Nginx 配置文件。
    3. 自动修改你的配置文件,插入 SSL 相关代码。
    4. 询问是否开启 HTTP -> HTTPS 强制跳转,并自动写入重定向代码。

自动修改流程演示 (Auto Install)

如果执行 certbot --nginx,Certbot 会对你的配置文件做如下修改:

修改前 (原始 Nginx 配置):

Nginx

server {
    listen 80;
    server_name example.com;
    location / { ... }
}

修改后 (Certbot 自动注入代码后):

Nginx

server {
    listen 80;
    server_name example.com;
    location / { ... }

    # --- 下面是 Certbot 自动插入的 ---
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

# --- Certbot 还可能自动在底部追加重定向块 ---
server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    listen 80;
    server_name example.com;
    return 404; # managed by Certbot
}

你会发现代码里多了很多 # managed by Certbot 的注释。


3. 多域名证书申请 (通用)

无论什么系统,申请多域名证书的逻辑一致。务必确保所有域名都解析到了当前服务器 IP。

Bash

# 同时也申请 www 和不带 www 的域名
sudo certbot certonly --nginx -d example.com -d www.example.com

4. 自动续期配置 (Systemd Timer)

Linux 系统通常使用 Systemd 来管理后台服务。我们需要配置一个定时器(Timer)来替代传统的 Cron 任务。

步骤 1:创建续期服务 (Service)

路径: /etc/systemd/system/certbot-renew.service

Ini, TOML

[Unit]
Description=Certbot Renewal Service
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
# 关键逻辑:尝试续期,成功后自动重载 Nginx
ExecStart=/usr/bin/certbot renew --post-hook "systemctl reload nginx"

步骤 2:创建定时器 (Timer)

路径: /etc/systemd/system/certbot-renew.timer

Ini, TOML

[Unit]
Description=Run certbot renew twice daily

[Timer]
# 每天 00:00 和 12:00 运行
OnCalendar=*-*-* 00/12:00:00
# 随机延迟 1 小时,避免整点高峰
RandomizedDelaySec=1h
Persistent=true

[Install]
WantedBy=timers.target

步骤 3:激活定时器12

Bash

# 1. 重载配置
sudo systemctl daemon-reload

# 2. 启用并运行
sudo systemctl enable --now certbot-renew.timer

# 3. 检查状态 (看到 Active: active 即成功)
systemctl status certbot-renew.timer

配置自动续期 之 手动配置

为什么手动配置? 虽然部分系统会自动安装 Cron 任务,但手动配置 Systemd Timer 更可控、可监控,且能确保 Nginx 在证书更新后自动重载。

1. 创建服务文件 (Service)

Bash

sudo tee /etc/systemd/system/certbot-renew.service > /dev/null <<EOF
[Unit]
Description=Certbot Renewal Service
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
# 核心:续期成功后 -> 重载 Nginx
ExecStart=/usr/bin/certbot renew --post-hook "systemctl reload nginx"
EOF

2. 创建定时器文件 (Timer)

Bash

sudo tee /etc/systemd/system/certbot-renew.timer > /dev/null <<EOF
[Unit]
Description=Run certbot renew twice daily

[Timer]
# 每天 00:00 和 12:00 运行
OnCalendar=*-*-* 00/12:00:00
RandomizedDelaySec=1h
Persistent=true

[Install]
WantedBy=timers.target
EOF

3. 激活并验证

Bash

# 重载配置
systemctl daemon-reload
# 启动定时器
systemctl enable --now certbot-renew.timer
# 验证状态 (应显示 Active)
systemctl status certbot-renew.timer

5. 常用运维命令速查表34

功能 命令 说明
查看证书列表9 certbot certificates10 查看剩余11有效期、域名列表
测试模拟续期 certbot renew --dry-run 检查续期逻辑是否通畅 (不实际修改)
强制立即续期 certbot renew --force-renewal 除非紧急,否则慎用 (有次数限制)
删除证书 certbot delete 按提示选择要删除的域名证书
查看定时器 systemctl status certbot-renew.timer 监控自动续期任务状态
Nginx 语法检查 nginx -t 每次改完配置必做

6. 常见问题 (FAQ)

Q: 我可以用 certbot --nginx 自动修改现有的复杂配置吗?

A: 不建议。 如果你的配置包含复杂的 proxy_pass、多端口监听或自定义的 if 逻辑,自动修改可能会打乱缩进,甚至导致逻辑错误(比如把 SSL 配置插到了错误的位置)。对于生产环境,始终建议使用 certonly 模式,然后手动复制路径。

Q: 阿里云安全组要注意什么?

A: Certbot 验证域名时,Let's Encrypt 的服务器会通过公网访问你的 80 端口。如果你的阿里云安全组(防火墙)没开 80 端口,验证会失败。

Q: 证书过期了会怎样?

A: 浏览器会弹出红色警告("您的连接不是私密连接"),用户无法正常访问。通过配置上述的自动续期,可以完全避免此问题。

Q: 我还需要去配置 crontab -e 吗?

A: 不需要。Systemd Timer 已经完全替代了 Cron 的功能。如果你的 /etc/cron.d/ 下有 certbot 文件,可以将其删除以避免重复运行(虽然重复运行也没坏处,Certbot 会自己判断)。

Q: 证书还有 80 天才过期,现在运行 renew 会怎样?

A: Certbot 会直接“跳过”续期(Skipped)。只有当有效期少于 30 天时,它才会真正向 Let's Encrypt 请求新证书。

Q: 为什么日志里显示 Skipped

A: 这是正常的。定时器每天运行两次,但在证书快过期之前,它大部分时间都在“空跑”检查,发现没过期就直接退出了。

posted @ 2026-01-16 16:01  Sappy  阅读(0)  评论(0)    收藏  举报