Creating a Drupal multisite installation

Create multiple Drupal sites from a single code base and multiple databases

One important feature of the Drupal content management system is that it enables you to run multiple websites from a single base installation of the framework — a feature called multisite installations. Such installations are particularly useful to web administrators who have to manage and maintain several sites on a long-term basis, and the functionality simplifies the administration of multiple Drupal installations located on the same server by making all of them run from a single script. In this article, see how to install, configure, run, and administer multisite Drupal.

Timi Ogunjobi, Software Engineer and Author, Xceedia Limited

Timi Ogunjobi photoTimi Ogunjobi is an author and web applications developer. He is CEO of Xceedia Limited, a software engineering firm with interest in web and mobile applications. He is also the author of three Drupal books among several others.



30 April 2013

Also available in Chinese Russian Japanese

There are many reasons why a systems administrator or developer may choose to host multiple sites on the same server in a multisite configuration. For example, such a configuration could:

  • Enable you to build an efficient development environment
  • Allow you to stage and test a site before it goes public
  • Let you run different software on the same site
  • Enable sharing of a single code base across several sites
  • Leverage shared hosting
  • Allow you to run similar secure (HTTP over Secure Sockets Layer [HTTPS]) and standard (HTTP) sites on the same server
  • Ease server administration

Not all of these reasons, however, are best addressed with Drupal's multisite feature. This articles focuses on just one strategy: how to use Drupal's built-in multisite support to create and serve several sites from one instance of Drupal.

Basic considerations

Using the Drupal content management system (CMS) to create a multisite configuration has several distinct advantages, one of which is that it is probably the easiest configuration to set up and maintain. Another is that data duplication is minimized because Drupal is installed in only one place on your server. Finally, Drupal has a wealth of contributed modules that can be used to augment multisite handling, offering advanced capabilities to all the sites in the arrangement. The combination of added features and simplified administration would be a boon for anyone wanting to set up and administer a multisite arrangement. Nevertheless, there are a few drawbacks to using such a configuration, and two in particular deserve clear mention: security and sharing.

The sites in a multisite installation share the same code and thus the same files. As a result, anyone who gains access to the server's file system for one site also has access to all the files used for all of the sites on that multisite installation. Therefore, in cases where different people are managing different sites within the arrangement, it may not be quite secure. If security among sites is a primary issue, or if different people manage the various sites, Drupal's built-in multisite functionality may not be the best solution. Instead, consider giving each site its own distinct Drupal instance. Although the advantages of easier administration that the multisite arrangement affords will be lost, your sites will be better protected from mishaps.

The second concern — sharing — centers around database access. Even though Drupal's multisites share the same source code, they do not share the same database data. Thus, nodes, users, and comments in one site are not accessible by default from other sites. This behavior can actually be regarded as a good thing, but in situations where the administrator may require that all the sites share information, it may be a limitation for the design of Drupal's multisite feature. Nevertheless, the sweet spot for Drupal's built-in multisite support is that modules, themes, and, even to some extent, uploaded files, can be shared across all the sites. In addition, the same or custom files can be retained in different folders corresponding to the different sites in the configuration. If it's important that all content be shared, consider using a multidomain feature, which is easily provided by a module of the same name.


Installing Drupal for multisite use

After downloading the Drupal archive (see Resources for a link), extract the file and copy the resulting folder to the document root of your web server. This task isn't difficult with a suitable FTP client; with Secure Shell (SSH) access to your server, the task is even easier. Simply run a wget command to download the archive file from the repository, and extract it to produce the Drupal installation folder. (If your development environment is local, stand by for more options.) Name the installation folder /goodsite.

Three different sites will be created for this exercise; therefore, three different databases are needed — one for each site. Using phpMyadmin from the Cpanel of your server, this task is easily accomplished. Next, install the framework. Two tasks must be performed, however, before using your web browser to run the installer:

  • Provide Drupal with the information it needs about the sites you intend to run.
  • Pass the site-specific configuration data from Drupal to all of the sites.

To achieve the first task, create a special directory for each site. For the second, locate a settings file in each directory. The objective is to set up one master installation with two sub-sites. Each sub-site shares the same files as the core, but each will have its own separate URL and database. The URLs of the sites to be created are:

  • Master site: http://goodsite.com
  • Sub-site 1: http://bettersite.com
  • Sub-site 2: http://bestsite.com

Installing the first host

After placing the Drupal source code in the document root and having created the MySQL databases, begin installing the first site and the master domain.

The master domain

