使用 Let's Encrypt 设置 HTTPS 支持

:bookmark: 本指南介绍如何在已安装的 Discourse 上使用 Let’s Encrypt 启用 HTTPS。前提是您之前已完成安装,但尚未启用 HTTPS。

:person_raising_hand: 所需用户权限:系统管理员

:exclamation: 本指南仅适用于尚未启用 HTTPS 的现有安装。按照 官方安装指南 进行操作时,HTTPS 会默认自动启用。

您想免费为您的 Discourse 添加 HTTPS 支持吗?这要感谢我们的朋友 Let’s Encrypt

:bell: 您的网站上其他所有内容是否已准备好支持 HTTPS?

在开始之前,请注意:为了让 HTTPS 正常工作,页面上的 每一个资源 都必须兼容 HTTPS。请检查您的 CDN、社交登录、Logo 文件、任何第三方 JavaScript、图片、字体或 CSS —— 这些 全部 都必须可通过 HTTPS 访问!

注意:运行 ./discourse-setup 将启用 Let’s Encrypt。自 2017 年 3 月起,您可以再次运行该脚本,按几次回车并输入您的电子邮件地址;脚本将自动包含所需的模板并插入您的邮箱。除非您是精通系统的管理员且清楚为何不应这样做,否则您应该直接运行 discourse-setup,而无需继续阅读本指南。(如果您很久以前就安装了 Discourse,可能仍需要手动编辑 app.yml。)

注意:如果您的 Discourse 通过某些反向代理(例如 Cloudflare)访问,则此配置将无法生效。

使用 Let’s Encrypt 配置 HTTPS

1. 编辑 app.yml

访问您的 Discourse 配置文件:

cd /var/discourse
nano containers/app.yml
  • 添加以下模板:
    templates:
      - "templates/web.template.yml"
      - "templates/web.ssl.template.yml"
      - "templates/web.letsencrypt.ssl.template.yml"
    

:warning: Discourse 是您服务器上的唯一网站吗?

如果您因为在同一服务器的 80 端口托管其他网站而正在使用 web.socketed.template.yml,请 立即停止。您应该在宿主机系统上使用 Let’s Encrypt 客户端;否则验证将失败,因为当前使用的客户端无法绑定到必要的套接字。

2. 暴露 HTTPS 端口

确保以下端口已开放以支持 HTTPS 流量:

expose:
  - "80:80"
  - "443:443"

3. 添加 Let’s Encrypt 邮箱

插入用于接收 Let’s Encrypt 通知的电子邮件地址:

env:
  LETSENCRYPT_ACCOUNT_EMAIL: 'your-email@example.com'

4. 重建应用程序

通过重建容器来应用更改:

./launcher rebuild app

5. 验证 HTTPS

通过 https://yourdomain.com 访问您的网站。如果成功,您将看到网站已通过 HTTPS 加密保护。

检查您的资源:

  • 确保资源(如图片、脚本)通过 HTTPS 加载。
  • 根据需要重新配置社交登录和 CDN 以支持 HTTPS。
  • 解决浏览器控制台中关于不安全资源的任何警告。

在通过有效的 HTTPS 证书重建后,Discourse 会自动启用 force_https

它是如何工作的?

该模板使用了 https://github.com/acmesh-official/acme.sh,这是一个

最简单的 Let’s Encrypt 免费证书客户端 Shell 脚本

简单且功能强大,您只需 3 分钟即可学会。

完全用 Bash 编写,不依赖 Python、acme-tiny 或 Let’s Encrypt 官方客户端。只需一个脚本,即可自动签发和续期您的证书。

这可能是自动签发和续期 Let’s Encrypt 免费证书的最小、最简便且最智能的 Shell 脚本。

web.letsencrypt.ssl.template.yml 为您的容器添加了一个启动脚本,该脚本会:

  1. 在主 nginx 启动之前,启动一个轻量级 nginx 以在端口 80 上提供 ACME 挑战响应。
  2. 使用 webroot 模式(以 /var/www/discourse/public 为目录)签发 RSA(4096 位)和 ECDSA(ec-256)Let’s Encrypt 证书。
  3. 将证书安装到 nginx 期望的 /shared/ssl/ 目录。同时,它会设置一个 cron 任务以自动续期证书。这将自动续期您的证书。如果证书未过期,不会发生任何操作。如果证书确实过期,您将在设置时提供的电子邮件地址收到来自 Let’s Encrypt 的邮件通知。
  4. 如果获取到有效证书,则将 force_https 设置为 true。

故障排除

检查日志

如果 HTTPS 无法工作,请使用以下命令检查 SSL 或 Let’s Encrypt 相关的错误日志:

./launcher logs app

验证证书文件

确保证书和密钥文件已就位:

ls -l /var/discourse/shared/standalone/ssl

您应该看到如下文件:

  • yourdomain.com.cer (RSA)
  • yourdomain.com.key (RSA)
  • yourdomain.com_ecc.cer (ECDSA)
  • yourdomain.com_ecc.key (ECDSA)

手动续期证书

如果自动续期失败,您可以手动重新签发证书:

