使用Docker进行多站点配置

:warning: While multisite is supported in the Discourse application, this is an advanced sysadmin setup. If you don’t know what you’re doing, do not set up multisite. The Discourse team is unable to provide multisite configuration support.

If you wish to host multiple domains on a singled Docker setup, you’ll need a multisite configuration. Here are the basic building blocks for one.

Understand hooks

Multisite is a fairly advanced topic. Before attempting a multisite build, spend some time to learn about them.

Discourse templates use pups; its rules are simple and powerful.

Each rule you run may define a hook:

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

Later on in your container you can insert rules before or after a hook:

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

So in the example above you will see output like the following:

I ran before
1
I ran after

You can read through the templates in /var/discourse/templates to see what hooks you have available.

Amend your standalone container to provision the second site tenant

Replace the entire hooks section with:

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 -E -u discourse bundle exec rake multisite:migrate

There are 3 hooks in play:

  1. after_postgres ensures that after postgres is installed an additional db called b_discourse is created with the appropriate permissions.

  2. before_bundle_exec, ensures docker_manager is in place and that the multisite.yml file is in place (which defines where to find the databases)

  3. after_bundle_exec, runs the custom db migration task rake multisite:migrate this ensures all the dbs are up to date.

Note on configuration

The above sample can be split into data container / app container if needed. Just run the after_postgres hook in the data container and the rest in web container.

The above sample can be extended to provision even more DBs. To do so, provision more DBs by duplicating the create db etc calls, and make sure you add additional sites in multisite.yml.

Make sure you amend the host_names node in multisite.yml to match the actual host name you wish to host.

Also, if you plan to run HTTPS, you will need a proxy in front of the site to handle it as the built in letsencrypt functionality will not work in a multisite scenario.

Last edited by @JammyDodger 2024-05-26T20:56:50Z

Check documentPerform check on document:
68 个赞

我想知道上面建议配置中的 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 文件,以便在将来的任何重建或升级时,多站点配置都能保持同步。

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

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

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

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

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

1 个赞

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

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

1 个赞