Setting up Drupal 8 multisites

Submitted by Juan on Wed, 07/13/2016 - 15:50
Lego construction blocks

Drupal has the ability to mount multiple websites within one single Drupal instance. How is convenient to use this feature? It's a great idea to consider when we have several sites which shares similar features. For instance, if we have several websites where we must implement the same modules or share one same visual theme, it could be convenient to integrate them as part of a Drupal multisite. The advantage of doing that is the maintenance tasks become easier when updating the Drupal core and its modules.

For purposes of this test I will emulate the creation of two children from my main website: one for HATR Project and one for a blog called Dev Notes.

First we must create a database for each site we want to integrate in our multisite. In this example I will use different MySQL user accounts for the databases:

CREATE USER hatr@localhost IDENTIFIED BY 'password1234';
CREATE USER devnotes@localhost IDENTIFIED BY 'password5678';
CREATE DATABASE hatr;
CREATE DATABASE devnotes;
GRANT ALL PRIVILEGES ON hatr.* TO hatr@localhost;
GRANT ALL PRIVILEGES ON devnotes.* TO devnotes@localhost;

We must add the children sites in the /sites/sites.php configuration file. This file must have the same format as example.sites.php located in the same folder. You can copy and use it as a template:

cp example.sites.php sites.php

We add the next lines into sites.php:

$sites = array(
// HATR Project
'85.hatr.capablanca.dev' => 'hatr',
// Dev Notes
'85.devnotes.capablanca.dev' => 'devnotes',
);

The keys of the $sites array must have the following format: portnumber.domain.folder. Please watch here I don't care about the folder name at the ending of the string as I want to access to my website from the root URL. For instance, my website will be accessible from http://hatr.capablanca.dev:85, not from something like http://capablanca.dev/hatr.

The values on the array corresponds to the folder name where the children sites configuration will be placed, so we must go later to the /sites folder of the parent Drupal site and create there two new folders: /sites/hatr and /sites/devnotes on this example:

mkdir hatr
mkdir devnotes

Now we must copy the default configuration files to the newly created folders. Run the following commands from the directory /sites/default:

cp default.settings.php ../hatr/settings.php
cp default.settings.php ../devnotes/settings.php
cp default.services.yml ../hatr/services.yaml
cp default.services.yml ../devnotes/services.yaml

We must create the files and translations folders for each of the children sites as follows:

mkdir devnotes/files
mkdir devnotes/files/translations

These folders must have writing permissions enabled to install Drupal. The next step is to point the subdomain on the Apache server configuration. Each children site must have a configuration file on /etc/apache2/sites-available/hatr.conf with the following parameters:

<VirtualHost *:85>
    ServerName hatr.capablanca.dev
    ServerAlias hatr.capablanca.dev
    ServerAdmin admin@capablanca.dev
    DocumentRoot /var/www/jdeleon/

    ErrorLog ${APACHE_LOG_DIR}/hatr-error.log
    CustomLog ${APACHE_LOG_DIR}/hatr-access.log combined
</VirtualHost>

Here is important to say that each site must point to its parent's location, not the newly created folder on /sites. We must replicate the same Apache configuration file for each children, in my case, devnotes. After that we have to activate the sites and restart the server so the changes take effect:

sudo a2ensite hatr
sudo a2ensite devnotes
sudo service apache2 restart

Don't forget to edit your local host file on your computer to let your new websites be accessible from the URL. In my case I had to point my two new subdomains to my virtual machine's IP as setting only the parent website domain is not enough. In my case I executed the next command on the terminal:

sudo nano /etc/hosts

And added the next two lines:

192.168.50.90 capablanca.dev
192.168.50.90 hatr.capablanca.dev
192.168.50.90 devnotes.capablanca.dev

And voilà! When I go to http://hatr.capablanca.dev:85 on my browser looks like I'm ready to install my new website.