Skip to main content

Using Lando to develop a Drupal Multisite

Member for

2 years 7 months
Submitted by admin on

The Lando development tool allows a developer to remove the unneeded boilerplate and distractions in creating and managing a personal local web development environment. Because Lando is extendable and standardised it's sponsored and created by companies and frameworks this relationship lowers technical debt and the need to manage another codebase by these businesses. The Lando teams moto is accurate:

Push-button development environments hosted on your computer or in the cloud. Automate your developer workflow and share it with your team.

As a docker base technology, it has installations for all major operating systems it is easy to see why development teams would standardise their tools and methodologies to create applications for everyone on the Internet to appreciate. 

Large companies have long seen the advantage of a Drupal Multisite and the business objectives that created an amazing digital experience for a customer and having the ability to lower their COGs to implement these changes because of the use of a single codebase. However, developers' local environments have been over the years cumbersome to maintain and work with. Tools like Lando and DDev (a similar technology) abstract all the unneeded local management.

Setting up a .lando.yml and Drupal on your local dev environment

After installing Lando you will need to first create domains that correspond with the business requirements. Locally start by adding the chosen domains to your `/etc/hosts` (linuxize.com has a page on how to edit the host file).  Our team find using the same base domain of the company we are developing ending it with the .local TLD and only a change to the subdomain works in most cases here is an example below. The IP 127.0.0.1 address at the start of each line is essential it tells a browser to look locally for the `thebusiness.local` domains.

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal

# End of section
# Multisite domains to be used.
127.0.0.1 surfing.thebusiness.local
127.0.0.1 cats.thebusiness.local
127.0.0.1 water.thebusiness.local

Setup up your project correctly is important. From the start having data separation for the Git repo will reduce the likelihood of committing incorrect items. Below is a suggestion for a directory structure that we have found mitigates a lot of the issues you may face

thebusiness_local
 |
 |_build
     |
     |_ .lando.yml
     |
     |_ Start of Drupal code
         |
         |_ web directory

To create its structure using a UNIX-based system navigate to your project directory on your local machine using the command line (CLI) and run the following commands this will create the directory that will hold all the data regarding this project on your local machine. 

~$ mkdir thebusiness_local
~$ cd thebusiness_local/

Browse to the release page on Drupal.org and click on the latest link at the top of the page. The release page will indicate how to install the version using composer which will need to be installed on your local machine. The version I currently see is Drupal 10.

composer create-project drupal/recommended-project:10.0.0 

Now return back to the CLI and type the following commands into your thebusiness_local directory this will download Drupal and install it into the build directory that was discussed earlier. The `ls -h` commands should enable you to see the composer.json and other directories.

~$ composer create-project drupal/recommended-project:10.0.0 build
~$ cd build
~$ ls -h
   composer.json	
   composer.lock	
   vendor		
   web

The file system for your application is now set up the next step is to initialise Lando this can be done by `lando init` inside the build directory. When you run the command choose the following options.

  • current working directory
  • drupal10
  • Where is your webroot relative to the init destination?./web
  • What do you want to call this app? <anything you want>

If you run an ls -al in the build directory you should see the following output.

ls -al
total 336
drwxr-xr-x   9 staff  staff     288 Dec 22 08:53 .
drwxr-xr-x   3 staff  staff      96 Dec 22 08:21 ..
-rw-r--r--   1 staff  staff     357 Dec 22 08:21 .editorconfig
-rw-r--r--   1 staff  staff    4034 Dec 22 08:21 .gitattributes
-rw-r--r--   1 staff  staff      61 Dec 22 08:53 .lando.yml
-rw-r--r--   1 staff  staff    3437 Dec 16 04:27 composer.json
-rw-r--r--   1 staff  staff  154747 Dec 16 04:27 composer.lock
drwxr-xr-x  16 staff  staff     512 Dec 22 08:21 vendor
drwxr-xr-x  20 staff  staff     640 Dec 22 08:21 web

If you see .lando.yml this file controls all the Docker configurations needed for the Lando on your project. There are many options available to a developer. However, for this discussion, the standard implementation is all that is needed to create a Multisite. Firstly, we will install default Drupal and then extend Lando and Drupal into a Multisite. Run the following command to start the Lando application. 

  • lando start

You should see output from the terminal like the following. Then copy one of the APP SERVER URLs into your browser if Lando is working correctly it will display the Drupal installation page as the example underneath attests to from my test.

 NAME            my-lando-app
 LOCATION        /Users/<redacted>/Temp/thebusiness_local/build
 SERVICES        appserver, database
 APPSERVER URLS  https://localhost:64425
                 http://localhost:64426
                 http://my-lando-app.lndo.site:8000/
                 https://my-lando-app.lndo.site/

Drupal 10 install image

Installation of Drupal can be completed in several ways. Because we are going to create a Multisite most of the installation will be completed through the CLI.  Navigate to the following directory in the terminal `/thebusiness_local/build/web/sites/default`. By running the `ls` command you should see default.settings.php. If so run the following commands.

  • cp default.settings.php settings.php
  • mkdir files
  • nano settings.php

This will open a terminal editor called nano scroll to the bottom and add the following information and save the file. This creates a database connection. Once you have saved the file go back to the browser and follow the installation prompts to install Drupal. Make sure you change the hash_salt string in my example below.

$site_environment = 'database';

// Multisite configuration path the variable in the string
// ensures configuration between sites are separated.
$settings['config_sync_directory'] = "../config/default/sync";

// Getting Lando json object and converting it into an array.
$lando = json_decode(getenv('LANDO_INFO'), TRUE);

// Testing if the Lando array is exist and is not empty.
if (!empty($lando)) {

   // Setting hash for install for Lando
   $settings['hash_salt'] = '<choose 32 numbers or letters>'; 

   // Database connection with using the Lando array. The benefit
   // by using the array to connect a developer never needs to
   // remember the settings it all managed by Lando.
   $databases['default']['default'] = [
     'database' => $lando[$site_environment]['creds']['database'],
     'username' => $lando[$site_environment]['creds']['user'],
     'password' => $lando[$site_environment]['creds']['password'],
     'host' => $site_environment,
     'port' => '3306',
     'namespace' => 'Drupal\\mysql\\Driver\\Database\\mysql',
     'driver' => 'mysql',
     'autoload' => 'core/modules/mysql/src/Driver/Database/mysql/',
   ];

   // Define tmp directory to be used by Drupal.
   $conf['file_temporary_path'] = '/tmp';

   // Private file directory settings that is separated
   // by site environment variable.
   $settings['file_private_path'] = "/app/private-files/$site_environment";
}
 else {
  // Setup for the Multi websites site live hosting environment.
  switch ($environment) {
    case 'prod':
      break;
    case 'test':
      break;
    default :
     break;
  }
}

Now the foundational "default website" has been created with the ratification of the installation being functional through in Browser. The challenge to extend Drupal into a Multisite using Lando will be the focus of this guide.

Enabling Drupal to work as Multisite