Configurazione multisito con 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 Mi Piace

Mi stavo chiedendo cosa fa il valore db_id: 2 nella configurazione suggerita sopra?

Inoltre, qual è la procedura per aggiungere un nuovo sito all’impostazione multisito esistente? È solo:

  • Aggiornare yaml
  • ./launcher bootstrap multisite [1]
  • ./launcher start multisite

Mi stavo chiedendo se ci fosse un modo per evitare di influire su tutti gli altri siti esistenti eseguendo un bootstrap o una ricostruzione completa e in qualche modo aggiungere solo il database aggiuntivo necessario?


  1. Ho provato prima ./launcher rebuild multisite, ma sembra fallire. ↩︎

Penso che l’ID del database non sia più necessario.

Puoi semplicemente modificare il file multisito con il nuovo sito ed eseguire

sv restart unicorn

E quindi migrare il database.

1 Mi Piace

Sei un salvavita!

Mi mancava questo comando per riavviare (ho anche provato sv reload unicorn ma in qualche modo non è bastato).

In termini di creazione del database, esiste già un modo per farlo automaticamente? Sarebbe bello avere rake db:create[mynewdbname]. Altrimenti, posso scrivere un breve script di shell.

Al momento, creo manualmente (creo il db e imposto i permessi) e poi eseguo RAILS_DB=newdb rake db:migrate per popolarlo.

E solo per verificare se sto facendo qualcosa di stupido:

  • Al momento, sto modificando il file multisite all’interno di docker (/var/www/discourse/config/multisite.yml)
  • Creando il db manualmente
  • Eseguendo rake db:migrate
  • sv restart unicorn
  • Devo anche modificare il file yaml al di fuori di docker in modo che la configurazione multisite sia sincronizzata in caso di future ricostruzioni o aggiornamenti.

Sto facendo stupidaggini modificando la configurazione multisite in 2 posti diversi quando c’è già un modo per modificarla in un posto e spingere le modifiche?

È possibile ripulire un po’ questa documentazione e fornire maggiori indicazioni? Innanzitutto, sono estremamente nuovo a Docker e, sebbene abbia esperienza con server Linux, non sono un professionista. Sono un vecchio schooler che ama i miei cron job e gli script di shell e cose strane come quelle con Apache multisite, ecc. Ma questo è un po’ più complesso e utilizza un linguaggio di programmazione diverso da quello a cui sono abituato… Forse anche un video dimostrativo di qualche tipo? Sarebbe bello se discourse fosse semplicemente pronto per il multisite e in quel modo tutto ciò che dovresti fare è modificare alcuni file, ecc.

Incolla quella strofa di testo e modificala a tuo piacimento.

Avrai quindi bisogno di un reverse proxy o di seguire Configura Let’s Encrypt con più domini/reindirizzamenti per ottenere certificati per tutti i domini.

Vedi anche Configurazione Multisito con Let’s Encrypt e senza Reverse Proxy. Ma questo deve essere aggiornato per includere un’altra riga richiesta che puoi ottenere dal link precedente. Avevo intenzione di aggiornarlo ma non ci sono riuscito.

1 Mi Piace

Se sto facendo il multisito, a cosa dovrei puntare il mio dominio? Allo stesso IP del server (che ospita un altro forum)?

Sì, punta tutti i domini allo stesso IP del server.
Configura un proxy inverso con il server Nginx o Caddy.
Caddy è il migliore in quanto fornisce certificati SSL automatici gratuiti a tutti i tuoi domini.

1 Mi Piace