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
相关推荐
睡觉z4 小时前
Jenkins持续集成系统
运维·ci/cd·jenkins
Wy_编程8 小时前
Linux文件相关命令
linux·运维
Viking_bird8 小时前
centos 7.5 + Hadoop 3.2.4 集群搭建
linux·运维·服务器·hadoop·centos
2501_915918418 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
黑客影儿9 小时前
Kali Linux 环境中的系统配置文件与用户配置文件大全
linux·运维·程序人生·安全·网络安全·系统安全·学习方法
岚天start9 小时前
Linux系统网络排查工具总结
linux·运维·网络·监控·扫描·连通性·流量
Lovyk11 小时前
基于 Ansible 与 Jinja2 模板的 LNMP 环境及 WordPress 自动化部署实践
linux·运维·服务器·自动化·ansible
知白守黑26711 小时前
Linux磁盘阵列
linux·运维·服务器
维尔切12 小时前
Linux中基于Centos7使用lamp架构搭建个人论坛(wordpress)
linux·运维·架构
丘大梨14 小时前
QT 基础聊天应用项目文档
运维·数据库·系统架构