Install Drupal 9 Multi-site in subdirectories

Install Drupal 9 Multi-site in subdirectories

Step 1: Instantiate the master site

1.1: Create a database for the multisite root site, ex: drupal-dev.

1.2: Create Drupal 9 project via Composer.

cd /var/www
composer create-project drupal/recommended-project drupal_root_dir

1.3: Add Drush in your Drupal 9 project.

cd /var/www/drupal_root_dir
composer require drush/drush

1.4: Install Drupal 9 master site using the Drush command line

cd /var/www/drupal_root_dir/web
drush site:install

or

cd /var/www/drupal_root_dir/web
drush si

1.5: Create a virtual host definition for the master site.

# File: /etc/apache2/sites-available/drupal_multisite.conf

<VirtualHost *:80>

  # virtual host configuration for Drupal 8 multisite

  ServerAdmin [email protected]
  DocumentRoot /var/www/drupal_root_dir/web
  ServerName example.com
  ServerAlias www.example.com
    
  <Directory /var/www/drupal_root_dir/web>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/drupal_error.log
  LogLevel warn
  CustomLog ${APACHE_LOG_DIR}/drupal_access.log combined

</VirtualHost>

1.6: Browse the website to make sure all settings are correct.

Step 2: Configure the first site for the multisite

2.1: Create a database for the multisite site 1, ex: drupal-dev-site-1.

2.2: Install Drupal 9 multisite 1 via Drush command line

cd /var/www/drupal_root_dir/web
drush si --sites-subdir=site1

2.3: Create symlink to multisite 1

cd /var/www/drupal_root_dir/web
ln -s . default
ln -s . site1

2.4: Make a copy of /var/www/drupal_root_dir/web/sites/example.sites.php called /var/www/drupal_root_dir/web/sites/sites.php

2.5: Edit sites.php, so the end of the file looks like this:

$sites = [
  'localhost' => 'default',
  'localhost.site1' => 'site1',
];

2.6: Create drush site alias via  /var/www/drupal_root_dir/drush/sites/self.site.yml , so that you can use  drush per site like drush @alias command

# File: self.site.yml
default:
  root: /var/www/drupal_root_dir/web
  uri: http://localhost
site1:
  root: /var/www/drupal_root_dir/web
  uri: http://localhost/site1

2.7: Run drush sa command to make sure site alias.

2.8: To avoid symlink infinite, add the following to the /var/www/drupal root dir/web/.htaccesss file.

# ref. https://www.drupal.org/docs/7/multisite-drupal/multi-site-in-subdirectories#s-avoid-infinite-symlink-recursion
  RedirectMatch 301 /(default|site1)/(default|site1)/(.*) /$1/$3

Step 3: Import default configs (optional)

cd /var/www/drupal_root_dir/web
drush @self.site1 config-set "system.site" uuid 64cf0233-39b3-4bf2-bdae-f6e9eaf8a5e5 -y
drush @self.site1 entity:delete shortcut_set -y
drush @self.site1 cim --source=../web/sites/default/config -y

For your reference:

Not so useful, but ... just note.

Y Cheung
Blogger, Programer & Traveler.
Shanghai