In my previous DDEV post we went over the basics of getting started with Drud's DDEV. This time around we'll get a little more involved by setting up a multisite installation for Drupal 7.

If you are unfamiliar with the Drupal multi-site concept, it is basically a supported mechanism for having multiple sites (with separate databases if you prefer) accessing the same core Drupal files in the web document root. You can have a subdomain such as site2.mysite.com using the same Drupal core files at mysite.com.

For this tutorial we'll be setting up a subsite site2 to work with the core files from mysite (the main drupal installation).

1 - Add additional hostname entries to config.yaml

Assuming you have already ran ddev config and ddev start in mysite's docroot, there will be a hidden folder named .ddev. Inside of that is your config.yaml file. We'll need to add an additional hostname entry for our second site inside of this file:

additional_hostnames:
  - site2

1.2 (optional) - Add mysql hook in .ddev/config.yaml to create the database if it doesn't exist:

In order for site2 to function correctly, we need to create and import it's database into ddev. One way to create the database is to utilize ddev's hook system to run a sql shell command.

In our .ddev/config.yaml file, we'll create the database if it doesn't exist by utilizing the post-start hook and exec command:

hooks:
  post-start:
    - exec: mysql -uroot -proot -hdb -e "CREATE DATABASE IF NOT EXISTS site2; GRANT ALL ON site2.* TO 'db'@'%';"

2 - import site2's database through phpmyadmin:

If you read my previous article, you know that DDEV containers have phpmyadmin built in!

You can run ddev describe to list your container's database information. Among the output is a section named other services - which states your phpmyadmin port. By default this is 8036.

Other Services

--------------

MailHog:   http://mysite.ddev.local:8025

phpMyAdmin: http://mysite.ddev.local:8036

Note: If you run into an error regarding memory or size, a simple workaround is below in 2.1.

2.1 (optional) - Import the second database with the command line:

If you ran into an issue with the database being too large to import with phpmyadmin (or prefer not to use it), you can create and import the database by hand with some basic sql commands.

Make sure you have the database.sql file available within the mysite's docroot (or somewhere where it will be accessible within the container).

  • ssh into mysite's container with the command ddev ssh
  • then log into mysql with the command mysql -uroot -proot
    • (optional) if you didn't use the post-start hook (step 1.2) run the following command to create the database:
      • create database site2;
  • use site2;
  • source /var/www/html/site2.sql;

3 - site2's folder within mysite/sites:

Make sure that the folder site2 exists in mysite/sites (mysite/sites/site2). This should contain site2's settings.php, modules, and files.

Now that we've set up the second database we can edit site2's settings.php:

<?php

$databases['default']['default'] = array(
'database' => "site2",
'username' => "db",
'password' => "db",
'host' => "db",
'driver' => "mysql",
'port' => 3306,
'prefix' => "",
);

ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);
ini_set('session.gc_maxlifetime', 200000);
ini_set('session.cookie_lifetime', 2000000);

$drupal_hash_salt = 'mysalt90j9t34jv90j03499sdh';

// This determines whether or not drush should include a custom settings file which allows
// it to work both within a docker container and natively on the host system.
if (!empty($_SERVER["argv"]) && strpos($_SERVER["argv"][0], "drush") && empty($_ENV['DEPLOY_NAME'])) {
  include __DIR__ . '../../../drush.settings.php';
}

4 - Edit sites.php

The last thing we'll need to do is edit our sites.php file, which should be located at mysite/sites/sites.php.

Note: The $sites variable is simply an array that maps host names to folders inside of mysite/sites.

Ex: You'll need to concatenate the site2 hostname (we created in our .ddev/config.yaml) with '.ddev.local' (by default) and set it equal to the site2 folder name (mysite/sites/site2).

<?php

$sites['site2.ddev.local'] = 'site2';

That's it. If you restart your ddev instance and visit site2.ddev.local, you should see your second site up and running!