The master domain name is the URL from which the main site will be accessed (in this article, http://goodsite.com). The primary purpose of the master domain is to host the base Drupal installation. Regardless of whether this domain is used as one of your sites, if any sub-sites fails, their URLs will default to the master domain; therefore, it is a good idea that this site contain information relevant to the sub-sites, such as contact information.

As mentioned, a subfolder must be created within the main Drupal folder for each sub-site. What many would imagine the most difficult task in Drupal's multisite installation is simply routine: Let Drupal know what sites you want to host, and simply create subfolders for those sites in the /sites folder.

In the Drupal main root folder is a directory called /sites. If you've administered Drupal before, you know all non-core code ought to reside within this folder. In the default Drupal installation, the /sites folder contains two directories. The first, called all/, is where you should put downloaded themes and modules. The all/ directory is the location for information shared across all sites. The second folder, named default/, is where configuration files should be kept, as well as non-core and custom themes and modules. Drupal typically searches the default/ directory for site configuration information unless it is instructed otherwise. Note, however, that these are assumptions for a generic, single-host site — not necessarily for a multisite installation.

Creating the subfolders and files

A big tip for building multisite Drupal is that apart from the all/ and default/ folders, other folders can be created in /sites, and Drupal will assign them special meaning. More specifically, Drupal assumes that all other directories found in the /sites folder represent a hostname or a hostname pattern.

You create a subfolder in the pattern /goodsite/sites/<sub-site> under the /sites folder for all sub-sites, substituting your intended URL for the name of each sub-site folder. In this example, you have the following sub-sites, as shown in Figure 1.

  • Sub-site1: /goodsite/sites/bettersite
  • Sub-site2: /goodsite/sites/bestsite
Figure 1. The /sites folder hierarchy
Image showing the sub-site folder hierarchy

For each sub-site, complete the following steps:

  1. Copy the file default.settings.php from sites/default to each sub-site folder, and rename it settings.php.
  2. Use CHMOD to set the permission on settings.php to 644, if it is not the current permission.
  3. For a Drupal, Version 6 installation, create a folder named files under each sub-site, and use CHMOD to change the permission to 755.

    This is a folder inside which Drupal is able to write content — typically, uploaded files, image thumbnails, aggregated Cascading Style Sheet (CSS), and JavaScript files. These files are not stored in the database, but rather in the file system, so you must have a directory in which these files can reside.

    By default, Drupal looks for a subdirectory named files/ within the directory of every site. In a default Drupal installation, this folder would be in sites/default/files/.

  4. Create a folder called modules for each sub-site.

    This is the folder in which any modules specific to that sub-site reside. Any modules that all sites will use should reside in the sites/all/modules folder.

  5. Create a folder called themes for each sub-site.

    This is where any themes unique to that sub-site are placed. Any theme that all sites will use should reside in sites/all/themes.

Now, point the sub-site URLs to your web server, if they are not administered by the same hosting company. If you use different hosts for the various sites, you will need to change the Domain Name System (DNS) information (both the registrars and the hosting companies should be able to tell you how to do so). Thereafter, you will have to "park" that domain on top of your primary domain — in this case, goodsite.com. From this point, you have a few alternatives, the more difficult of which involves using what's called a symlink. Parking is much simpler, however; so in your Cpanel, click on Parked Domains. Add bettersite.com and bestsite.com in New Domain Name, and then click on Add Domain.


Creating the multisites

There are several ways to go from here, but again, let's take the easiest route: directly loading the database for each sub-site with the tables created in the master site installation. To do so, you must create a dump of the database tables from the master site installation and save it locally. Then, either upload this file with phpMyadmin or open it using a code editor. To use the second option, perform these tasks within phpMyadmin:

  1. Go to the database created for the sub-site.
  2. Go to SQL, and then paste all of the code from the Structured Query Language (SQL) dump there.
  3. Click on Go.

This procedure reproduces the master site database tables in the sub-site database.

Next, open the settings.php file for each sub-site, and look for the following line:

$db_url = 'mysql://username:password@localhost/databasename';

Change this database connecting string to reflect the parameters for the sub-site database (username, password, hostname, database name). Now if you go to bettersite.com, you should get the standard "Welcome to your new Drupal website" page, as shown in Figure 2. Repeat these steps for all additional sites.

Figure 2. Your new website in Drupal
Image showing the new website in Drupal

Creating multisites from sub-domains

There is another way to quickly create multisites by first detailing them as sub-domains from the Cpanel. In this method, however, you must initially create folders for each site under /goodsite/sites, this time using the following convention (see Figure 3):

  • Sub-site1: /www/goodsite/sites/ bettersite.com
  • Sub-site2: /www/goodsite/sites/ bestsite.com
Figure 3. Creating multisites from sub-domains
Image showing the sub-domains used to create multisites

Note that the sub-domain folders must have the same names as the sub-domains themselves. As before, put a copy of default.settings.php in each sub-domain folder, change the file name to settings.php, and issue CHMOD to change the permission to 644.

Now, return to Cpanel and create two sub-domains:

  • bettersite.goodsite.com
  • bestsite.goodsite.com

A symlink is a file that contains a reference to another file or folder in the form of an absolute or relative path and is created to affect path name resolution. Use any code editor to create a file called symlink.php, and then add code similar to the following:

<?php
symlink( '/home/Cpanel_User_Name/public_html/', 'sub-domain' );
?>

In this case, the file would look something like this:

<?php
symlink('/home/myuserdirectory/goodsite/','bettersite');
symlink('/home/myuserdirectory/goodsite/','bestsite');
?>

Put this symlink in the main directory of the main site, and run it from the URL http://goodsite.com/symlink.php. If the code syntax is good, you should see a blank page; otherwise, you will see error messages. Tweak the code in this file until it comes out correctly.

Configure .htaccess

Now open the .htaccess file in any code editor. Look for the following line:

 # RewriteBase/

Remove the hash sign (#), and add the following line before saving the changes:

RedirectMatch 301 ^/sub-domain/(.*)$
http://sub-domain.yoursite.com $1

Here's what .htaccess should look like when finished:

RewriteBase / RedirectMatch 301 ^/bettersite/(.*)$
http://bettersite.goodsite.com/$1 RedirectMatch 301
^/bestsite/(.*)$http://bestsite.goodsite.com/$1

This redirection makes it possible for both http://goodsite.com/bettersite and http://bettersite.goodsite.com to be redirected to the same site. If everything has been done correctly, you will be directed to the installation page when you access the sub-site URLs http://bettersite.goodsite.com and http://bestsite.goodsite.com.

With all the sites installed, it's easy from the Cpanel to associate the site http://bettersite.goodsite.com using a redirect to the URL http://bettersite.com.


Multisite on a local host running Ubuntu

Running a multisite installation on Ubuntu is a bit more complicated than what you have done so far for remote hosting. A multisite installation can be easily configured using the method outlined in the following sections.

Create a new hosts folder

Begin by creating a folder called goodsite in your home folder using the following code:

>> filesystem/home/<your ubuntu login name>/

The result should be -> /home/<your ubuntu login name>/goodsite.

Now copy all your Drupal files into this folder.

Edit the hosts file

Edit the hosts file by opening a terminal window with the following command:

sudo gedit /etc/hosts'

You can then add any site name in the format 127.0.0.1 mysite.com. For this example, add the following lines:

127.0.0.1 goodsite.com
127.0.0.1 bettersite.com
127.0.0.1 bestsite.com

Edit the sites available

Now go to the sites-available folder to create a file called goodsite.com using the following commands:

cd /etc/apache2/sites-available
sudo cp default goodsite.com
sudo gedit goodsite.com

The first command goes to the sites-available folder; the second command copies the default file and renames it goodsite.com. The third command opens the file goodsite.com for editing. Add the code shown in Listing 1 to the file.

Listing 1. Addition to the sites-available folder file for the new site
<VirtualHost *:80>
ServerAdmin webmaster@localhost 
ServerName goodsite.com               
DocumentRoot /home/<your ubuntu login name>/goodsite
        
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>

Save goodsite.com, enable it, and then reload Apache using the following commands:

sudo a2ensite mysite.com
sudo /etc/init.d/apache2 reload

Create sub-site folders

Create your sub-site in the /sites folder /home/[your ubuntu login name]/goodsite/sites/bettersite.com, and then add settings.php to the new folder. If you have already navigated to the sites/ directory, copy the settings.php file with the following command:

$ cp default/default.settings.php bettersite.com/settings.php 
$ chmod 777 bettersite.com/settings.php

Run sub-site installation

Open your browser, then type http://bettersite.com. You should see the installation setup page. Make the settings.php file read-only again to avoid security messages, then run the sub-site installation.

Next, create a site folder and run the installation for bestsite.com as well as any other sub-sites mentioned in your hosts configuration.


Multisite modules

Modules are used to extend the functionality of a basic Drupal installation and come in various categories, from administrative to actual performance-enhancement functionalities. Several modules can assist in creating a multisite or similar configuration. For details on how to use these modules, consult the installation documentation for each module.

The Domain Access modules

The Domain Access modules (see Resources for a link) enable the creation of a multisite using a single code base, a shared user database, and shared sign-on but with different content databases. Such a configuration is different from what we have done in this article. Domain Access is actually described as a set of modules that facilitate running a group of sites by using one basic Drupal code installed inside a single shared database. These modules enable all of the sites to share users, content, and settings if they have been preconfigured to operate in such a way. The Domain Access modules employ Drupal's Node Access System to establish which content should be made available on each site in the multisite arrangement. The modules, unlike other multidomain modules, also control user access on the basis of the active domain the user is viewing, not on the basis of the group or site to which the user belongs.

The Multisite Manager module

The Multisite Manager (see Resources for a link) is a contributed module that allows creation of new Drupal sites from a base Drupal site without the need to access the installation database. By default, the new site is installed in the same database as the base installation, but with a different prefix. Otherwise, if the current user has access permission to create a database, the installation could possibly be done in that database.


Conclusion

This article explored how to create a multisites configuration using Drupal as the base CMS. You installed the master site and located the sub-sites within its directory structure. You learned how to run multisite installations on a remote server by using the administrative facilities your hosting company provides. You also learned how to create a multisite installation on a local machine — specifically, on a computer running Ubuntu Linux®. This article also introduced two Drupal modules that can assist you in the creation of both multisite and multidomain configurations.

Resources

Learn

Get products and technologies

Discuss

  • The developerWorks community: Connect with other developerWorks users while exploring the developer-driven blogs, forums, groups, and wikis.

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Web development on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development, Open source
ArticleID=926620
ArticleTitle=Creating a Drupal multisite installation
publish-date=04302013