CentOS Stream 8 使用 Certbot 配置 HTTPS 证书指南

前言

趁着双十一新买了个腾讯云服务器,心血来潮把以前装过的应用再装一下玩玩,到了配置 https 环节,基于以前的经验,certbot 自动申请 SSL 证书并自动续签是最佳方案,但具体怎么安装怎么配置已经忘了,这次仍然又踩了很多坑才配好,这次把过程记录下来,希望下次再有类似经历能少踩点坑

1. 配置系统镜像源

bash 复制代码
# 删除原有的 repo 文件
sudo rm -f /etc/yum.repos.d/*

# 创建新的 repo 文件
sudo vi /etc/yum.repos.d/centos.repo

添加以下内容(使用阿里云镜像):

bash 复制代码
[base]
name=CentOS Stream $releasever - Base
baseurl=https://mirrors.aliyun.com/centos/8-stream/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

[appstream]
name=CentOS Stream $releasever - AppStream
baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

[extras]
name=CentOS Stream $releasever - Extras
baseurl=https://mirrors.aliyun.com/centos/8-stream/extras/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

[epel]
name=Extra Packages for Enterprise Linux $releasever - $basearch
baseurl=https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8

更新系统:

bash 复制代码
sudo dnf clean all
sudo dnf makecache
sudo dnf update -y

2. 安装 Certbot

bash 复制代码
sudo dnf install certbot python3-certbot-nginx -y

3. 配置网站(以宝塔面板为例)

  1. 在宝塔面板中添加站点

  2. 配置反向代理(如果需要):

    • 目标 URL:http://127.0.0.1:端口号
    • 发送域名:$host
  3. 确保域名已解析到服务器 IP

4. 申请 SSL 证书

bash 复制代码
# 使用 certbot 申请证书,注意指定 nginx 配置路径
sudo certbot --nginx --nginx-server-root=/www/server/nginx/conf -d your-domain.com

按提示操作:

  • 输入邮箱(用于证书过期提醒)
  • 同意服务条款
  • 选择是否重定向 HTTP 到 HTTPS

5. 验证自动续期

bash 复制代码
# 测试续期是否正常工作
sudo certbot renew --dry-run --nginx-server-root=/www/server/nginx/conf

# 查看证书信息和过期时间
sudo certbot certificates

注意事项

  • 确保服务器的 80 和 443 端口开放
  • 证书有效期为 90 天
  • 系统会在证书过期前 30 天自动续签
  • 续签失败会发送邮件通知
  • certbot 安装时会自动创建定时任务,无需手动配置自动续期

常用命令

bash 复制代码
# 查看证书状态
sudo certbot certificates

# 查看自动续期任务
systemctl list-timers | grep certbot

# 查看证书续期日志
sudo tail -f /var/log/letsencrypt/letsencrypt.log

如果发现没有配置自动续期,则可以手动配置

配置自动续期

  1. 首先测试续签是否正常工作:
css 复制代码
sudo certbot renew --dry-run --nginx-server-root=/www/server/nginx/conf
  1. certbot 在安装时会自动创建定时任务,我们可以查看:
perl 复制代码
systemctl list-timers | grep certbot

bash 复制代码
ls -l /etc/cron.d/certbot
  1. 如果要手动创建或修改自动续签任务,可以编辑 crontab:

    sudo crontab -e

添加以下内容(每天两次检查,随机延迟防止服务器同时请求):

javascript 复制代码
0 0,12 * * * /usr/bin/certbot renew --nginx-server-root=/www/server/nginx/conf --quiet --deploy-hook "systemctl reload nginx"
  1. 查看证书信息和过期时间:

    sudo certbot certificates

删除证书

查看现有证书

bash 复制代码
certbot certificates

删除指定域名证书

bash 复制代码
sudo certbot delete --cert-name example.com
相关推荐
learning-striving5 小时前
华为云欧拉操作系统的服务器实例中手工部署 Docker
linux·运维·服务器·docker·容器·华为云
小此方5 小时前
Re:Linux系统篇(十五)工具篇 ·六:GDB 调试从底层逻辑到高阶实战
linux·运维·服务器·chrome
白菜欣12 小时前
Linux — 进程控制
android·linux·运维
JoneBB13 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
Tolalal13 小时前
Vmware Ubuntu虚拟机扩容
linux·运维·ubuntu
咚为14 小时前
比AccessLog更全面的原生Nginx 日志记录
运维·nginx·junit
我星期八休息14 小时前
Linux系统编程—基础IO
linux·运维·服务器·c语言·c++·人工智能·算法
a7520662814 小时前
零基础实操:小龙虾 AI OpenClaw 接入 Kimi 详细步骤
运维·服务器
Goldbioinformatics15 小时前
Windows版Claude Cowork启动Linux问题
linux·运维·windows