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

28 阅读3分钟

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

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


1. 下载 Certbot 镜像

首先,拉取 Certbot 的 Docker 镜像:

docker pull certbot/certbot

2. 创建 docker-compose.yml 文件

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

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 验证路径:

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 启动服务:

docker compose up -d

5. 测试证书发放

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

docker compose run --rm certbot certonly --webroot --webroot-path /etc/letsencrypt --dry-run -d xxx.com

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


6. 正式获取证书

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

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:

server {
    listen       443 ssl;
    server_name  xxx.com;

    ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xxx.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 /xxx-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:

docker compose restart nginx

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

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

sudo crontab -e

添加以下内容:

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,并设置了自动续期任务。确保网站始终安全可靠,避免因证书过期导致的服务中断。