Creating a Drupal multisite enviroment using Drupal 6

This guide will show you how to create a multisite environment using a shared user system with the user only needing to log in once, to be logged in to all the sites.

I will be doing this using WAMP on a Windows XP machine, so some of the steps won't work if you are using IIS or will differ if you are using a different setup.

The result we will be aiming for is a shared user system allowing the user to log in/out of 3 sites which I will use the following domains:

WAMP installation

Download and install the latest version of WAMP from the WAMP website. The latest version comes with PHP 5.3 which has problems with Drupal 6, so you will also need to download and install PHP 5.2.x from the WAMP Addons page once WAMP is installed.

Once WAMP and PHP 5.2.x have been installed and WAMP has restarted, left click the WAMP icon in the system tray and go to PHP > PHP Version and select the 5.2.x version you downloaded.

Left click the WAMP icon again and this time select Apache > Apache Modules and scroll down and select rewrite_module and vhost_alias_module.

WAMP Virtual Hosts set up

Again, left click the WAMP icon and go to Apache then select httpd.conf.

Scroll down to the bottom of this file and look for the following lines:
# Virtual hosts
# Include conf/extra/httpd-vhosts.conf

Remove the comment from the beginning of the second line to end up with this:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Save and close the file.

Now in Explorer, navigate to the WAMP directory\bin\bin\apache\Apache2.2.11\conf\extra\ directory.

Open the httpd-vhosts.conf file with your favourite text editor.

Replace the contents of this file with the following:

NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "c:/wamp/www/"
ServerName www.local.com
ErrorLog "logs/local-www-error.log"
CustomLog "logs/local-www-access.log" common
<directory "c:/wamp/www/">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "c:/wamp/www/"
ServerName community.local.com
ErrorLog "logs/local-com.localhost-error.log"
CustomLog "logs/local-com.localhost-access.log" common
<directory "c:/wamp/www/">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "c:/wamp/www/"
ServerName support.local.com
ErrorLog "logs/local-sup.localhost-error.log"
CustomLog "logs/local-sup.localhost-access.log" common
<directory "c:/wamp/www/">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</directory>
</VirtualHost>

This sets up 3 virtual hosts all using the same directory for the files, as we are using a shared codebase. This is also set up to only allow traffic from the localhost. To make the site public to all, change the line:

Allow from 127.0.0.1

To:

Allow from all

Save and close the file.

HOSTS set up

The local.com domain already exist, so we need to overwrite the IP address where it points to be the local computer in the HOSTS file. We do this by editing the HOSTS file which on Windows is located in the following directory:

C:\WINDOWS\system32\drivers\etc

The HOSTS file has no file extension. So open it up in Notepad and add the following line at the very bottom:

127.0.0.1 www.local.com community.local.com support.local.com

Save the file, but make sure you don’t add a .txt extension.

You will need to close all your browser windows and in some cases restart the computer for the changes to take effect.

Open a new browser window and try to navigate to www.local.com, you should now get the WAMP welcome screen.

Create a database in MySQL

Using which ever MySQL admin tool you prefer, create a new database to use for Drupal. If using WAMP, you can use PHPMyAdmin by navigating to http://localhost/phpmyadmin/

By default MySQL has no password in WAMP.

Download and set up Drupal

Download the latest version of Drupal 6 which at the time of writing is Drupal 6.17.

Extract the files and subfolders to the C:\wamp\www directory overwriting the existing index.php when asked. If you are not asked to overwrite the index.php file, you have tried to extract the files at the wrong directory level.

Navigate to the /sites/ directory and copy the default directory twice and rename the 2 new directories support.local.com and community.local.com.

Navigate in your browser to www.local.com, you should get the Drupal installation screen. Follow the screens through and install Drupal how you normally would. Once installation has finished and you are at the Welcome screen, navigate to support.local.com.

During installation, add a database prefix as support_ and continue with installation.

Once at the welcome screen, do the same with community.local.com but this time using the prefix comm_.

Configure Drupal for multisite by modifying settings.php

In the /sites/ directory there are now 4 directories.

- all
- community.local.com
- default
- support.local.com

The default directory is used for www.local.com, the other 2 xxx.local.com directories should be self explanitory and the all directory is present for modules and themes.

Open up the settings.php file in the default directory.

Around a quater of the way down the file (around line 93 for most people) there is a line for $db_prefix, if you didn’t set one it should just say:

$db_prefix = '';

Scroll 3 quaters down (around line 169) and uncomment the line:

# $cookie_domain = 'example.com';

And change to:

$cookie_domain = '.local.com';

This will share the session cookie across all subdomains of local.com

Save and close the file.

Now open up settings.php from within the community.local.com directory.

Scroll down to line 93 and replace with the following:

$db_prefix = array(
'default' => 'comm_',
'users' => '',
'sessions' => '',
'role' => '',
'authmap' => '',
'users_roles' => '',
'profile_fields' => '',
'profile_values' => '',
);

Save and close the file.

And again, scroll 3 quaters down (around line 169) and uncomment the line:

# $cookie_domain = 'example.com';

And change to:

$cookie_domain = '.local.com';

Repeat the same for support.local.com, although this time set the default $db_prefix to support_.

Save and close the file.

Navigate to each site in the browser and log out of each site. Now log in to www.local.com. Now if you navigate to support.local.com, you should be logged in, same with community.local.com.

We now have 3 sites with a shared user base and a single signon. As you may be able to tell from the $db_prefix array above, we are sharing the users table as well as sessions which keeps the user data and log in data the same across all the sites. We also share the role and users_roles tables to be able to set multi-sitewide roles as well as the profile tables so that the user doesn’t have to type any information over and over again.

Cleaning up unneeded MySQL tables

Now in our database, we have redundant tables which aren’t doing anything and aren’t being used. We can now delete these to keep the database a bit tidier. Delete the following tables:

  • support_users
  • support_sessions
  • support_role
  • support_authmap
  • support_users_roles
  • support_profile_fields
  • support_profile_values
  • comm_users
  • comm_sessions
  • comm_role
  • comm_authmap
  • comm_users_roles
  • comm_profile_fields
  • comm_profile_values