使用 Docker 进行多站点配置

:warning: 虽然 Discourse 应用程序支持多站点,但这是一种高级系统管理员设置。如果您不知道自己在做什么,请不要设置多站点。Discourse 团队无法提供多站点配置支持。

如果您希望在单个 Docker 设置上托管多个域,则需要多站点配置。以下是其基本组成部分。

理解钩子 (hooks)

多站点是一个相当高级的主题。在尝试进行多站点构建之前,花些时间了解它们。

Discourse 模板使用 pups;其规则简单而强大。

您运行的每条规则都可能定义一个钩子:

run:
  exec:
    cd: some/path
    hook: my_hook
    cmd:
      - echo 1

稍后在您的容器中,您可以插入规则到钩子之前或之后:

hooks:
  before_my_hook:
    - exec: echo "I ran before"
  after_my_hook:
     - exec: echo "I ran after"

因此,在上面的示例中,您将看到如下输出:

I ran before
1
I ran after

您可以通读 /var/discourse/templates 中的模板,以查看有哪些钩子可用。

修改您的独立容器以配置第二个站点租户

整个 hooks 部分替换为:

hooks:
  after_postgres:
     - exec: sudo -u postgres createdb b_discourse || exit 0
     - exec:
          stdin: |
            grant all privileges on database b_discourse to discourse;
          cmd: sudo -u postgres psql b_discourse
          raise_on_fail: false

     - exec: /bin/bash -c 'sudo -u postgres psql b_discourse <<< "alter schema public owner to discourse;"'
     - exec: /bin/bash -c 'sudo -u postgres psql b_discourse <<< "create extension if not exists hstore;"'
     - exec: /bin/bash -c 'sudo -u postgres psql b_discourse <<< "create extension if not exists pg_trgm;"'

  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - mkdir -p plugins
          - git clone https://github.com/discourse/docker_manager.git
  before_bundle_exec:
    - file:
        path: $home/config/multisite.yml
        contents: |
         secondsite:
           adapter: postgresql
           database: b_discourse
           pool: 25
           timeout: 5000
           db_id: 2
           host_names:
             - b.discourse.example.com

  after_bundle_exec:
    - exec: cd /var/www/discourse && sudo -H -E -u discourse bundle exec rake multisite:migrate

这里涉及 3 个钩子:

  1. after_postgres 确保在安装 postgres 之后创建一个名为 b_discourse 的附加数据库,并具有适当的权限。

  2. before_bundle_exec 确保 docker_manager 已就位,并且 multisite.yml 文件已就位(该文件定义了在哪里可以找到数据库)。

  3. after_bundle_exec 运行自定义数据库迁移任务 rake multisite:migrate,这确保了所有数据库都是最新的。

关于配置的注意事项

上述示例可以根据需要拆分为数据容器/应用容器。只需在数据容器中运行 after_postgres 钩子,而在Web 容器中运行其余部分。

上述示例可以扩展以配置更多数据库。为此,请通过复制创建数据库等调用来配置更多数据库,并确保在 multisite.yml 中添加其他站点。

务必修改 multisite.yml 中的 host_names 节点,以匹配您希望托管的实际主机名。

此外,如果您计划运行 HTTPS,则需要在站点前面有一个代理来处理它,因为内置的 letsencrypt 功能在多站点场景中将无法工作。

69 个赞
How does multisite.yml look?
Multiple Forums in a Single Installation
How to create 1000 subcategories in a scalable way?
Multiple Discourses, multiple containers, one server
Pros and cons of multiple standalone containers vs one for multiple Discourse installs?
Success - New Multisite Install on Dedicated server using ServerPilot, Nginx and Apache
Multisite Installation
Will I need to start over with multisite configuration from standalone version?
Starting a second Discourse forum on the same VPS
Multiple Discourse Installs - Single Server
Pros and cons of a multisite installation
Installation wizard not appearing in multisite installs
How do you set up multiple discourse forums on the same server?
Disabling/restricting user search
Multisite installation with seperated smtp emails
Sandbox and test discourse on host?
Second discourse instance on the same server
Micro Forums: how many could I create?
Discourse for 3 different Countrys
Need proper documentation for multisite discourse with docker
Discourse multisite installation help with digitalocean
Help me setup many websites on one VPS
Remove sites from multisite config with Docker
Totally Walled-Off Groups
Multitenat Dockerize Discourse
Not receiving activation email for admin on multisite installation
Need help with multisite configuration
Install Discourse on Plesk / Ubuntu 14 without Docker
How could I install another discourse on same server (docker)?
Multisite viability - 2 read-only and 1 active
Can I log into multiple instances of discourse simultaneously?
Is it possible to totally hide members
I want to host hundreds of instances of Discourse
Adding an instance to multisite without rebuilding the container
How to setup host mapping on a multi-site discourse instance
Publishing multisite
Multiple communities on discourse?
Multisite Config
Three sites in one setup
Recover to one site from multisite
Use CloudPanel to manage multiple sites with Discourse
Does discourse hosting use docker for all customer instances?
Tips for setting up a multi-subdomain Discourse instance with shared SSO?
Move from standalone container to separate web and data containers
Move from standalone container to separate web and data containers
Guidance on multi-site setup
Running multiple things on the same sever as my Pi (where Discourse is hosted)?
How extensible can I make my installation in terms of sub domains and restricting membership to them?
Discourse instance stops running in every midnight
One server for 2 Discourse communities?
One server for 2 Discourse communities?
Multiple discourse installation on single droplet
Feature proposal: Customizations based on primary group membership, a.k.a. tenancy 'lite'
Stable branch assets won't compile
[mutlisite installation] pnpm permission error during rebuild
Stable branch assets won't compile
Pull request for Wikis?
PAID Require Discourse expert for extreme customization
Add path to cookie
App.yml shared volumes for a two website setup
Contabo experience with Plesk, Webmin or other server-admin setup?
Move from standalone container to separate web and data containers
Using a launcher built docker image in docker-compose
Two standalone instances on one server?
Best approach for test and production instance of discourse
Are there other workarounds to not having category moderation such as multiple instances of the board?
WP SSO + Discourse Multisite
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column categories.search_priority does not exist
Multisite installation ... in subdirectories?
Multiple discourse instances in a single server
Integration with .NET MVC application for a SaaS platform
Multisite for small collection of sites for a family?
Hosting Plan Suggestion for 500 Concurrent Online Users
Avatar, Site Logos, and Cert Errors
Database access issues after upgrade v3.5.2 -> v3.6.0.beta2
Move from standalone container to separate web and data containers
Can I have two domains pointing towards the same IP address without redirect?
Move from standalone container to separate web and data containers
Multiple container setup problems
Installing on Kubernetes
How might we better structure #howto?
How to set redis DB ID in Docker container?
How can I host more than 1 Discourse forum on 1 VPS?
How can Admin login while Read-only mode is enabled on multisite?
Setup Multisite Configuration with Let's Encrypt and no Reverse Proxy
Docker image update: Redis 6 and 25% smaller image size
Setup Multisite Configuration with Let's Encrypt and no Reverse Proxy
Pups::ExecError
Web-only - do I need a separate image for each container?
All users are visible in @mentions autocomplete list
Migrate from VPS to a Dedicated
Discourse Multisite – Nginx (Inside Docker) Only Serving Default Site
Two Discourse communities on the same VPS
How can I use different setting value for subdomain?
Will This Configuration Successfully Create a Second Discourse Instance (with Proxy)?
Pups::ExecError

