当前位置: 首页 > news >正文

Certbot实现SSL免费证书自动续签(CentOS 7版 + Docker部署的nginx)

前置安装,可参考Certbot实现SSL免费证书自动续签(CentOS 7 + nginx/apache)

如果是通过 Docker 运行 Nginx, certbot 无法直接检测到本地的 Nginx 配置。解决方案是 使用 standalone 模式挂载 Webroot 方式获取 SSL 证书,并手动配置 Nginx。


方案 1:Standalone 模式(临时关闭 Nginx 获取证书)

如果你的服务器 不支持 Webroot(或 Nginx 配置不方便修改),可以使用 Standalone 模式,但需要 短暂关闭 Nginx

1. 停止 Nginx 容器

先找到 Nginx 容器 ID:

docker ps | grep nginx

然后停止容器:

docker stop <nginx-container-id>

2. 申请 SSL 证书

运行 Certbot,使用 standalone 模式:

certbot certonly --standalone -d youdomain.com -d www.youdomain.com

说明

  • certonly:只获取证书,不修改配置
  • --standalone:使用 Certbot 内置的 web 服务器进行验证(需要 80 端口)
  • -d 指定域名

3. 启动 Nginx

docker start <nginx-container-id>

方案 2:Webroot 模式(不停止 Nginx)

适用于 Nginx 容器可挂载 /var/www/html 或有 root 目录

1. 确定 Nginx 容器的 Webroot 路径

进入容器:

docker exec -it <nginx-container-id> sh

查找 root 路径(通常是 /usr/share/nginx/html):

grep -r "root" /etc/nginx/nginx.conf

假设 Webroot 是 /usr/share/nginx/html,退出容器。

2. 申请 SSL 证书

在宿主机运行:

certbot certonly --webroot -w /usr/share/nginx/html -d youdomain.com -d www.youdomain.com

输入邮箱后按enter
协议输入Y后按enter
是否接收来自官方的新闻等邮件通知,可以选择Y/N,不影响使用

3. 配置 Nginx 使用 SSL

编辑 nginx.conf(在 server 配置中添加 SSL):

server {
    listen 443 ssl;
    server_name youdomain.com www.youdomain.com;

    ssl_certificate /etc/letsencrypt/live/youdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/youdomain.com/privkey.pem;

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

注意:容器要挂载/etc/letsencrypt目录 :ro表示只读

-v /etc/letsencrypt:/etc/letsencrypt:ro

然后重启 Nginx:

docker restart <nginx-container-id>

配置 SSL 证书自动续约

Let’s Encrypt 证书 90 天 后过期,因此需要自动续约。

1. 测试续约

certbot renew --dry-run

如果显示 Congratulations,说明续约正常。

2. 设置自动续约

添加 crontab 任务,每天检查证书并重启 Nginx:

crontab -e

添加:

0 2 * * * certbot renew --quiet --deploy-hook "docker restart <nginx-container-id>"

解释

  • 0 2 * * *:每天凌晨 2 点运行
  • certbot renew --quiet:静默续约
  • delopy-hook:只有成功续期后才执行后面的命令
  • docker restart nginx:续约后重启 Nginx 使新证书生效

验证证书是否生效

certbot certificates
或者
openssl s_client -connect yourdomain.com:443 -servername yourdomain.com | openssl x509 -noout -dates

如果 Not After 日期已更新,说明证书续约成功!✅


总结

  • Docker 运行 Nginx 不能直接用 certbot --nginx
  • 推荐 standalone 模式(需临时关闭 Nginx)
  • 或者 webroot 模式(需手动修改 Nginx 配置)
  • 自动续约通过 cron 计划任务完成

这样,你的 SSL 证书会自动更新,无需手动操作!🚀

相关文章:

  • 【MySQL篇】基本查询实战OJ
  • [Hello-CTF]RCE-Labs超详细WP-Level13Level14(PHP下的0/1构造RCE命令简单的字数限制RCE)
  • python爬虫Scrapy(5)之CrawlSpider
  • vscode--工作区和相对路径
  • Nginx静态资源访问设置
  • PyTorch系列教程:高效保存和加载PyTorch模型
  • Redis中常见的问题
  • 蓝牙基础知识学习补充
  • 前端工程化之前端工程化详解 包管理工具
  • 深度学习多模态人脸情绪识别:从理论到实践
  • 卷积神经网络(CNN)的主要架构
  • 数据库的基本知识
  • pytest+allure+jenkins
  • 力扣 11.盛水最多的容器(双指针)
  • matlab 八自由度汽车垂向动力学参数优化带座椅
  • ​【C++设计模式】第二十一篇:模板方法模式(Template Method)
  • Docker命令笔记
  • 网页制作14-Javascipt时间特效の显示动态日期
  • HTB 学习笔记 【中/英】《Web 应用 - 布局》P2
  • JavaCV
  • 光明日报:家长孩子共同“息屏”,也要保证高质量陪伴
  • 河南省委常委会会议:坚持以案为鉴,深刻汲取教训
  • 广东省人大教科文卫委原主任委员梁万里被开除党籍:退休后受贿仍不知止
  • 线下哪些商家支持无理由退货?查询方法公布
  • 80后莆田市文旅局长马骏登台与杨宗纬合唱,“演唱会秒变旅游推介会”
  • 欧阳娜娜携家人回江西探亲,受聘为江西吉安文化旅游大使