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 迁移说明。