Install Drupal 9 Multi-site in subdirectories

Posted by Y Cheung on Thu, Nov 3, 2022

Instantiate master site

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

1.2: Create Drupal 9 project via Composer.

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

1.3: Add Drush in your Drupal 9 project.

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

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

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

or

1cd /var/www/drupal_root_dir/web
2drush si

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

 1# File: /etc/apache2/sites-available/drupal_multisite.conf
 2
 3<VirtualHost *:80>
 4
 5  # virtual host configuration for Drupal 8 multisite
 6
 7  ServerAdmin [email protected]
 8  DocumentRoot /var/www/drupal_root_dir/web
 9  ServerName example.com
10  ServerAlias www.example.com
11    
12  <Directory /var/www/drupal_root_dir/web>
13    Options Indexes FollowSymLinks MultiViews
14    AllowOverride All
15    Order allow,deny
16    Allow from all
17  </Directory>
18
19  ErrorLog ${APACHE_LOG_DIR}/drupal_error.log
20  LogLevel warn
21  CustomLog ${APACHE_LOG_DIR}/drupal_access.log combined
22
23</VirtualHost>

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

Configure the first multisite site

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

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

2.3: Create symlink to multisite 1

1cd /var/www/drupal_root_dir/web
2ln -s . default
3ln -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:

1$sites = [
2  'localhost' => 'default',
3  'localhost.site1' => 'site1',
4];

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]

1# File: self.site.yml
2default:
3  root: /var/www/drupal_root_dir/web
4  uri: http://localhost
5site1:
6  root: /var/www/drupal_root_dir/web
7  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.

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

Step 3: Import default configs (optional)

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

References