Linux 系统免费自动部署 HTTPS(SSL 证书)与自动续费教程

一、前置条件

  1. 环境准备
  • 一台运行 Linux 系统的服务器(推荐 Ubuntu 20.04+/Debian 11+、CentOS 7+/RHEL 7+);
  • 已安装 Web 服务器(Apache 或 Nginx,需正常运行);
  • 已注册域名(如example.com),且域名已解析到服务器公网 IP(A 记录或 AAAA 记录,解析需生效);
  • 服务器开放80 端口(HTTP)443 端口(HTTPS)(Let’s Encrypt 验证需 80 端口,HTTPS 服务需 443 端口);
  • 拥有服务器sudo权限(用于安装软件和修改配置)。
  1. 核心工具说明
  • Let’s Encrypt:免费、自动化的 SSL 证书机构,提供 90 天有效期的证书(需定期续费,本教程实现自动续费);
  • Certbot:Let’s Encrypt 官方推荐的客户端工具,可自动完成 “证书申请、Web 服务配置、到期续费” 全流程。

二、第一步:安装 Certbot 客户端

Certbot 支持通过系统包管理器安装,不同 Linux 发行版和 Web 服务器的安装命令略有差异,需根据实际环境选择。

1. 情况 1:Ubuntu/Debian 系统(Apache 或 Nginx)

(1)更新系统包列表

sudo apt update && sudo apt upgrade -y

(2)安装 Certbot 及对应 Web 服务器插件

  • 若使用 Apache:安装支持 Apache 的插件(自动修改 Apache 配置)
sudo apt install certbot python3-certbot-apache -y
  • 若使用 Nginx:安装支持 Nginx 的插件(自动修改 Nginx 配置)
sudo apt install certbot python3-certbot-nginx -y

2. 情况 2:CentOS/RHEL 系统(Apache 或 Nginx)

(1)安装 EPEL 仓库(CentOS/RHEL 默认无 Certbot 官方源,需先添加 EPEL)

# CentOS 7/RHEL 7sudo yum install epel-release -y# CentOS 8/RHEL 8+sudo dnf install epel-release -y

(2)安装 Certbot 及对应 Web 服务器插件

  • 若使用 Apache
# CentOS 7sudo yum install certbot python2-certbot-apache -y# CentOS 8+sudo dnf install certbot python3-certbot-apache -y
  • 若使用 Nginx
# CentOS 7sudo yum install certbot python2-certbot-nginx -y# CentOS 8+sudo dnf install certbot python3-certbot-nginx -y

三、第二步:申请 SSL 证书并自动配置 HTTPS

Certbot 的 Web 服务器插件(certbot-apache/certbot-nginx)可自动完成 “证书验证、证书下载、Web 服务 HTTPS 配置”,无需手动修改配置文件。

1. 通用操作逻辑

执行 Certbot 命令时,需指定:

  • 插件(–apache或–nginx);
  • 域名(-d 你的域名,支持多个域名用-d 域名1 -d 域名2);
  • 邮箱(用于证书到期提醒和找回,–email 你的邮箱)。

2. 情况 1:Web 服务器为 Apache

执行以下命令(替换your-email@example.comexample.com为实际信息):

sudo certbot --apache --email your-email@example.com -d example.com -d www.example.com

交互过程说明(按提示操作):

  1. 同意 Let’s Encrypt 服务条款:输入A(Agree);
  2. 共享邮箱用于安全通知:输入Y或N(推荐Y,接收续费提醒);
  3. 选择 HTTPS 配置方式:
  • 若提示Please choose whether or not to redirect HTTP traffic to HTTPS(是否将 HTTP 重定向到 HTTPS),推荐选择2(Redirect,强制 HTTPS,更安全)。

成功标志:

终端输出Congratulations! You have successfully enabled HTTPS on https://example.com,表示证书申请成功且 Apache 已自动配置 HTTPS。

3. 情况 2:Web 服务器为 Nginx

执行以下命令(替换your-email@example.comexample.com为实际信息):

sudo certbot --nginx --email your-email@example.com -d example.com -d www.example.com

交互过程说明(与 Apache 类似):

  1. 同意条款(A)、选择是否共享邮箱(Y/N);
  2. 选择 HTTP 重定向方式:推荐2(Redirect,强制 HTTPS)。

成功标志:

终端输出Congratulations! You have successfully enabled HTTPS on https://example.com,表示 Nginx 已自动配置 HTTPS。

四、第三步:验证 HTTPS 是否生效

证书配置完成后,通过以下方式验证:

1. 浏览器验证(最直观)

