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
相关推荐
志栋智能8 小时前
低成本自动化巡检:7×24小时守护业务稳定
运维·网络·自动化
ToB营销学堂8 小时前
MarketUP | B2B 自动化营销实战:如何打破“营-销”数据孤岛,构建高转化线索流?
运维·自动化
CDN3609 小时前
360CDN 全系列产品体验:CDN / 高防 / SDK 游戏盾横向测评
运维·安全·游戏
行者-全栈开发10 小时前
容器化时代来临 - Docker 技术演进与核心价值
运维·docker·容器·cicd·自动化构建·容器化时代
云飞云共享云桌面10 小时前
非标自动化研发成本高?云飞云共享云桌面:1台主机=10台工作站,年省数十万。
大数据·运维·服务器·人工智能·自动化·云计算·电脑
chehaoman10 小时前
Failed to restart nginx.service Unit nginx.service not found
运维·nginx
Linux运维技术栈10 小时前
生产环境Linux应用目录迁移至LVM独立分区 标准化实战方案
linux·运维·服务器·lvm·逻辑卷
feasibility.11 小时前
SSH Agent Forwarding 与 tmux 排障笔记
linux·运维·服务器·经验分享·笔记·ssh
ShawnLiaoking11 小时前
Linux 会话窗口常开
linux·运维·服务器
CDN36012 小时前
中小团队加速 + 防护方案:360CDN+SDK 游戏盾实测
运维·游戏·网络安全