Set up a local Drupal multisite with Lando on Mac OS

Setting up a local Drupal multisite with Lando isn’t complex with the right docs, but it took me a little while to find all the right bits of configuration. I’ve centralized them here for posterity.

Building and operating local development environments that reflect your production environments can be tricky — especially as the landscape of tools, libraries, and automations continues to evolve. Recently we’ve been pretty satisfied with Lando for Drupal development. It’s well supported, has an active developer community, and can be configured for a variety of tricky use cases like emulating a Pantheon environment (that will have to be a different post) or setting up a Drupal multisite.

If you’re here you probably know what a Drupal multisite is, but just in case: Multiple separate websites, each with its own database and configuration, served from the same code base. There are a host of uses for multisites, but if you think about different websites for different store branches or maybe a variety of individual blog sites driven from the same or similar setup you’d be on the right track.

Installing Lando and Docker Desktop

First things first: install Docker and Lando. If you already have Lando installed on your machine you can skip ahead, but just FYI I’m using a particular version of Docker for this build. A lot of us here at Aten have been running Lando with Docker Desktop for Mac 4.6.1 — it’s possible this Drupal multisite setup will work with other versions but I’m sure it works with this one. Follow the link above to install Docker — and make sure you choose the right platform for your chipset, whether that’s Apple or Intel. You can check your chipset on your Mac by clicking Apple Icon > About this Mac.

Once you have Docker Desktop installed, you can install Lando. I used the DMG installer for this walkthrough. When downloading the installer make sure to choose the right file — again, depending on your Mac’s chipset. If you have a newer Mac with an Apple chip choose the latest lando-arm64-vx.x.x.dmg, for Intel chips choose the lando-x64-vx.x.x.dmg.

The Lando DMG installer is very straightforward, just follow the onscreen instructions but make sure to choose “Custom” installation type, and uncheck Docker from the list of packages to install. This way Lando will work with the particular 4.6.1 version we already downloaded and installed.

Initializing a Drupal project on Lando

For this demo I used Lando’s Quick Start for Drupal which just takes a couple of minutes to run. You’ll want to start in your projects directory then run the following commands. I called my app landomultidrupal so change that value as you see fit. The somewhat unwieldy terminal commands look like this (pulled directly from the link above):

# Initialize a drupal9 recipe using the latest Drupal 9 version
mkdir landomultidrupal \
  && cd landomultidrupal \
  && lando init \
    --source remote \
    --remote-url https://www.drupal.org/download-latest/tar.gz \
    --remote-options="--strip-components 1" \
    --recipe drupal9 \
    --webroot . \
    --name landomultidrupal
 
# Start it up
lando start
 
# Install a site local drush
lando composer require drush/drush
 
# Install drupal
lando drush site:install --db-url=mysql://drupal9:drupal9@database/drupal9 -y
 
# List information about this app
lando info

And just like that we’ve got a Drupal 9 site running on Lando. Now let’s turn it into a multisite. We’ve got three tiny steps to complete here: modifications to your .lando.yml file, your settings.php file(s) and your sites.php file.

Multisite setup in .lando.yml

Below is a barebones .lando.yml configuration for a Drupal 9 multisite setup with two sites which we’ll call multisite1 and multisite2. It’s likely (especially if you’re a Lando aficionado) that your lando file will have some other configurations — but below are the bare necessities for running a multisite. You may notice I’m specifying ports for the portforward values. Specifying ports simplifies using tools like TablePlus to browse your databases, although the more straightforward portforward: true should work as well, and is Lando’s recommended approach.

name: landomultidrupal
recipe: drupal9
proxy:
  appserver:
    - multisite1.lndo.site
    - multisite2.lndo.site
services:
  appserver:
    webroot: .
  multisite1:
    type: mysql:5.7
    portforward: 3307
  multisite2:
    type: mysql:5.7
    portforward: 3308

Drupal 9 multisite configuration in settings.php

The above lando file configuration sets us up with two sites: multisite1 and multisite2. Our multisite1 will live at /sites/default, and multisite2 will live at /sites/multisite2. Each of these sites will have their own files directories, and could also include their own modules or themes directories depending on the needs of the project. They also have their own settings.php file which is the important component for this post.

You’ll want to paste the following code blocks at the bottom of the existing settings.php files. For multisite2, you can copy and paste the original /sites/default/settings.php to get all the boilerplate stuff in there — then paste in the special Lando code below.

// PASTE THIS AT THE END OF /sites/default/settings.php
 
$lando_info = json_decode(getenv('LANDO_INFO'), TRUE);
if (!empty($lando_info)) {
 
  // Define this site's default database. Be sure to change ‘multisite1’ to the same identifier that
  // you use under ‘services’ in your .lando.yml file.
  $databases['default']['default'] = [
    'database' => $lando_info['multisite1']['creds']['database'],
    'username' => $lando_info['multisite1']['creds']['user'],
    'password' => $lando_info['multisite1']['creds']['password'],
    'host' => 'multisite1',
    'driver' => 'mysql',
  ];
 
  // Define file system settings.
  $conf['file_temporary_path'] = '/tmp';
  $settings['file_private_path'] = '/app/private-files/';
 
  // Trusted host pattern settings.
  $settings['trusted_host_patterns'][] = '\.lndo\.site$';
 
// Define this site's own config sync directory for local environment.
  $settings['config_sync_directory'] = '/app/config/multisite1'; 
}
// PASTE THIS AT THE END OF /sites/multisite2/settings.php
 
$lando_info = json_decode(getenv('LANDO_INFO'), TRUE);
if (!empty($lando_info)) {
 
  // Define this site's default database. Be sure to change ‘multisite2’ to the same identifier that
  // you use under ‘services’ for the second site in your .lando.yml file.
  $databases['default']['default'] = [
    'database' => $lando_info['multisite2']['creds']['database'],
    'username' => $lando_info['multisite2']['creds']['user'],
    'password' => $lando_info['multisite2']['creds']['password'],
    'host' => 'multisite2',
    'driver' => 'mysql',
  ];
 
  // Define file system settings.
  $conf['file_temporary_path'] = '/tmp';
  $settings['file_private_path'] = '/app/private-files/';
 
  // Trusted host pattern settings.
  $settings['trusted_host_patterns'][] = '\.lndo\.site$';
 
	// Define this site's own config sync directory for local environment.
  $settings['config_sync_directory'] = '/app/config/multisite2'; 
}

Set up sites.php

Next we need to indicate in sites.php which environment will point to which site configuration. We only need to define multisite2 here because by default, Drupal looks in /sites/default for the first site in a multisite installation. You can make a copy of /sites/example.sites.php to start with — although that file just includes some basic documentation and no actual code. The necessary code is just one line:

$sites['multisite2.lndo.site'] = 'multisite2';

Rebuild, and presto

That’s it! Make sure all your files are saved, then let Lando know to rebuild using the new configuration. From terminal navigate to your project directory and:

lando rebuild
lando start

Voila! You should now have multisite1.lndo.site and multisite2.lndo.site running on your local — and both should resolve to the Install Drupal 9 web interface. You can follow the steps in your browser as usual, just remember to give your sites different names!

Have a quicker or easier way to set up a Drupal multisite with Lando? Or some other considerations you’d like to contribute? Feel free to share in the comments below!

Drupal

Read This Next