Setting up a multi-site architecture can sound overwhelming and scary😱 with all the database setup and creating vhost for each site... it can become painful and daunting. But here is how you can setup a drupal 9 multi-site with single database using prefixes within 15 min(30 mins tops I swear!😬). So let's dive into it!
Pre-requisites:
Steps:
1. Start by downloading codebase for drupal9 by
composer create drupal/recommended-project
Inside your project folder run:
lando init   --source cwd   --recipe drupal9   --webroot .   --name <project-name>
 lando composer require drush/drush
  
  
  2. Edit .lando.yml with following (example) content:
name: <project-name>
recipe: drupal9
config:
  webroot: web
  drush: ^10
  xdebug: true
  via: nginx
services:
  database:
    type: mariadb:10.5
proxy:
  appserver_nginx:
    - subsite1.lndo.site
    - subsite2.lndo.site
    - subsite3.lndo.site
Note: Replace  with your project name.
The important thing to see here is proxy: appserver_nginx key which lists  the URLs for our default and subsites which is equivalent to setting up vhost for each site, you may also need to add an entry for your default site like - <project-name>.lndo.site
  
  
  3. Create folders for subsites in your sites directory
   mkdir subsite{1..3}
  
  
  4. Copy examples.sites.php to sites.php and add entries for subsites  in your sites.php file:
   $sites['subsite1.lndo.site'] = 'subsite1';
   $sites['subsite2.lndo.site'] = 'subsite2';
   $sites['subsite3.lndo.site'] = 'subsite3';
5. Copy settings.php to your default and subsites directories.
  
  
  6. Run lando rebuildso it creates urls for our subsites.
7. Go to the respective URL on browser and install the default site first.
By default lando creates database with name drupal9 and username as password as drupal9 too and host as database so use these while installing the site:

Tip: you can quickly find info about configuration using lando info
Since this is default site we will leave the prefix empty. 
Proceed and install the site.
8. Installing subsites
Go the URL for subsite, repeat step7, except this time also add the value in prefix field:

Proceed and install the site.
Note: Install site using browser instead of drush because drush will ask to drop and recreate the database for each site.
And We are all done!!!
In case you want to add more sites just repeat 2-6 and then 8 for your new site. And get it up and running in no time.
Remember we are doing everything here inside a docker container so if you want to check your databases for example, you will need to use lando mysql instead of just mysql and also the credentials will be the one which are created by lando rather than what you have on your system.
Let me know in the comments if you found this helpful! KThnxByee!👋🏼
 
 
    
Top comments (0)