./launcher enter app
sv stop nginx
/usr/sbin/nginx -c /etc/nginx/letsencrypt.conf
LE_WORKING_DIR=/shared/letsencrypt DEBUG=1 /shared/letsencrypt/acme.sh --issue -d example.com -k 4096 -w /var/www/discourse/public
LE_WORKING_DIR=/shared/letsencrypt /shared/letsencrypt/acme.sh --installcert -d example.com --fullchainpath /shared/ssl/example.com.cer --keypath /shared/ssl/example.com.key --reloadcmd "sv reload nginx"
LE_WORKING_DIR=/shared/letsencrypt DEBUG=1 /shared/letsencrypt/acme.sh --issue -d example.com --keylength ec-256 -w /var/www/discourse/public
LE_WORKING_DIR=/shared/letsencrypt /shared/letsencrypt/acme.sh --installcert --ecc -d example.com --fullchainpath /shared/ssl/example.com_ecc.cer --keypath /shared/ssl/example.com_ecc.key --reloadcmd "sv reload nginx"
/usr/sbin/nginx -c /etc/nginx/letsencrypt.conf -s stop

使用干净证书重建

删除旧的证书文件并重新构建以从头开始:

rm -rf /var/discourse/shared/standalone/ssl
rm -rf /var/discourse/shared/standalone/letsencrypt
./launcher rebuild app

限制说明

Let’s Encrypt 证书仅验证域名和加密,不确认所有权或身份,这可能在某些浏览器中被标记。更多详情,请参阅 Let’s Encrypt 社区

143 个赞
Setting up Let's Encrypt for multisite
How should I enable letsencrypt while discourse is beside other websites
Cant setting SLL (Let's Encrypt) for Discourse
Add to homescreen banner on Android
How to Set Up SSL in Discourse
Setting up SSL with my domain name and Discourse instance
Issue installing on subdomain
Completely automated SSL certificate generation
German 1&1-hosting user experience?
Problem with my SSL certificate
Replacement for whitelist-iframe
Site throws a blank screen after trying to enable LetsEncrypt
Missing file(discourse.conf) when launching after lets encrypt update?
Missing file(discourse.conf) when launching after lets encrypt update?
My discourse has either been hacked or catfished?
Discourse has stopped opening
Using a certificate when Discourse is installed behind a reverse proxy
Can discourse be installed in private mode
My Forum Is showing "Privacy Error" after upgrading SSL certificate
Using a certificate when Discourse is installed behind a reverse proxy
Why my forum not pop-up "Add to Home screen"(PWA) automatically?
Why my forum not pop-up "Add to Home screen"(PWA) automatically?
HTTPS : issue while trying to set up SSL certification
HTTPS : issue while trying to set up SSL certification
SSL received a record that exceeded the maximum permissible length
Discourse site loads via IP but via domain only header
Why is the Apple Touch Icon loaded via HTTP instead of HTTPS?
Defaultish app won't rebuild
Why is the Apple Touch Icon loaded via HTTP instead of HTTPS?
Why is the Apple Touch Icon loaded via HTTP instead of HTTPS?
Defaultish app won't rebuild
Unable to connect Discourse and WordPress
I have a very difficult problem installing ssl - please help
Not able to access site after letsencrypt cert expiry and rebuild due to IPV6
Cannot connect to IP address and no errors in log
Http logo urls after enabling LetsEncrypt
How to install SSL certificate in Discourse
White blank page on mobile app
Cloud installation not working
My forum goes offline after removing https
Trying to use Let's Encrypt + Cloudflare
New user invite links only give ERR_SSL_PROTOCOL_ERROR
[DigitalOcean] hostname having "www" in A records showing blank page
Error at LetsEncrypt validation
Migrate a phpBB3 forum to Discourse
Set Up Let's Encrypt with Second Domain for Existing Discourse Install
Not starting up after rebuild
Port 443 of computer does not appear to be accessible
Make auto-linked URLs use HTTPS
Unable to change domain name
LetsEncrypt certificate not renewing
How to deny request from unauthorized domain pointing to my IP address?
No login is possible after recovery a Discourse Backup on a new server
Letsencrypt issued on every build?
My Discourse is Down. Certificate Issue?
Setup Let’s Encrypt + non-www > www
No connection accepted on http / https after fresh installation on Ubuntu 22.04 LTS
SSL certificate expired and after that - Error 404 Not Found
SSL didn't renew automatically and I can't manually renew it
443 address already in use? Letencrypt
Set up Let’s Encrypt with multiple domains / redirects
Using Discourse with Cloudflare: Best Practices
./launcher rebuild app error bootstrap failed with exit code 125
Set up Let’s Encrypt with multiple domains / redirects
Uncaught ReferenceError: Discourse is not defined due to Cloudflare Rocket Loader
Let's Encrypt SSL Certificate Not Renewing
Uploads paths have 80 port but protocol is https
Bootstrap error during Discourse install: ENOENT - /etc/runit/1.d/letsencrypt
How to install Discourse in the AWS EC2 Instances(Ubuntu Server LTS)?
Question about the email configuration
Discourse-saml: There was an error authorizing your account
Problem in installing Let's Encrypt SSL for www and non-www
Email not sending out after installation. I need help please
Discourse not starting up: nginx: unable to open supervise/ok: file does not exist
Failed to bootstrap: Failure with receiving network data
Error when installing ssl Let's Encrypt
How to adjust dependencies for https?
Clicking links is stuck in click tracking, shows ERR_FAILED
Minimum needed to get LetsEncrypt working on a GCE instance
Can we install discourse in another container distro?
Problem with my SSL certificate
Cant setting SLL (Let's Encrypt) for Discourse
[PAID] setup ssl - Let's encrypt
Error after moving from HTTP to HTTPS
How to renew Let's Encrypt?
ServiceWorker script evaluation failed due to HTTP (not HTTPS)
Defaultish app won't rebuild
Too many redirects after enabling https