【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 启用成功
相关推荐
网硕互联的小客服18 分钟前
服务器防火墙是如何区分正常流量和攻击流量?
运维·服务器·网络
安当加密26 分钟前
基于 RADIUS 的 Linux 服务器双因子认证:从 FreeRADIUS 到轻量级 ASP 方案的演进
linux·运维·服务器
ai产品老杨1 小时前
打破异构算力壁垒:基于GB28181/RTSP与Docker容器化的企业级AI视频平台架构解析(附源码交付方案)
人工智能·docker·音视频
me8321 小时前
【Java】踩坑实录:Spring Boot + Nginx 本地部署404终极排查:从80端口被占用到配置生效全流程
java·spring boot·nginx
西西学代码1 小时前
Flutter---路由与导航
服务器·前端·javascript
wangqiaowq1 小时前
llama.cpp 启动命令优化
运维·服务器
江湖有缘2 小时前
本地化JSON 处理新方案:基于 Docker的JSON Hero部署全记录
java·docker·json
l1t2 小时前
在华为arm64 kylin计算机上安装docker编译llama.cpp的步骤
华为·docker·llama·kylin
double2li2 小时前
linux/unix 段错误捕获【续】
linux·运维·服务器·网络·unix
万能菜道人2 小时前
多次连接共享内存大小不一致在win和linux的区别
linux·运维·服务器