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:

bash 复制代码
docker ps | grep nginx

然后停止容器:

bash 复制代码
docker stop <nginx-container-id>

2. 申请 SSL 证书

运行 Certbot,使用 standalone 模式:

bash 复制代码
certbot certonly --standalone -d youdomain.com -d www.youdomain.com

说明

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

3. 启动 Nginx

bash 复制代码
docker start <nginx-container-id>

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

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

1. 确定 Nginx 容器的 Webroot 路径

进入容器:

bash 复制代码
docker exec -it <nginx-container-id> sh

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

bash 复制代码
grep -r "root" /etc/nginx/nginx.conf

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

2. 申请 SSL 证书

在宿主机运行:

bash 复制代码
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):

nginx 复制代码
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:

bash 复制代码
docker restart <nginx-container-id>

配置 SSL 证书自动续约

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

1. 测试续约

bash 复制代码
certbot renew --dry-run

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

2. 设置自动续约

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

bash 复制代码
crontab -e

添加:

bash 复制代码
0 2 * * * certbot renew --quiet --deploy-hook "docker restart <nginx-container-id>"

解释

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

验证证书是否生效

bash 复制代码
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 证书会自动更新,无需手动操作!🚀

相关推荐
莫陌尛.6 小时前
Docker MySQL 删表卡死、执行超时(MDL锁阻塞)问题复盘与根治方案
mysql·docker·容器
星辰徐哥11 小时前
本地视频预览别只自己看:把Remotion动效项目发给客户远程验收
docker·ai·node.js·html·音视频·react·remotion
fb_1234515 小时前
Docker入门-第3章-容器镜像深度解析
运维·docker·容器
IT摆渡者16 小时前
基于 Docker 部署 Nextcloud 私有云盘(外挂 Windows 共享)项目实践
运维·docker·容器
新时代牛马18 小时前
单机 docker run 管不住了?从编排需求、期望状态到调度/自愈闭环讲透
运维·docker·容器
binqian18 小时前
Docker Desktop(WSL2 后端)三层网络互通技术文档
网络·docker·容器
LuiChun19 小时前
与精神病患者的日常探讨20260913
docker
闲云野鹤在人间19 小时前
Docker入门|第1章 容器生态系统完整解析
运维·服务器·docker·容器·centos
KIDULT°21 小时前
Docker 从 0 入门|吃透容器生态、架构与 Dockerfile 实战
docker·容器·架构
putItInYourHand1 天前
Docker Compose 部署 Mosquitto MQTT Broker 完整实战指南
运维·docker·容器