09 January 2012

Drupal 7 and sites.php

Profile picture for user sampo.turve

You might have seen the example.sites.php file on the sites directory when setting up your Drupal installation. This is your new friend when setting up a multisite installation on Drupal.

The the g' old way of doing this was to create symlinks for each of the site into the sites directory. Like this:

Multisite installation with aliases

This is a typical installation of a multisite with a development, staging and live site for each site:

  • Local development sites for developers (.dev domains)
  • Staging server sites for staging (stagingsite1/2/3.mearra.com)
  • Live server sites for, yeah, live sites (livesite1/2/3.mearra.com)

As you can see, this doesn't look nice and has it's flaws. For example, let's take PhpStorm IDE: the lovely editor is automatically assuming that you have 12 (twelve!) different sites directories that needs the indexing. That goes without saying, the indexing might take a lot of a time.

But the sites.php is the cure for this. You can just copy the example.sites.php file into sites.php or create a new file. Then add something like this in it:

$sites = array(
// Development sites
'devsite1.dev' => 'site1',
'devsite3.dev' => 'site2',
'devsite3.dev' => 'site3',

// Staging sites
'stagingsite1.mearra.com' => 'site1',
'stagingsite2.mearra.com' => 'site2',
'stagingsite3.mearra.com' => 'site3',

// Live sites
'livesite1.mearra.com' => 'site1',
'livesite2.mearra.com' => 'site2',
'livesite3.mearra.com' => 'site3',

Now your sites directory looks way more cleaner and you don't have to worry about any of the common problems caused by symlinks.

Multisite installation with sites.php

And you're all set!