跳至主内容

Let's Encrypt

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

Let's Encrypt 是一项为用户提供免费 SSL/TLS 证书的服务。Certbot 是一个客户端工具,可轻松实现证书的获取与自动化管理。此外,它还提供 Apache 和 Nginx 插件,进一步简化证书生成的自动化流程。

大多数 Linux 发行版的安装指南可在 Certbot 官网查阅。

安装完成后,即可开始生成新证书。

Apache

Certbot Apache 插件

安装 Certbot 及其 Apache 插件后,通过以下命令生成证书:

certbot certonly --apache --noninteractive --agree-tos --email YOUR_EMAIL -d DOMAIN_NAME

更新配置文件中的 'SSLCertificateFile' 和 'SSLCertificateKeyFile' 字段,然后重启服务。

添加 cron 定时任务以实现自动续期。

echo "0 0 * * *  root  certbot renew --quiet --no-self-upgrade --post-hook 'systemctl reload apache2'" | sudo tee -a /etc/cron.d/renew_certbot

Certbot Webroot 方式

Debian

若 Apache 插件与您的配置不兼容,可改用 webroot 方式。

配置反向代理后,在 <VirtualHost> 区块添加以下内容:

DocumentRoot /var/www/html/
#Do not pass the .well-known directory when using certbot and webroot
ProxyPass /.well-known !

以 root 身份执行 certbot 命令:

sudo certbot certonly --webroot -w /var/www/html --agree-tos --email YOUR_EMAIL -d DOMAIN_NAME

Caddy

Caddy 在提供域名后会自动从 Let's Encrypt 获取 SSL 证书,无需手动操作。

HAProxy

HAProxy 目前没有 Certbot 插件。解决方案是:以独立模式运行 Certbot,并通过网络代理流量。

启用上述配置中的前端(frontend)和后端(backend),然后运行 Certbot。

certbot certonly --standalone --preferred-challenges http-01 --http-01-port 8888 --noninteractive --agree-tos --email YOUR_EMAIL -d DOMAIN_NAME

端口可自定义,但需确保 HAProxy 配置与 Certbot 命令中的端口一致。

HAProxy 需将证书和密钥文件合并为同一文件才能正确读取。可通过以下命令实现:

cat /etc/letsencrypt/live/DOMAIN_NAME/fullchain.pem /etc/letsencrypt/live/DOMAIN_NAME/privkey.pem > /etc/ssl/DOMAIN_NAME.pem

取消配置文件中 bind *:443 和重定向区块的注释,然后重新加载服务。

自动证书续期

将以下脚本放置于 /usr/local/bin/ 以实现 SSL 证书自动更新:

SITE=DOMAIN_NAME

# move to the correct let's encrypt directory
cd /etc/letsencrypt/live/$SITE

# cat files to make combined .pem for haproxy
cat fullchain.pem privkey.pem > /etc/ssl/$SITE.pem

# reload haproxy
service haproxy reload

确保脚本具有可执行权限:

chmod u+x /usr/local/bin/letsencrypt-renew.sh

添加 cron 定时任务以实现自动续期。

@monthly /usr/bin/certbot renew --renew-hook "/usr/local/bin/letsencrypt-renew.sh" >> /var/log/letsencrypt-renewal.log

Nginx

通过 sudo apt install certbot python3-certbot-nginx 安装 Certbot 及 Nginx 插件后,生成证书:

注意:Fedora 系发行版(如 CentOS 8)请使用 sudo dnf install python3-certbot-nginx 安装 Nginx 插件。

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email YOUR_EMAIL -d DOMAIN_NAME

如需生成 4096 位密钥,添加 --rsa-key-size 4096 参数。

复制上方完整的 Nginx 示例配置文件,根据您的设置修改参数并取消注释相应行。

添加 cron 定时任务以实现自动续期。

echo "0 0 * * *  root  certbot renew --quiet --no-self-upgrade --post-hook 'systemctl reload nginx'" | sudo tee -a /etc/cron.d/renew_certbot

Let's Encrypt 与 Docker

本节假设 Jellyfin 运行在 Docker 容器(Linux 环境),且您希望在 Docker 容器中运行 Let's Encrypt。推荐使用内置 Nginx 反向代理的 linuxserver/swag 容器。

linuxserver/letsencrypt 已弃用,请迁移至 linuxserver/swag。如需迁移指南,请参阅 GitHub 上的 SWAG 迁移说明