打开浏览器,输入你的域名(如https://example.com):

  • 地址栏显示 “绿色小锁”,表示 HTTPS 生效;
  • 点击小锁可查看证书信息(有效期、颁发机构为 Let’s Encrypt)。

2. 命令行验证(服务器端或本地终端)

使用curl命令检查证书:

curl -I https://example.com

成功输出特征:

  • 第一行显示HTTP/1.1 200 OK(或HTTP/2 200);
  • 包含Strict-Transport-Security头(强制 HTTPS 的安全头);
  • 无 “证书错误” 提示。

五、第四步:配置证书自动续费(核心!避免手动操作)

Let’s Encrypt 证书默认有效期为 90 天,Certbot 提供renew命令用于续费,结合 Linux 定时任务(crontab)可实现 “自动检测到期时间→自动续费→自动重启 Web 服务”。

1. 原理说明

  • Certbot 的renew命令会自动检查所有已申请的证书:若证书剩余有效期小于 30 天,自动执行续费;若大于 30 天,不执行操作(避免频繁请求 Let’s Encrypt 接口);
  • 通过crontab设置 “每日定时执行renew命令”,确保证书到期前完成续费。

2. 配置自动续费步骤

(1)测试续费命令是否正常工作

先手动执行续费命令,验证无错误(不会影响未到期证书):

sudo certbot renew --dry-run
  • 若输出Congratulations, all simulated renewals succeeded,表示续费命令正常。

(2)添加定时任务(crontab)

执行以下命令编辑定时任务列表:

sudo crontab -e

(3)添加定时任务语句

在打开的文件末尾添加以下内容(每天凌晨 3 点 30 分执行续费,时间可自定义):

30 3 * * * /usr/bin/certbot renew --quiet && systemctl reload apache2  # 若为Apache# 或30 3 * * * /usr/bin/certbot renew --quiet && systemctl reload nginx    # 若为Nginx
  • 参数说明:
  • 30 3 * * *:定时规则(分 时 日 月 周,即每天 3 点 30 分);
  • /usr/bin/certbot renew –quiet:静默执行续费(无输出,仅日志记录);
  • systemctl reload 服务名:续费后重启 Web 服务(让新证书生效,reload比restart更轻量,不中断服务)。

(4)保存并退出

  • 若使用nano编辑器(默认):按Ctrl+O保存,按Enter确认文件名,按Ctrl+X退出;
  • 若使用vim编辑器:按Esc,输入:wq保存退出。

3. 验证定时任务是否生效

可通过查看 Certbot 日志确认(日志路径因系统略有差异):

# Ubuntu/Debiansudo cat /var/log/letsencrypt/letsencrypt.log# CentOS/RHELsudo cat /var/log/letsencrypt/letsencrypt.log

后续若证书临近到期(剩余 30 天内),日志会显示Renewing an existing certificate,表示自动续费成功。

六、常见问题与解决方案

1. 问题 1:Certbot 验证失败(Failed authorization procedure)

原因:

  • 80 端口被占用(如其他服务占用 80 端口,Let’s Encrypt 需通过 80 端口验证域名所有权);
  • 域名解析未生效(域名未指向当前服务器 IP);
  • 防火墙未开放 80 端口。

解决方案:

  1. 检查 80 端口占用:sudo lsof -i :80,若有非 Web 服务占用,停止该服务(如sudo systemctl stop 占用服务);
  2. 验证域名解析:本地终端执行ping example.com,确认 IP 与服务器公网 IP 一致;
  3. 开放 80 端口:
  • Ubuntu/Debian:sudo ufw allow 80 && sudo ufw reload;
  • CentOS/RHEL:sudo firewall-cmd –permanent –add-port=80/tcp && sudo firewall-cmd –reload。

2. 问题 2:Web 服务器重启失败(systemctl reload 服务名报错)

原因:

  • Certbot 自动修改的 Web 配置文件存在语法错误(如 Nginx 的server块配置冲突)。

解决方案:

  1. 检查 Web 服务配置语法:
  • Apache:sudo apache2ctl configtest(Ubuntu)或sudo httpd -t(CentOS);
  • Nginx:sudo nginx -t;
  1. 根据报错信息修改配置文件(Apache 配置路径:/etc/apache2/sites-available/;Nginx 配置路径:/etc/nginx/sites-available/);
  2. 修复后重启服务:sudo systemctl restart apache2或sudo systemctl restart nginx。

3. 问题 3:自动续费未执行(证书到期未更新)

原因:

  • 定时任务(crontab)配置错误(如命令路径错误、服务名错误);
  • Certbot 日志无续费记录。

解决方案:

  1. 检查 crontab 命令路径:执行which certbot,确认路径是否为/usr/bin/certbot(若不是,修改定时任务中的路径);
  2. 手动执行定时任务命令,测试是否报错:
sudo /usr/bin/certbot renew --quiet && systemctl reload apache2  # Apache# 或sudo /usr/bin/certbot renew --quiet && systemctl reload nginx    # Nginx
  1. 查看 crontab 日志(Ubuntu/Debian):sudo cat /var/log/syslog | grep CRON,确认定时任务是否执行。

七、注意事项

  1. 证书备份:证书默认存储在/etc/letsencrypt/live/你的域名/,建议定期备份该目录(避免服务器故障导致证书丢失);
  2. 邮箱通知:Let’s Encrypt 会在证书到期前 14 天、7 天、1 天向申请时填写的邮箱发送提醒,即使配置了自动续费,也建议关注邮件;
  3. 避免频繁申请:Let’s Encrypt 对同一域名的证书申请有频率限制(每周最多 5 次),测试时尽量用–dry-run(模拟续费),避免真实申请;
  4. HTTPS 安全优化:可进一步配置HSTS(强制 HTTPS)、OCSP Stapling(加速证书验证),提升安全性和访问速度(Certbot 默认已配置基础 HSTS)。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注