如何在 Apache 虚拟主机中运行 Discourse

我已成功在我的服务器上安装了 Discourse,注册了第一个用户,并发布了第一个话题。因此,Discourse 方面的一切似乎都在正常运行。

然而,为了安装并运行 Discourse,我不得不禁用我的 Apache httpd 守护进程。这对我来说是个大问题,因为我基本上关闭了整个 Web 服务器,其中包括多个域名以及一个 Jitsi 会议服务器。

我的 Jitsi 安装作为单独的虚拟主机运行,使用我托管的某个网站的子域名。由于 Jitsi 能够与其他 Web 流量共享 443 端口,我希望也能以同样的方式让 Discourse 共享该端口。

是否有人拥有虚拟主机配置模板,可以在请求进入 Discourse 子域名时将流量转发给 Discourse?

2 个赞

是的,Meta 上有讨论将 Discourse 与其他网站一起运行的指南。

我相信如果你使用搜索功能,就能找到它们。

2 个赞

基本上已解决,但主页似乎需要很长时间才能完成请求,即使页面看起来已经完全加载。(我不确定这是否是正常行为。)

Discourse
我修改了 /var/discourse/containers/app.yml 中的 expose 块为:

expose:
  - "127.0.0.1:8000:80"   # http
  - "127.0.0.1:8443:443"  # https

这将流量从 localhost 转发到 Docker 容器。完成此操作后,需要使用 ./launcher rebuild app 重新构建。

Apache2 2.4.43 (Ubuntu)
我为子域添加了一个新的虚拟主机:discourse..com.conf,位于 sites-available 目录中,该配置将子域的流量转发到 Discourse Docker 实例监听的 localhost 端口。虚拟主机定义如下:

<VirtualHost *:80>
    ServerName discourse.<myDomain>.com
    Redirect permanent / https://discourse.<myDomain>.com/
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>

  ServerName discourse.<myDomain>.com

  SSLProtocol TLSv1 TLSv1.1 TLSv1.2
  SSLEngine on
  SSLProxyEngine on
  SSLCertificateFile /etc/letsencrypt/live/<myDomain>.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/<myDomain>.com/privkey.pem
  SSLCACertificateFile /etc/letsencrypt/live/<myDomain>.com/chain.pem
  SSLCipherSuite "EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA256:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EDH+aRSA+AESGCM:EDH+aRSA+SHA256:EDH+aRSA:EECDH:!aNULL:!eNULL:!MEDIUM:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS:!RC4:!SEED"
  SSLHonorCipherOrder on
  Header set Strict-Transport-Security "max-age=31536000"

  <Location />
    Order allow,deny
    Allow from all
    Require all granted
  </Location>

  ProxyPreserveHost on
  ProxyRequests Off
  RequestHeader set X-Forwarded-Proto expr=%{REQUEST_SCHEME}
  RequestHeader set X-Real-IP expr=%{REMOTE_ADDR}
  ProxyPass / https://127.0.0.1:8443/
  ProxyPassReverse / https://127.0.0.1:8443/

</VirtualHost>

由于我不常配置 Apache,可能存在一些不必要的行,或者可以添加或改进某些行。欢迎提供任何反馈。

1 个赞

你好 @Nap

很高兴看到你解决了问题。

我在多台服务器上运行 Discourse,前端使用 Apache2 作为反向代理连接到 Unix 套接字,配置运行良好(正如我们所知,它比 nginx 稍慢一些,而我在某些服务器上也运行 nginx,但它完全能正常工作。)

基本上,我的虚拟主机配置如下:

端口 80 配置

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName  discourse.mygreatwebsite.com
    DocumentRoot /website/discourse  # 通常不需要

    RewriteEngine On
    ProxyPreserveHost On
    ProxyRequests Off
    ErrorLog /var/log/apache2/discourse.error.log
    LogLevel warn
    CustomLog /var/log/apache2/discourse.access.log combined

    RewriteCond %{SERVER_NAME} =discourse.mygreatwebsite.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

端口 443 配置:

<VirtualHost *:443>
  ServerAdmin webmaster@localhost
  ServerName  discourse.mygreatwebsite.com
  DocumentRoot /website/nginx  # 在此配置中通常不需要
  
  SSLProxyEngine on
  RewriteEngine On
  ProxyPreserveHost On
  ProxyRequests Off
  RequestHeader set X-Forwarded-Proto expr=%{REQUEST_SCHEME}
  RequestHeader set X-Real-IP expr=%{REMOTE_ADDR}
  
  ProxyPass / unix:/var/discourse/shared/socket-only/discourse.http.sock|http://my.ip.address.here/
  ProxyPassReverse  / unix:/var/discourse/shared/socket-only/discourse.http.sock|http://my.ip.address.here/
  ErrorLog /var/log/apache2/discourse-ssl.error.log
  LogLevel warn
  CustomLog /var/log/apache2/discourse-ssl.access.log combined

  Include /etc/letsencrypt/options-ssl-apache.conf
  SSLCertificateFile /etc/letsencrypt/live/discourse.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/discourse.com/privkey.pem
</VirtualHost>

注意事项:

  1. 通常,我让 letsecrypt certbot 负责所有 SSL 证书设置、重定向等工作。

  2. 我们在 Apache2 配置文件中使用的 IP 地址是 Apache2 绑定的 IP 地址。

  3. 我们总是通过 Discourse 容器中的 Unix 套接字进行配置(暴露),在此配置中不暴露任何 TCP/IP 容器端口。

祝好,希望这能在最后阶段提供一些价值。

5 个赞

两个快速补充:

  1. 如果您想使用 Unix 套接字而不是直接代理,可以这样做(在您的 app.yml 中启用 web.socketed.template.yml 后):
  ProxyPreserveHost On
  ProxyRequests Off
  RequestHeader set X-Forwarded-Proto https
  RequestHeader set X-Real-IP expr=%{REMOTE_ADDR}
  <Location />
    ProxyPass unix:/var/discourse/shared/standalone/nginx.http.sock|http://localhost/
    ProxyPassReverse unix:/var/discourse/shared/standalone/nginx.http.sock|http://localhost/
  </Location>

请注意,如果您尝试让 Apache 像这样从 /var/discourse 读取数据,可能还需要处理 SELinux。类似 semanage fcontext -a -t httpd_sys_rw_content_t /var/discourse/shared/standalone/nginx.http.sock 后跟 restorecon /var/discourse/shared/standalone/nginx.http.sock 的命令应该可以解决此问题。

  1. Apache 拥有一个非常出色的 mod_md 模块,可以自动为您获取 Let’s Encrypt 证书。我强烈推荐它。
2 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.