我想知道上面建议配置中的 db_id: 2 值有什么作用?

另外,向现有的多站点设置添加新站点的过程是什么?仅仅是:

  • 更新 yaml
  • ./launcher bootstrap multisite [^1]
  • ./launcher start multisite

我想知道是否有办法避免通过进行完全引导或重建来影响所有其他现有站点,而是以某种方式仅添加所需的额外数据库?

[^1]:我首先尝试了 ./launcher rebuild multisite,但这似乎失败了

我认为数据库 ID 不再需要了。

您可以直接编辑多站点文件并添加新站点,然后执行

sv restart unicorn

然后迁移数据库。

1 个赞

您真是救命恩人!

我缺少这个重启命令(我也尝试过 sv reload unicorn,但不知何故这还不够)。

关于创建数据库,是否已经有自动执行的方法了?如果能有 rake db:create[mynewdbname] 就好了。否则,我可以编写一个简短的 shell 脚本。

目前,我手动创建(创建数据库并设置权限),然后执行 RAILS_DB=newdb rake db:migrate 来填充它。

另外,我只是想确认一下我是否在做一些愚蠢的事情:

  • 目前,我正在 Docker 中编辑多站点文件(/var/www/discourse/config/multisite.yml)
  • 手动创建数据库
  • 运行 rake db:migrate
  • sv restart unicorn
  • 然后我还必须在 Docker 之外编辑 yaml 文件,以便在将来的任何重建或升级时,多站点配置都能保持同步。

我是否在两个不同的地方编辑多站点配置很傻,而实际上有一个方法可以在一个地方编辑然后推送更改?

1 个赞

能否稍微整理一下这份文档,并在其中提供更多指导?首先,我对 Docker 非常陌生,虽然我有 Linux 服务器经验,但算不上是专家。我习惯于使用 cron 作业、shell 脚本以及 Apache 多站点等老式方法。但这个东西更复杂,而且使用的是我不太熟悉的编程语言。也许还可以提供一个演示视频?如果 Discourse 能够直接支持多站点功能就好了,那样的话只需要编辑几个文件就可以了。

2 个赞

只需粘贴该诗节文本并进行编辑即可。

然后,您需要一个反向代理,或者遵循使用多个域/重定向设置 Let’s Encrypt 来获取所有域的证书。

另请参阅使用 Let’s Encrypt 和无反向代理设置多站点配置。但需要更新该指南,添加另一行来自上一个链接的必需项。我一直想更新它,但一直没能做到。

1 个赞

如果我正在进行多站点设置,我的域名应该指向哪里?指向服务器的同一 IP(托管另一个论坛的服务器)吗?

是的,将所有域指向同一个服务器 IP。
使用 Nginx 或 Caddy 服务器设置反向代理。
Caddy 是最好的,因为它为您的所有域提供免费的自动 SSL 证书。

1 个赞

我按照本教程成功地使用 Docker 设置了多站点安装。我最近在尝试升级时遇到了一个错误,我相信这一行:

    - exec: cd /var/www/discourse & sudo -E -u discourse bundle exec rake multisite:migrate

应该编辑为:

    - exec: cd /var/www/discourse & sudo -H -E -u discourse bundle exec rake multisite:migrate

有人能确认/编辑本教程吗?

1 个赞

是的,我也遇到了这个问题。你需要进行此更改,否则你的引导会失败。

2 个赞

我热烈地确认,这个修正也是强制性的!

好的,已编辑,谢谢!

2 个赞