【Docker】快速部署 Certbot 并为 Nginx 服务器配置 SSL/TLS 证书

【Docker】快速部署 Certbot 并为 Nginx 服务器配置 SSL/TLS 证书

引言

Certbot 是一个免费的开源工具,用于自动化管理和获取 SSL/TLS 证书,主要用于与 Let's Encrypt 证书颁发机构交互。

步骤

  1. Nginx 挂载 certbot 文件夹。
bash 复制代码
docker run -d \
  --name nginx \
  -p 80:80 \
  -p 443:443 \
  -v "$(pwd)/nginx/nginx.conf:/etc/nginx/nginx.conf" \
  -v "$(pwd)/nginx/conf.d:/etc/nginx/conf.d" \
  -v "$(pwd)/nginx/log:/var/log/nginx" \
  -v "$(pwd)/nginx/html:/usr/share/nginx/html" \
  -v "$(pwd)/certbot/www:/usr/share/certbot/www:ro" \
  -v "$(pwd)/certbot/ssl:/usr/share/certbot/ssl:ro" \
  --restart always \
  nginx:latest
  1. 修改 Nginx 配置文件 default.conf。
bash 复制代码
server {
    listen       80;
    listen  [::]:80;
    server_name  example.com www.example.com;

    location /.well-known/acme-challenge/ {
        root /usr/share/certbot/www;
    }

    location / {
        root   /usr/share/nginx/html;
        try_files $uri $uri/ /index.html last;
        index  index.html index.htm;
    }
}
  1. 创建证书(仅需运行一次)
bash 复制代码
docker run --rm \
  -v "$(pwd)/www:/usr/share/certbot/www:rw" \
  -v "$(pwd)/ssl:/etc/letsencrypt:rw" \
  certbot/certbot certonly \
  --webroot -w /usr/share/certbot/www \
  -d example.com \
  -d www.example.com \
  --non-interactive \
  --agree-tos \
  --expand \
  -m example@example.com
  1. 修改 Nginx 配置文件 default.conf,启用 SSL 证书。
bash 复制代码
server {
    listen       80;
    listen  [::]:80;
    server_name  example.com www.example.com;

    location /.well-known/acme-challenge/ {
        root /usr/share/certbot/www;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen       443 ssl;
    listen  [::]:443 ssl;
    server_name  example.com www.example.com;

    ssl_certificate /usr/share/certbot/ssl/live/example.com/fullchain.pem;
    ssl_certificate_key /usr/share/certbot/ssl/live/example.com/privkey.pem;

    location / {
        root   /usr/share/nginx/html;
        try_files $uri $uri/ /index.html last;
        index  index.html index.htm;
    }
}
  1. https 启用成功
相关推荐
中云DDoS CC防护蔡蔡1 小时前
DDOS和CC攻击谁更可怕
运维·服务器·经验分享·网络安全·ddos
Ruiery1 小时前
Linux 6.6内核内存管理深度解析(十六):mmap — 用户态“申请内存“的真相
linux·运维·服务器
欧米欧1 小时前
Linux系统篇(七),环境变量的底层溯源,核心获取机制与安全应用
linux·运维·服务器
Ruiery1 小时前
Linux 6.6内核内存管理深度解析(十八):KASLR 内核地址空间随机化
linux·运维·服务器·安全
todoitbo2 小时前
群晖NAS部署JmalCloud:Docker安装、文件管理、博客与公网访问完整教程
运维·docker·容器
前端精髓2 小时前
调用大模型API
运维·服务器·microsoft
逆风的蚂蚁4 小时前
linux服务器性能调整怎么落地?我把 CPU 调度、I/O 优先级和内核参数这几个旋钮都拧了一遍
linux·运维·服务器
ESDWAN4 小时前
企业网络监控应该监测哪些目标?网关、网络节点还是业务系统?
服务器·网络·php
发光小北6 小时前
EtherNet/IP 转 ModbusTCP 网关现场应用中会遇到什么问题?
服务器·网络·tcp/ip
程序猿老A6 小时前
轻量云服务器总是卡,内存占比高?
运维·服务器