Drupal 9 Multisite setup in Lando

The multisite feature in Drupal allows to effectively create and manage a fleet of websites where all the sites will share the same codebase while having separated databases, configurations, and assets. In this post, we will see how to setup a Drupal multisite setup locally using Lando with an assumption that you already have a master site setup using drupal9 recipe. 

Setup sites for multisite

The first thing we need to do is to configure the sites that we need. In this case, let's call the master site as the main site and the other 2 sites which we will create are mojave and yosemite

Step 1: Create the folders for the sites in web/sites/* directory
web/sites/mojave
web/sites/yosemite

Step 2: Copy the default.settings.php from web/sites/default to the multisite directories as settings.php

Step 3: Copy the default.sites.php to sites.php and add entries for each of the multisite to make the main site aware of the sites.


// @codingStandardsIgnoreFile
$sites['mojave.lndo.site'] = "mojave";
$sites['yosemite.lndo.site'] = "yosemite";

Add db services to Lando file

Now that our files are setup, we will need databases for each of the sites. This will be handled via Lando by update the .lando.yml file to add each database as new services.  

Note: We don't need to add the database service for the main site since it will use the default database service.


services:
  # The database service used for the `main` site.
  database:
    type: mariadb:10.4
    portforward: true
    creds:
      user: drupal
      password: drupal
      database: main
  # The database service used for the `mojave` site.
  mojave:
    type:  mariadb:10.4
    portforward: true
    creds:
      user: drupal
      password: drupal
      database: mojave
  # The database service used for the `yosemite` site.
  yosemite:
    type:  mariadb:10.4
    portforward: true
    creds:
      user: drupal
      password: drupal
      database: yosemite

Add proxy for site domain

Lando uses Traefix as a reverse proxy to make the routing and have clean URLs to  point various ports inside of various services. So, by default if the app name is set to drupal, we will get a nice URL like http://drupal.lndo.site

Now, to add the URLs for the other two sites we need update the .lando.yml file to make an proxy entry for each sites. Alternatively, you can update the default URL for the main site as well.

Note: The proxy URL must match the domain entered in the sites.php file.


proxy:
  appserver:
    - main.lndo.site
    - mojave.lndo.site
    - yosemite.lndo.site

Install the sites via UI

Now that the multisites are configured, we can start installing Drupal on each of the site. Below is the table to map the domain and database credentials for each site.

SiteURLDB UsernameDB PasswordDB Name
Mainhttp://main.lndo.sitedrupaldrupalmain
Mojavehttp://mojave.lndo.sitedrupaldrupalmojave
Yosemitehttp://yosemite.lndo.sitedrupaldrupalyosemite

If, the files directory is not created automatically then you will need to create one for each site and give the appropiate permissions for the directory.

Install the sites via Drush Aliases

Although, the sites are accesible via UI and browser, Drush access and setup is also important for development and deployment purposes. For this, we will need to setup the Drush aliases for each site so that we can access the sites right from the terminal.

Setup Drush aliases

To setup the Drush aliases, create a *.sites.yml file in the PROJECTROOT/drush/sites/ directoryIn our case, we will create a file called drupal.sites.yml with the following content.


# The uri key should map to the proxy domain names 
# being setup in the .lando.yml file.
main:
 root: /app
 uri: http://drupal.lndo.site
mojave:
 root: /app
 uri: http://mojave.lndo.site
yosemite:
 root: /app
 uri: http://yosemite.lndo.site

Once, this is is setup you can access the sites using Drush aliases. To check the aliases list run lando drush sa whcih should give


'@drupal.main':
  root: /app
  uri: 'http://drupal.lndo.site'
'@drupal.mojave':
  root: /app
  uri: 'http://mojave.lndo.site'
'@drupal.yosemite':
  root: /app
  uri: 'http://yosemite.lndo.site'

So, to access the "mojave"  site you can run lando drush @drupal.mojave status

Site Installation

Now, that the Drush aliases are setup we can install the sites using the Drush site-install command and passing the sites directory name as the option.


# To install the main site
drush @drupal.main si --sites-subdir=default -y

# To install the mojave site
drush @drupal.mojave si --sites-subdir=mojave -y

# To install the yosemite site
drush @drupal.yosemite si --sites-subdir=yosemite -y

Verify site settings & configuration

Once, the sites are installed & setup we can verify if the configurations and data is isolated for each site. To check enable different themes for each site, enable different modules and/or create few dummy contents for each site. You will notice that each of the sites configurations are not isolated. However, we would also like to use Drupal Configuration management for each site but that can be a separate post by itself.

Tl;dr

To add a new multisite

  1. Create a new directory under web/sites by the name of the site
  2. Copy the default.settings.php from web/sites/default to the multisite directories as settings.php
  3. Update sites.php to make an entry for the new site.
  4. Add a new service to Lando file for the new site database
  5. Add a new proxy for the new site in the Lando file
  6. Run lando rebuild to create a the new database and update proxy
  7. Add new entry in the *.sites.yml for Drush alias
  8. Install the site either by UI or using Drush alias.
malabyaSat, 05/01/2021 - 20:30Drupal development