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
相关推荐
地衣君14 分钟前
Ubuntu 配置使用 zsh + 插件配置 + oh-my-zsh 美化过程
linux·运维·ubuntu
2401_8582861116 分钟前
OS11.【Linux】vim文本编辑器
linux·运维·服务器·编辑器·vim
朱包林24 分钟前
day27-shell编程(自动化)
linux·运维·服务器·网络·shell脚本
钢铁男儿24 分钟前
一文掌握 Tombola 抽象基类的自动化子类测试策略
运维·自动化
kaede33 分钟前
Linux实现线程同步的方式有哪些?
linux·运维·云计算
子正1 小时前
一键编译包含多个独立模块和应用的工程(linux cmake)
linux·运维·cmake
小阳睡不醒1 小时前
小白成长之路-Linux Shell脚本练习
linux·运维·服务器
dessler2 小时前
代理服务器-LVS的DR模式
linux·运维·云计算
cui_win3 小时前
每日一令:Linux 极简通关指南 - 汇总
linux·运维·服务器
知星小度S3 小时前
Linux权限探秘:驾驭权限模型,筑牢系统安全
linux·运维·服务器