使用Docker申请Let‘s Encrypt证书

发布于:2025-06-13 ⋅ 阅读:(19) ⋅ 点赞:(0)

1、安装Docker

# 安装Docker
https://docs.docker.com/get-docker/

# 安装Docker Compose
https://docs.docker.com/compose/install/

# CentOS安装Docker
https://mp.weixin.qq.com/s/nHNPbCmdQs3E5x1QBP-ueA

2、申请Let's Encrypt证书

详见:
https://docs.linuxserver.io/general/swag
https://github.com/linuxserver/docker-swag
https://hub.docker.com/r/linuxserver/swag

创建目录:

mkdir letsencrypt
cd letsencrypt

创建docker-compose.yaml文件:

services:
  swag:
    image: linuxserver/swag:latest
    container_name: swag
    cap_add:
      - NET_ADMIN
    environment:
      - PUID=0
      - PGID=0
      - TZ=Asia/Shanghai
      - URL=example.com
      - VALIDATION=http
      - SUBDOMAINS=www, #optional
      - CERTPROVIDER= #optional
      - DNSPLUGIN=cloudflare #optional
      - PROPAGATION= #optional
      - EMAIL= #optional
      #- ONLY_SUBDOMAINS=false #optional
      - ONLY_SUBDOMAINS=true #optional
      - EXTRA_DOMAINS= #optional
      - STAGING=false #optional
      - DISABLE_F2B= #optional
      - SWAG_AUTORELOAD= #optional
      - SWAG_AUTORELOAD_WATCHLIST= #optional
    volumes:
      - ./config:/config
    ports:
      - 443:443
      - 80:80 #optional
    restart: unless-stopped
说明:
1、根据实际情况修改PUID、PGID的值,使用id命令来查看当前用户的PUID、PGID的值
2、根据实际情况修改URL、SUBDOMAINS的值,URL为域名、SUBDOMAINS为子域名
3、将ONLY_SUBDOMAINS的值设置为true,只获取子域名证书
参数说明:

-p 443:443	HTTPS 端口。
-p 80	HTTP端口(HTTP 验证和 HTTP -> HTTPS 重定向所需)。
-e PUID=1000	UserID。
-e PGID=1000	GroupID。
-e TZ=Etc/UTC	指定要使用的时区。
-e URL=example.com	域名/DNS(example.com 或者 customsubdomain.example.com)。
-e VALIDATION=http	使用Certbot验证方法,选项为http或dns。
-e SUBDOMAINS=www,	子域名(逗号分隔,无空格),如www,ftp,cloud。对于通配符证书,设置为wildcard。
-e CERTPROVIDER=	可选地定义证书提供商。设置zerossl为 ZeroSSL 证书(需要现有的ZeroSSL 帐户EMAIL和在环境变量中输入的电子邮件地址)。否则默认为 Let's Encrypt。
-e DNSPLUGIN=cloudflare	如果VALIDATION设置为dns。
-e PROPAGATION=	可选择覆盖(以秒为单位)dns 插件的默认传播时间。
-e EMAIL=	用于证书到期通知的可选电子邮件地址(ZeroSSL 必需)。
-e ONLY_SUBDOMAINS=false	如果您希望只获取某些子域的证书,而不是主域的证书(主域可能托管在另一台机器上,无法验证),请将其设置为true。
-e EXTRA_DOMAINS=	额外的完全限定域名(以逗号分隔,无空格),如example.net、subdomain.example.net、*.example.org。
-e STAGING=false	设置为true以在暂存模式下检索证书。速率限制会更高,但生成的证书将无法通过浏览器的安全测试。仅用于测试目的。
-e DISABLE_F2B=	如果您已经在其他地方运行该服务或使用不同的 IPS,则设置true为禁用容器中的 Fail2ban 服务。
-e SWAG_AUTORELOAD=	设置为true,以便在配置文件发生更改时自动重新加载,而无需停止/重启 nginx。您的文件系统必须支持 inotify。此功能之前通过 modtrue提供。
-e SWAG_AUTORELOAD_WATCHLIST=	除以下文件夹外,还包含一个管道分隔的用于自动重新加载的附加文件夹列表:/config/nginx。
-v /config	持久配置文件。
--read-only=true	使用只读文件系统运行容器。
--cap-add=NET_ADMIN	需要 fail2Ban 才能修改 iptables 规则。

创建并启动容器:

docker-compose up -d

查看容器列表:

docker ps

查看日志:

docker logs swag

进入容器:

docker exec -it swag bash

重启容器:

docker restart swag

停止并销毁容器:

docker-compose down

删除镜像:

docker rmi linuxserver/swag:latest

删除目录:

rm -rf ./config

SSL证书文件:

# 证书路径:
./config/etc/letsencrypt/live/域名


# 证书文件:
cert.pem -> ../../archive/域名/cert1.pem
chain.pem -> ../../archive/域名/chain1.pem
fullchain.pem -> ../../archive/域名/fullchain1.pem
priv-fullchain-bundle.pem
privkey.pem -> ../../archive/域名/privkey1.pem
privkey.pfx
README


# README文件:
This directory contains your keys and certificates.

`privkey.pem`  : the private key for your certificate.
`fullchain.pem`: the certificate file used in most server software.
`chain.pem`    : used for OCSP stapling in Nginx >=1.3.7.
`cert.pem`     : will break many server configurations, and should not be used
                 without reading further documentation (see link below).

WARNING: DO NOT MOVE OR RENAME THESE FILES!
         Certbot expects these files to remain in this location in order
         to function properly!

We recommend not moving these files. For more information, see the Certbot
User Guide at https://certbot.eff.org/docs/using.html#where-are-my-certificates.


# 说明:
私钥文件:privkey.pem
证书文件:fullchain.pem

创建./config/nginx/proxy-confs/域名.subdomain.conf文件:

详见:./config/nginx/proxy-confs/homepage.subdomain.conf.sample
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name 域名;
    #include /config/nginx/ssl.conf;
    client_max_body_size 0;
    location / {
        root /config/www;
        #include /config/nginx/proxy.conf;
        #include /config/nginx/resolver.conf;
        #set $upstream_app homepage;
        #set $upstream_port 3000;
        #set $upstream_proto http;
        #proxy_pass $upstream_proto://$upstream_app:$upstream_port;
    }
}

重启nginx服务:

# 重启nginx服务:
docker exec -it swag nginx -s reload

# curl访问:
curl -L 域名

浏览器访问:

3、详见

https://mp.weixin.qq.com/s/vNwwniGxGcKTPKKNJjYjfg
https://www.linuxserver.io/
https://docs.linuxserver.io/general/swag
https://github.com/linuxserver/docker-swag
https://hub.docker.com/r/linuxserver/swag
https://github.com/linuxserver-archive/docker-letsencrypt
https://hub.docker.com/r/linuxserver/letsencrypt
https://mp.weixin.qq.com/s/SOjKZ7ekufLVJ_dmfKwjZQ