Run multiple sites from the same code base (multi-site)

Last modified: July 3, 2009 - 19:16

If you are running more than one Drupal site, you can simplify management and upgrading of your sites by using the multi-site feature. Multi site allows you to share a single Drupal installation (including contributed modules and themes) among several sites. This is particularly useful for managing the code since each upgrade only needs to be done once.

Each site would continue to have its own database and its own configuration settings so each site would have entirely different content and you can still specify the theme, modules and other settings for each site.

Configuring a new site

To create a new site using a shared code base you must complete the following steps:

  1. Create a new database for the site.
  2. Create a new subdirectory of the 'sites' directory with the name of your new site.
  3. Copy the file sites/default/default.settings.php into the folder you created in the previous step. Rename the new file to settings.php.
  4. Adjust the permissions of the new site directory, and grant write permissions on the configuration file
  5. In a Web browser, navigate to the URL of the new site and continue with the standard Drupal installation procedure.

It may also be necessary to modify your Web server's configuration file (often named httpd.conf for Apache) to allow Drupal to override Apache's settings. This is true for all installations of Drupal and is not specific to the multi-site install. Additional information is available in the Best Practices: Configuring Apache and PHP for Drupal in a Shared Environment section of the Install Guide.

Domains and URLs

The new directory name is constructed from the site's URL. The configuration for www.example.com would be in sites/example.com/settings.php. You do not need to include 'www' as part of the directory name.

Any sub-domain of example.com, including www, is served from that directory's settings unless there is an alternative, matching sub-domain sites directory. Installing a separate site in a sub-folder is also possible. For example, example.com, sub.example.com, and sub.example.com/site3 can all be defined as independent Drupal sites using the following directory setup:

sites/example.com/settings.php 
sites/sub.example.com/settings.php 
sites/sub.example.com/site3/settings.php

Installing Drupal in a sub-folder is accomplished by adding a dot after the domain name and then the name of the sub-folder, as in the site3 example above.

If you are installing on a non-standard port, the port number is treated as the deepest sub-domain. For example:
http://www.example.com:8080could be loaded from sites/8080.example.com. The port number will be removed according to the pattern above if no port-specific configuration is found, just like a real sub-domain.

Domain-specific modules and themes

Each site configuration can have its own site-specific modules and themes in addition to those installed in the standard 'modules' and 'themes' directories. To use site-specific modules or themes, simply create a 'modules' or 'themes' directory within the site configuration directory. For example, if sub.example.com has a custom theme and a custom module that should not be accessible to other sites, the setup would look like this:

sites/sub.example.com/settings.php 
sites/sub.example.com/themes/custom_theme 
sites/sub.example.com/modules/custom_module

Document root

One area of frequent confusion is that in a Drupal multisite installation the webserver document root is the same for all sites. For example with the following three sites: example.com, sub.example.com and example.com/site3 there will be a single Drupal folder and all sites will be calling the same index.php file.

Some webhosts automatically create a folder (i.e. example.com) when creating a new domain. In this case it is necessary to create a symbolic link to the main Drupal folder.

Localhost

On many systems it is possible to create entries in a "hosts" file to create aliases for the localhost name for a local workstation. By creating aliases for localhost it is possible to create names such as localdev1.example.com and localdev2.example.com, both for the local computer.

Domain name changes

Once a site is in production under the folder sites, it should not be renamed, even if the Web site URL changes. This is because several database tables (for example: system, and files) include references to "sites/www.mydomain.com." Of course, instead of renaming the sites directory, you can create a symlink to the new URL from the old one. Navigate to the sites directory and then use the following command:
$ ln -s /path/to/drupal/sites/old.domainname.com new.domainname.com

 
 

Drupal is a registered trademark of Dries Buytaert.