Docker+certbot+Nginx实现自动续期https证书

使用 Docker 和 Certbot 配置 HTTPS 的完整指南

本文将详细介绍如何使用 Docker 和 Certbot 为你的网站配置 HTTPS,并设置自动续期任务。以下是详细步骤:


1. 下载 Certbot 镜像

首先,拉取 Certbot 的 Docker 镜像:

bash 复制代码
bash
复制
docker pull certbot/certbot

2. 创建 docker-compose.yml 文件

创建一个 docker-compose.yml 文件,定义 Nginx 和 Certbot 服务:

yaml 复制代码
yaml
复制
version: '3.8'

services:
  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf          # 主配置文件
      - ./nginx/conf:/etc/nginx/conf.d                  # 动态配置目录
      - ./nginx/html:/usr/share/nginx/html                # 网站根目录
      - ./certs:/etc/letsencrypt                            # 证书持久化存储(关键!)
      - ./nginx/logs:/var/log/nginx                       # 日志目录
    networks:
      - nginx-network
    restart: always
    command: >
      sh -c "nginx -g 'daemon off;'"
    depends_on:
      - certbot   # 确保 Certbot 先启动(仅首次申请时必要)

  certbot:
    image: certbot/certbot
    container_name: certbot
    volumes:
      - ./certs:/etc/letsencrypt                          # 证书存储(与 Nginx 共享)
      - ./nginx/html:/var/www/html                      # HTTP 验证所需的 Webroot
    networks:
      - nginx-network
    restart: always

networks:
  nginx-network:
    driver: bridge
    name: nginx-network

3. 创建 nginx.conf 文件

./nginx/nginx.conf 中配置 Nginx 的 HTTP 验证路径:

bash 复制代码
nginx
复制
server {
    listen 80;
    server_name xxx.com;  # 替换为你的域名

    # 配置 HTTP 验证可访问
    location /.well-known/acme-challenge/ {
        root /etc/letsencrypt;  # 对应 Certbot 的 Webroot 路径
    }

    # 301 永久重定向到 HTTPS(正式获取证书后启用)
    # return 301 https://$host$request_uri;
}

4. 启动服务

使用 Docker Compose 启动服务:

复制代码
bash
复制
docker compose up -d

5. 测试证书发放

运行以下命令测试证书发放是否正常:

css 复制代码
bash
复制
docker compose run --rm certbot certonly --webroot --webroot-path /etc/letsencrypt --dry-run -d xxx.com

如果输出 The dry run was successful.,说明测试成功。


6. 正式获取证书

如果测试成功,去掉 --dry-run 参数正式获取证书:

css 复制代码
bash
复制
docker compose run --rm certbot certonly --webroot --webroot-path /etc/letsencrypt -d xxx.com

按照提示输入邮箱等信息,完成证书申请。


7. 生成结果文件

Certbot 会在 ./certs/live/xxx.com/ 目录下生成证书文件,包括:

  • fullchain.pem:完整的证书链
  • privkey.pem:私钥

8. 创建 HTTPS 配置文件

./nginx/https.conf 中配置 HTTPS:

ini 复制代码
nginx
复制
server {
    listen       443 ssl;
    server_name  xxx.com;

    ssl_certificate /etc/letsencrypt/live/xjskr.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xjskr.com/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

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

    location /skr-oa {
        alias   /usr/share/nginx/html/oa;
        index  index.html index.htm;
    }

    location /okx {
        alias /usr/share/nginx/html/okx;
        index okx.html okx.htm;
    }

    location /love {
        alias   /usr/share/nginx/html/hp;
        index  index.html index.htm;
    }

    location /api/ {
        proxy_pass http://gateway:8001/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

将此文件挂载到 Nginx 的配置目录中,或者替换 nginx.conf 中的 server 块。


9. 重启 Nginx

重新加载 Nginx 配置以应用 HTTPS:

复制代码
bash
复制
docker compose restart nginx

10. 配置定时任务自动续期

为了避免证书过期,设置一个定时任务每月 1 号和 15 号自动续期证书并重启 Nginx:

复制代码
bash
复制
sudo crontab -e

添加以下内容:

bash 复制代码
bash
复制
0 0 1,15 * * cd /root/docker/nginx && /usr/bin/docker compose run --rm certbot renew && /usr/bin/docker compose restart nginx

保存后,定时任务将自动运行。


总结

通过以上步骤,你已经成功使用 Docker 和 Certbot 为你的网站配置了 HTTPS,并设置了自动续期任务。这样可以确保你的网站始终安全可靠,避免因证书过期导致的服务中断。

相关推荐
XXOOXRT1 分钟前
基于SpringBoot的加法计算器
java·spring boot·后端·html5
moxiaoran57531 小时前
Go语言的错误处理
开发语言·后端·golang
短剑重铸之日7 小时前
《7天学会Redis》特别篇: Redis分布式锁
java·redis·分布式·后端·缓存·redission·看门狗机制
小北方城市网7 小时前
SpringBoot 全局异常处理与接口规范实战:打造健壮可维护接口
java·spring boot·redis·后端·python·spring·缓存
hanqunfeng7 小时前
(三十三)Redisson 实战
java·spring boot·后端
小北方城市网8 小时前
SpringBoot 集成 MyBatis-Plus 实战(高效 CRUD 与复杂查询):简化数据库操作
java·数据库·人工智能·spring boot·后端·安全·mybatis
hanqunfeng9 小时前
(四十)SpringBoot 集成 Redis
spring boot·redis·后端
小北方城市网10 小时前
SpringBoot 集成 MinIO 实战(对象存储):实现高效文件管理
java·spring boot·redis·分布式·后端·python·缓存
程序员泠零澪回家种桔子10 小时前
RAG自查询:让AI精准检索的秘密武器
人工智能·后端·算法
曹轲恒10 小时前
SpringBoot配置文件(1)
java·spring boot·后端