Drupal Multisite with Probo



Search Results

You can add --site-[anything] to the end of any build’s URL – where [anything] can be anything URL safe with no periods – and Probo will still route to the appropriate container. For example, if your $BUILD_ID is “abc-123” and your normal URL is https://abc-123.probo.build, you can add --site-foo to the end of the URL. The new URL, https://abc-123--site-foo.probo.build, will still pass you through to the exact same build. This effectively gives you a way to create subdomains for your builds.

With this in mind, you can add the necessary site entries in your sites.php file to route the appropriate Probo URLs to your site folders. For example, you might want to route $BUILD_ID--site-de.probo.build to your de site folder. For a build with the ID “abc-123”, the resulting entry in sites.php would look like:

1
$sites['abc-123--site-foo.probo.build'] = 'foo';

Since the build ID is different for every build, you append these entries to sites.php at build time in your .probo.yaml file, where the $BUILD_ID environment variable is available:

1
2
3
4
5
6
steps:
  - name: Configure Multisite
    plugin: Script
    script: |
      cd /var/www/html/sites
      echo "\$sites['$BUILD_ID--site-foo.probo.build'] = 'foo';" >> sites.php

Site Settings Files

Each site in your multisite needs its own settings.php file in its site folder. See the Drupal settings file documentation for how to create these.

Within each site’s settings.php file, add a condition that checks for a Probo.CI environment and assigns that site’s database credentials. Probo build containers set the PROBO_ENVIRONMENT environment variable to the string TRUE. Note that Drupal reads each site’s connection from $databases['default']['default'], so each site’s settings.php file points that key at its own database:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (getenv('PROBO_ENVIRONMENT') === 'TRUE') {
  $databases['default']['default'] = [
    'database' => 'accounts', // This site's database, e.g. lms for the LMS site.
    'username' => 'root',
    'password' => 'strongpassword',
    'prefix' => '',
    'host' => 'localhost',
    'port' => '3306',
    'namespace' => 'Drupal\Core\Database\Driver\mysql',
    'driver' => 'mysql',
  ];

  $settings['trusted_host_patterns'] = [
    '^.+\.probo\.build$',
  ];
}

If your sites define trusted_host_patterns, the *.probo.build hostnames will be rejected with a “provided host name is not valid” error unless you allow them. Including the pattern above inside the Probo condition keeps your production patterns untouched.

Configuring Databases for Multisites

In most use cases, each Drupal site in a multisite will have its own SQL database. By default, Probo.CI will only import one database as part of the Drupal Plugin. This means you will need to compensate for this in other steps of your .probo.yaml file.

In a hypothetical example, we have three multisites. A main web site, a second site for a learning management system (Opigno) and a third site to manage accounts for the other two - essentially a Single Sign On.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
type: lamp
php: 8.4
database: mariadb:11.4

assets:
  - main.sql.gz
  - lms.sql.gz
  - accounts.sql.gz

steps:
  - name: Install Our Drupal Site
    plugin: Drupal
    database: main.sql.gz
    databaseGzipped: true
    databaseUpdates: true
    clearCaches: false
    composer: true
    subDirectory: docroot
    drupalVersion: 11
    configSyncDirectory: /src/config/main/sync
    dbOptions:
      key_buffer_size: 16M
      max_allowed_packet: 128M
    phpIniOptions:
      memory_limit: 512M

  - name: Import LMS Database
    plugin: Script
    script: |
      gunzip $ASSET_DIR/lms.sql.gz
      mysql -uroot -pstrongpassword -e "CREATE DATABASE lms;"
      mysql -uroot -pstrongpassword lms < $ASSET_DIR/lms.sql
      # Be sure to do housekeeping on your database assets
      rm $ASSET_DIR/lms.sql

  - name: Import Accounts Database
    plugin: Script
    script: |
      gunzip $ASSET_DIR/accounts.sql.gz
      mysql -uroot -pstrongpassword -e "CREATE DATABASE accounts;"
      mysql -uroot -pstrongpassword accounts < $ASSET_DIR/accounts.sql
      # Be sure to do housekeeping on your database assets
      rm $ASSET_DIR/accounts.sql

  - name: Configure Multisite
    plugin: Script
    script: |
      cd $SRC_DIR/docroot/sites
      echo "\$sites['$BUILD_ID--site-main.probo.build'] = 'default';" >> sites.php
      echo "\$sites['$BUILD_ID--site-lms.probo.build'] = 'lms';" >> sites.php
      echo "\$sites['$BUILD_ID--site-accounts.probo.build'] = 'accounts';" >> sites.php
      echo "\$sites['$BUILD_ID'] = 'default';" >> sites.php

Running Drush For Each Site

The Drupal plugin’s clearCaches, databaseUpdates and configImport options only run against the default site. Your secondary sites need their own database updates, configuration imports and cache rebuilds, which you can run in an additional step by passing each site’s URL to Drush with the -l option:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  - name: Update Secondary Sites
    plugin: Script
    script: |
      # Make sure each site has a writable public files directory.
      mkdir -p $SRC_DIR/docroot/sites/lms/files $SRC_DIR/docroot/sites/accounts/files
      chown -R www-data:www-data $SRC_DIR/docroot/sites/lms/files $SRC_DIR/docroot/sites/accounts/files

      drush -r /var/www/html -l $BUILD_ID--site-lms.probo.build updb -y
      drush -r /var/www/html -l $BUILD_ID--site-lms.probo.build cim -y
      drush -r /var/www/html -l $BUILD_ID--site-lms.probo.build cr

      drush -r /var/www/html -l $BUILD_ID--site-accounts.probo.build updb -y
      drush -r /var/www/html -l $BUILD_ID--site-accounts.probo.build cim -y
      drush -r /var/www/html -l $BUILD_ID--site-accounts.probo.build cr

The configSyncDirectory option on the Drupal plugin applies to the default site only. If your secondary sites import configuration, each site’s settings.php file needs to define its own $settings['config_sync_directory'] so the cim commands above find the correct configuration.

Viewing Your Sites

Once the build completes, each of your sites is available at its own URL:

  • Main site: https://$BUILD_ID.probo.build (also https://$BUILD_ID--site-main.probo.build)
  • LMS site: https://$BUILD_ID--site-lms.probo.build
  • Accounts site: https://$BUILD_ID--site-accounts.probo.build


Get Started with Probo.CI

Want to Try Probo today?
Get Started Fast!.

When you sign up for a Probo.CI registration code, you will get a two week trial of the plan of your choosing. For more information on how to get started, click here .

LEARN MORE ABOUT PROBO.CI'S PRICING