fbpx Drupal 8: Setting Up Multi-site | DrupalExp - Premium Drupal Themes & Modules

Drupal 8: Setting Up Multi-site

Multiple Sites

There is an incredibly easy way to create multiple Drupal sites. You don’t need to duplicate the code. You can have multiple sites, with different content, users, and themes since each site can use a different database. They will all run on a single codebase and will be able to share modules. This is one of Drupal’s greatest features called Multi-site. It was first implemented in Drupal 4, and it is here now in Drupal 8.

Getting Prepared

To get started, go ahead and download Drupal.

Then, upload and extract all the files into a new folder for Drupal. For example: /drupal/

Or simpler, use drush run command drush dl drupal

$ drush dl drupal

I will be creating two sites, one for dogs, and the other for cats.

These multiple sites can be accessed either from subdomains or subdirectories. If you are going to use subdomains, go ahead and create them. However, if you are going to use subdirectories, hold off until later, since you will have to remove it anyways.

I want the dogs website to be accessed by a subdomain and the cats website by a subdirectory, so I will create dogs.drupalexp.com

Now create a separate database for each site. Also, create a user or two to access these databases.

I will name my databases drupal_dogs and drupal_cats.

The Technical Part


Navigate into the directory: /drupal/sites/

We need to tell Drupal we are going to have multiple sites. To do this, create a file called sites.php. You can find extra examples and documentation of how to set it up in the example.sites.php file.

This file will contain an $sites array containing all the rules. For each index, the key will be the formatted address the website will be accessed at, and the value will be what folder to look in.

Format of each index:

'<port>.<domain>.<path>' => 'directory',

$sites = array(
    // http://dogs.drupalexp.com/
    'dogs.drupalexp.com' => 'dogs',
    // http://www.drupalexp.com/cats/
    'www.drupalexp.com.cats' => 'cats',

Since we said dogs.drupalexp.com was going to use the /dogs/ folder and www.drupalexp.com/cats/ was going to use the /cats/ folder, these folders need to be created: /drupal/sites/dogs/ and /drupal/sites/cats/

SSH / Shell / Terminal

Connect to your webserver via SSH. If you’re on Windows, you can use PuTTY.

The /drupal/sites/default/ site will have default files we need to copy into each of our sites. Copy these files using the cp command in the sites directory:

cp default/default.settings.php dogs/settings.php
cp default/default.settings.php cats/settings.php

cp default/default.services.yml dogs/services.yml
cp default/default.services.yml cats/services.yml

Now we have successfully set up Drupal for multi-site. Although, if you visit dogs.drupalexp.com or www.drupalexp.com/cats/ it will be blank. Why? How does the web server know to point these sites to the /drupal/ directory? This is why we need to make symbolic links from /dogs/ and /cats/ to /drupal/

If a subdirectory is created when you create a subdomain, delete that directory.

rmdir dogs

Now time to create the symbolic links. This is why we did’t have to create the /cats/ subdirectory. The symbolic link will automatically point /dogs/ and /cats/ to /drupal/ like they are the same folder.

ln -s drupal dogs

ln -s drupal cats

You may notice that these folders/links have all permissions (chmod 777). You don’t need to worry about this because these permissions won’t be used, instead the permissions of /drupal/ or the file/folder you are linking to will be used.

Finishing Up

All that is left to do now is to install Drupal. I can visit dogs.drupalexp.com and www.drupalexp.com/cats/ and set them up using databases dogs and cats. Using Multi-site, these sites can be completely different except for the fact that they share the same underlying code.