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
相关推荐
ITOWARE_SAPer8 小时前
选择SAP实施公司能否兼得官方授权与高性价比?
运维·能源·制造·零售
Elastic 中国社区官方博客8 小时前
Elasticsearch:快速近似 ES|QL - 第一部分
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索
学代码的真由酱9 小时前
HTTPS
网络协议·http·https
AC赳赳老秦9 小时前
OpenClaw生成博客封面图+标题,适配CSDN视觉搜索,提升点击量
运维·人工智能·python·自动化·php·deepseek·openclaw
Eric.Lee202110 小时前
docker 启动停止命令
运维·docker·容器
samson_www10 小时前
EC2的GRUB引导程序问题
运维·ai
代码中介商11 小时前
Linux 帮助手册与用户管理完全指南
linux·运维·服务器
weixin_4491736513 小时前
Linux -- 项目中查找日志的常用Linux命令
linux·运维·服务器
深念Y14 小时前
赛米尼M02/海纳斯HiNAS系统-WiFi驱动安装教程
运维·服务器·网络·docker·nas·机顶盒·hinas
会飞的土拨鼠呀14 小时前
Ubuntu24.04安装open-vm-tools
运维·服务器