首先需要确认以下事项:

  1. 在继续操作前,请确保已在DNS服务商处为Jellyfin设置好CNAME记录

  2. 确定您希望存储Let's Encrypt相关数据的位置(Docker中称为"volumes"存储卷)

  3. 确定要用于Let's Encrypt的子域名或子路径(例如 jellyfin.example.com)

  4. 选择您要使用的时区

  5. 确定将使用HTTP-01还是DNS-01验证方式

  6. 确定容器运行的网络(推荐使用默认的macvlan网络"br0")

  7. 指定容器运行时使用的IP地址

  8. 确定要使用的端口映射(例如180映射到80端口,1443映射到443端口)

  9. 确保路由器已将80端口(如果使用http验证)和443端口转发至Docker容器(不同品牌路由器设置方法不同)

  10. 确定容器运行用户身份(可通过运行id命令获取PUID和PGID,将"user"替换为实际运行容器的用户名)

如果使用DNS-01验证方式,可查阅Certbot文档中的DNS插件列表获取支持情况。

根据以上设置,您需要相应调整以下配置参数。

以LinuxServer团队的Swag Docker容器创建命令为例:

docker create \
--name=swag \
--cap-add=NET_ADMIN \
-e PUID=1000 \
-e PGID=1000 \
-e TZ=Europe/London \
-e URL=example.com \
-e SUBDOMAINS=www, \
-e VALIDATION=http \
-e DNSPLUGIN=cloudflare `#optional` \
-e DUCKDNSTOKEN=<token> `#optional` \
-e EMAIL=<e-mail> `#optional` \
-e DHLEVEL=2048 `#optional` \
-e ONLY_SUBDOMAINS=false `#optional` \
-e EXTRA_DOMAINS=<extradomains> `#optional` \
-e STAGING=false `#optional` \
-p 443:443 \
-p 80:80 `#optional` \
-v </path/to/appdata/config>:/config \
--restart unless-stopped \
linuxserver/swag

假设我按照此模板并根据我的区域、端口和路径进行调整,配置示例如下(个人敏感信息已脱敏):

docker create --name=swag --cap-add=NET_ADMIN -e PUID=1000 -e PGID=1000 -e TZ=America/Chicago -e URL=example.com -e SUBDOMAINS=jellyfin -e VALIDATION=http -e EMAIL=email@email.com -e DHLEVEL=2048 -e ONLY_SUBDOMAINS=false -e STAGING=false -p 443:443 -p 80:80 -v /path/to/appdata/swag/:/config --restart unless-stopped linuxserver/swag

此命令将拉取linuxserver/swag容器镜像,并根据指定变量创建容器。随后使用docker start swag启动容器,可通过docker ps命令验证是否成功运行,输出结果类似:

CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS                                      NAMES
09346434b8ea linuxserver/swag "/init" 2 minutes ago Up 5 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp swag

此时,访问您指定的存储卷路径(本例中为/mnt/swag),进入该目录下的nginx/proxy-confs子目录。列出目录内容将看到多个配置文件。

Jellyfin需要的是jellyfin.subdomain.conf.sample(子域名方式)或jellyfin.subfolder.conf.sample(子路径方式)。复制所需文件并移除.sample后缀(例如cp jellyfin.subdomain.conf.sample jellyfin.subdomain.conf),用文本编辑器打开该文件。

文件内容应类似如下(以jellyfin.subdomain.conf为例,jellyfin.subfolder.conf也基本相同):

server {
listen 443 ssl;
listen [::]:443 ssl;

server_name jellyfin.*;

include /config/nginx/ssl.conf;

client_max_body_size 0;

location / {
include /config/nginx/proxy.conf;
resolver 127.0.0.11 valid=30s;
set $upstream_app jellyfin;
set $upstream_port 8096;
set $upstream_proto http;
proxy_pass $upstream_proto://$upstream_app:$upstream_port;

proxy_set_header Range $http_range;
proxy_set_header If-Range $http_if_range;
}

location ~ (/jellyfin)?/socket/ {
include /config/nginx/proxy.conf;
resolver 127.0.0.11 valid=30s;
set $upstream_app jellyfin;
set $upstream_port 8096;
set $upstream_proto http;
proxy_pass $upstream_proto://$upstream_app:$upstream_port;

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
}
}

关键配置行是set $upstream_app jellyfin。如果Jellyfin和Let's Encrypt容器在同一Docker网络内,通常会自动识别并处理反向代理。若未生效,需将此处的jellyfin改为您Jellyfin服务器的实际IP。同时修改location ~ (/jellyfin)?/socket配置行,在socket后添加斜杠,即改为location ~ (/jellyfin)?/socket/

接着在Jellyfin设置中(控制台→网络),滚动至"公共HTTP端口号"和"公共HTTPS端口号",确保HTTP端口设为8096,HTTPS端口设为8920。

运行docker restart swag重启Let's Encrypt容器,通过docker logs -f swag查看日志。若一切正常,日志末尾将显示Server Ready,表明Let's Encrypt已正常运行。