Multi-Site is one of the Powerful feature available in Drupal CMS.  If you are running more than one Drupal site, multi-site will help you simply all your site management tasks. Multi-site allows the user to share a single Drupal installation (including core code, contributed modules, and themes) among several sites. This is particularly useful for managing the code since each upgrade only needs to be done once.

There are multiple Options available for Drupal Multi-Site installation. Couple of them are listed down here;

  • Each site can have its own database
  • All the sites can share the same database
  • Each site can have own content database & shared user database. This can be done in two different ways either by creating a separate DB for User related tables (or) considering one of the site as a base  &  the hold the user tables in that DB

Approach for single sign-on

If  the option of having different content database for all sites & using one site as base (for User tables) is selected, then the following snippet might help you implementing the solutions. I’m providing snippet (or) information to this implementation because the drupal.org, have solutions for all the other implementation.

Solution :

Step 1: Install & Configure Drupal Multi-Site , remember to setup all the drupal instance with different database.  After the installation is complete you should be having multiple site running with multiple database on one code base

Step 2: As per the  Module developer Guide, Drupal can connect to different databases with elegance and ease.  Please note, it has to be of the same database type. So we convert the $db_url variable to an array.

$db_url['default'] = 'mysql://drupal:drupal@localhost/drupal';
$db_url['base'] = 'mysql://user:pwd@localhost/anotherdb';

now, we need to establish connect to the base database  using the drupal function db_set_active() as & when required.  db_set_active() function can be used to change where database queries are sent. If the database has not yet been used, it is initialized using the URL specified for that name in Drupal’s configuration file. If this name is not defined, a duplicate of the default connection is made instead.Be sure to change the connection back to the default when done with custom code.

db_set_active($name = ‘default’)
Parameters
$name The name assigned to the newly active database connection. If omitted, the default connection will be made active.
Return value
the name of the previously active database or FALSE if non was found.

Note : This only works with two databases of the same type.

Alternative Implementation of the Solution:

Now there is one challenge of  harding coding the base DB informations, as its not a good practice,because if the DB connection parameter value changes you may need to update multiple place. So to avoid this, you can use the following code snippet.

Base Site’s Setting - Add this line after the $db_url

$db_url=array(‘default’=>$db_url, ‘base’=>$db_url);

Child Site’s Custom Setting – Add the below snippet after your $db_url declaration of the respective child sites.

 // Folder name of the site which you consider as base. Please remember in multi-site environment you are going to have folder for ever site
$dName='example.com';
require_once "sites/$dName/settings.php";
$b_db_url=$db_url['base'];
$db_url=array('default'=>$db_url, 'base'=>$b_db_url);

Some Alternatives : http://drupal.org/project/sso,

This solution will be applicable only if you wish to host the User database on your server, else you can use Open ID authentication

Updated on Jan 07,2010 : This solution can be implemented for the Drupal sites which is running as Separate instance & in the same server (Shared Hosting). Provide the complete path of the BASE Site’s default folder.

For Ex :

include_once(“/srv/www/sites/www_xyz_com/sites/default/settings.php”);

To make this solution, completely configurable one, please create a variable to hold the Base site folder Name

Related Reading:

C Programming Language (2nd Edition)C Programming Language (2nd Edition)Presents a complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the fin... Read More >
Code Complete: A Practical Handbook of Software ConstructionCode Complete: A Practical Handbook of Software ConstructionFor more than a decade, Steve McConnell, one of the premier authors and voices in the software community, has helped change the way developers write c... Read More >