Setting up drupal9 multi-site with DDEV

shriaas2898

Aastha Shrivastava

Posted on September 22, 2021

Setting up drupal9 multi-site with DDEV

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. Make a new folder for your project and in your folder run:

 ddev config --project-type=drupal9 --docroot=web --create-docroot
 ddev composer create drupal/recommended-project
 ddev composer require drush/drush
 ddev restart
Enter fullscreen mode Exit fullscreen mode

2. Create .ddev/config.multisite.yaml with following (example) content:

   additional_hostnames:
  - subsite1
  - subsite2
  - subsite3
Enter fullscreen mode Exit fullscreen mode

This file contains list of URLs for our subsites which is equivalent to setting up vhost for each site.

3. Create folders for subsites in your sites directory

   mkdir subsite{1..3}
Enter fullscreen mode Exit fullscreen mode

4. Copy examples.sites.php to sites.php and add entries for subsites in your sites.php file:

   $sites['subsite1.ddev.site'] = 'subsite1';
   $sites['subsite2.ddev.site'] = 'subsite2';
   $sites['subsite3.ddev.site'] = 'subsite3';
Enter fullscreen mode Exit fullscreen mode

5. DDEV creates settings.php in default directory so copy this to your subsites

6. Since we are using single DB with prefixes add entry for db prefix in each of your subsite's settings.php after the line to import settings.ddev.php. Like this:

   // Automatically generated include for settings managed by ddev.
if (file_exists($app_root  . '/sites/default/settings.ddev.php') && getenv('IS_DDEV_PROJECT') == 'true') {
  include $app_root . '/sites/default/settings.ddev.php';
}
$databases['default']['default']['prefix'] = 'subsite1';
Enter fullscreen mode Exit fullscreen mode

Note: By default ddev uses __DIR__ . '/settings.ddev.php' since it assumes settings.ddev.php is in the same directory, but since that is not the case with our subsites folders, we are using $app_root . '/sites/default/settings.ddev.php' instead.

7. Run ddev restart so it creates urls for our subsites.

8. Go to the respective URL on browser 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-8 for your new site. And get it up and running in no time.

In case you want to use multiste with multiple databse refer to this example.

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 ddev mysql instead of just mysql and also the credentials will be the one which are created by DDEV rather than what you have on your system.

Let me know in the comments if you found this helpful! KThnxByee!👋🏼

💖 💪 🙅 🚩
shriaas2898
Aastha Shrivastava

Posted on September 22, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Setting up drupal9 multi-site with DDEV
drupal Setting up drupal9 multi-site with DDEV

September 22, 2021