Drupal 7 and sites.php

9. January 2012 - 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!

 

AttachmentSize
sites.php.zip1.43 KB

Next post

Motivation for both Body and Mind

Read More »

Comments

10. January 2012 - 18:01 - BarisW (not verified)

But how does this relate to the settings.php? We use a different database for dev, staffing and production. Can this be handled in sites.php as well?

10. January 2012 - 20:46 - Marcel (not verified)

I simply use a switch statement to include all the db credentials in settings.php Also quite handy for other settings (e.g. caching always off on dev and always on on prod)

Sampo Turve's picture

10. January 2012 - 20:53 - Sampo Turve

That sounds pretty cool. Wanna share any examples with multiple different settings between sites on settings.php? I'd love to see.

6. February 2012 - 23:22 - Marcel (not verified)

It simply looks like this (D6, but you get the point for a D7 site). I normally store this in a separate file (not in settings,php) and refer to it from within settings.php. This way I can even store the credentials outside the document root, making it somewhat safer in case the webserver fails to protect my files. It's also convenient to switch off caching / aggregating in the dev or test environments, to disable analytics in non-production, and so on. I have not tried yet the multisite config, but I don't see any reasons why that shouldn't work.

switch($_SERVER['HTTP_HOST']) {
// development
case 'sitename.local':
$db_url = 'mysql://<username>:<password>@<hostname>/<dbname>';
$db_prefix = '';
$conf['environment_indicator_text'] = 'DEVELOPMENT SERVER';
$conf['environment_indicator_color'] = 'dark-red';
$conf['environment_indicator_enabled'] = TRUE;
break;

// Test
case 'test.domain.tld':
$db_url = 'mysql://<username>:<password>@<hostname>/<dbname>';
$conf['environment_indicator_text'] = 'TEST SERVER';
$conf['environment_indicator_color'] = 'orange';
$conf['environment_indicator_enabled'] = TRUE;
break;

// Accept
case 'accept.domain.tld':
$db_url = 'mysql://<username>:<password>@<hostname>/<dbname>';
$db_prefix = '';
$conf['environment_indicator_text'] = 'ACCEPT SERVER';
$conf['environment_indicator_color'] = 'green';
$conf['environment_indicator_enabled'] = TRUE;
break;
// Production
default:
case 'domain.tld':
case 'www.domain.tld':
$db_url = 'mysql://<username>:<password>@<hostname>/<dbname>';
$db_prefix = '';
$conf['environment_indicator_text'] = '';
$conf['environment_indicator_color'] = 'transparent';
$conf['environment_indicator_enabled'] = FALSE;
break;
}

Add new comment