Drupal 8: Setting Up Multi-site

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: /public_html/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.kalose.net

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

I will name my databases user_dogs and user_cats which will be accessed by user user_drupal who will have privileges: ALTER, CREATE, CREATE TEMPORARY TABLES, DELETE, DROP, INDEX, INSERT, SELECT, AND UPDATE.

The Technical Part

Coding

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'.

My sites.php:

Since we said dogs.kalose.net was going to use the /dogs/ folder and www.kalose.net/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:

Now we have successfully set up Drupal for multi-site. Although, if you visit dogs.kalose.net or www.kalose.net/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.

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.

* 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.kalose.net and www.kalose.net/cats/ and set them up using databases user_dogs and user_cats. Using Multi-site, these sites can be completely different except for the fact that they share the same underlying code.

Author: Akshay Kalose

A teenager, who is interested in Computer Science, Information Technology, Programming, Web Designing, Engineering and Physical Sciences.

23 thoughts on “Drupal 8: Setting Up Multi-site”

  1. I have set up a multi-site with 2 sites in Drupal8. My problem is that trying to use the Configure Management it says it needs the sites to exactly like each other through cloning. I have done that but now the cloned site is not reading the database as it is set up in the settings.php file. The problem is that the user from the cloned site is showing up in the clone site even though the 2 databases are showing different users.

    Any ideas?

    Thanks.

    1. I think the solution to this would be to instead of symbolic linking the whole drupal directories, just symlink the directories and files inside the drupal directory that should stay in sync excluding the settings.php.

      1. When I did that it just showed the index of the directories. It needs to be linked to the Drupal main directory.

        Also discovered that the cloned site it reading/writing to the original database even though the settings.php file is pointing to the cloned database.

          1. Discovered it is NOT a symlink problem. When I did as you suggested I started getting a lot of errors.

            The problem is that the “default” directory that is created upon installation of Drupal 8 has a “setting.php” file in it that Drupal is reading. It was pointing to the original database, overwriting the “setting.php” that was created in each of the other sites. I renamed the “default” directory and now it is writing to the correct database for each of the sites.

            However the Configuration Management module is still not working as it should as when I export from the development site and then import into the production site the data/configuration is NOT being imported.

          2. When you created your first site, did you initialize everything with multisite setup, or did you go through a single site install, then attempt to add another site? It sounds like you might have an issue with your initial install method. Trying to convert a single site install into multisite can get really tricky, as the first site would originally write to the “default” directory. Just a thought…

  2. Thanks a lot for this blog post!
    I was following your youtube video, but I kept getting errors. I think I was messing up in the part involving subdomains. Anyways I tried again following the article itself and finally got it to work!

  3. Thanks for the clear and concise article. Just a question… Have you configured a D8 multisite install sharing user tables (minus roles)? I am in the process of building out a project that will host multiple sites, but need a single set of credentials to be able to access the various back office areas. Any suggestions?

  4. Thanks for the helpful tutorial.

    One thing worth mentioning is that Drupal 8 provides a template for the sites.php file called “example.sites.php”. I would recommend renaming or copying this rather than creating the file from scratch.

    Having run multisites on previous Drupal versions, I was wondering why my sites were being ignored until I found this. If you don’t have a sites.php file, Drupal will operate in single site mode, and will require the site to be in sites/default.

    Thanks!

    James

    1. Glad you found it helpful! I didn’t include a reference to the example.sites.php file because it just consists of comments and I have included basic documentation in my code example. However, since the documentation in that file is much more in depth I have included that in this article! Thanks!

  5. Thank you for posting this tutorial!
    So I have set up my site and installed everything I have two subdirectory linked to my drupal folder in public_html/drupal. They both installed but when I attempt to switch a page it says that drupal has already been installed. Any ideas as to why they are not acting independently?

    1. Hey, glad to hear you found this tutorial useful! Could you give some more information and links to the specific problem? Have you made sure you have created the symbolic links correctly? You can check by going into those subdirectories and if it is working properly you should see the contents of the drupal directory and be able to freely navigate through. I am asking this because it was one of my major problems at first when setting up multi-site.

  6. Hi. I have tried your solution for install drupal 8 on multi sites but I have a problem because I can’t start with configuration setup after creation the link. My procedure:
    – I have created a path with name “template1” into “public/sites”;
    – I have created the file site.php;
    – Into “/www/mysite/source” I have unzip drupal file tar.gz;
    – Into “/www/mysite” I have executed “ln -s source template1”
    – I have executed “http://mysite/template1” and I have an error with message “page not found” from default site with standard drupal template;
    Where is my error?

    Thanks a lot for you help!

    1. Hmm… A Drupal page is loading, so it wouldn’t be a problem with the symlink. Have you double checked your “sites.php” file and also made sure you copied the “settings.php” and “services.yml” files from “sites/default” to “sites/template1”? Those would be my first guesses as to what the problem is being caused by.

    2. Did you create “site.php” or “sites.php”. It should be the latter.

      Also, if you untared into “www/mysite/source”, your actual Drupal source directory would be something like”www/mysite/source”

      1. Sorry, pressed submit to soon. Your Drupal source directory would be something like “www/mysite/source/drupal-8.0.0-rc4”. I’m assuming you’ve moved it back to source.

  7. Thanks for the great, clear instructions! I’m relatively new to Drupal, couldn’t get Drupal 7 to do what I needed design-wise, and have come back because Drupal 8 looks to be designer friendly.

    I’m setting up multi-site subdomains (thanks to your instructions) on Godaddy shared Linux hosting and managed to successful get through the install without trouble BUT cannot access any page past the index.php page on any site, including the site that’s tied to the hosting account. The hosting site throws 404 errors and the subdomains show “the webpage cannot be found”. am thinking it’s an .htaccess issue.

    Am troubleshooting now, will post the answer when I find it.

    Thanks again!

      1. So the problem was very straight-forward.

        I used Godaddy’s file manager (in cpanel) to untar the drupal core tarball and thenmove the contents to parent directory, like in your tutorial. HOWEVER when it moved the files, it omitted files starting with “.” so no .htaccess or .gitignore, etc etc.

        Once I figured out what I was missing, I unzipped drupal core locally and ftp’d the missing files. Problem solved – site(s) work great!!!! Thank you!!!

        1. Great to hear! For future reference if needed, when you click “File Manager”, there is actually a check box to show these hidden files, “Show Hidden Files (dotfiles)”.

          Once you have this checked, you will be able to see the .htaccess or any dot files in the web file manager.

Leave a Reply

Your email address will not be published. Required fields are marked *