May
16
2011

Development Workflows With Different settings.php Across Environments

When working with larger websites, development can be tedious when dealing with settings.php changes across each environment. There's an way to solve this and streamline your workflow.

Share
Email

First off, with any site that is big enough to have a development, staging, and production environment you should be using a code revision system such as CVS, SVN, GIT, etc. I’d recommend a look into GIT first if you’ve never used one.

Applying The Settings Override

The following will work for any version of Drupal (I’ve tested with Drupal 5, Drupal 6, and Drupal 7). To begin, open your /sites/default/settings.php file and add this to the very bottom:

if (is_readable('./sites/default/settings_override.php')) {
 require_once 'settings_override.php';
}

Then create your settings_override.php file. Here’s a sample of what mine looks like (Drupal 7):

<?php
/**
 * This is an example configuration file.
 *
 * Copy this into /src/sites/default/ and change to suit your needs.
 */
// Set this to the base URL of your dev name eg. http://localhost:8000
// This should be automatic unless you want to manually set it for some reason.
$base_url = 'http://'.$_SERVER['HTTP_HOST'];
$databases = array (
  'default' =>
  array (
    'default' =>
    array (
        'driver' => 'mysql',
        'database' => 'dbname',
        'username' => 'username',
        'password' => 'pass',
        'host' => 'localhost',
        'port' => '',
        'prefix' => '',
    ),
  ),
);
//This is here in case you have multipule web servers on the same dev enviornment sharing the same files.
if ($base_url == 'http://localhost:8001') {
  $databases = array (
    'default' =>
    array (
      'default' =>
      array (
        'driver' => 'mysql',
        'database' => 'dbname',
        'username' => 'username',
        'password' => 'pass',
        'host' => 'localhost',
        'port' => '',
        'prefix' => '',
      ),
    ),
  );
}

Ignore or delete the last database configuration setup if it doesn’t apply to you. The reason this is there (in my setup) is because I often start websites in Apache when they’re younger and migrate them to Nginx sites on dedicated servers when the resource needs become larger. This allows me to do testing under each web server from the same local machine at the same time but still use a different database. Something you might need if one database is hosted locally under XAMPP/MAMP/WAMP and the other is hosted inside of a VMWare/Parelles development environment.

Also I’ve setup the base URL to be dynamic to the environment you’re currently in. This is nice in the case that you change you frequently change your VirtualHost/Server setup (change of port number perhaps) or your local host name mappings (C:\Windows\System32\drivers\etc\hosts). It also allows that the former point where we can do checks on the base URL to define a different database to be used.

Other Versions Of Drupal

The example is using the configuration setup from Drupal 7. Parameters might be slightly different depending on what version of Drupal you’re using. For example, the database configuration in D5/D6 is not an array and looks something closer to this (I’ll shorthand from my version above):

$base_url = 'http://'.$_SERVER['HTTP_HOST'];
$db_url = 'mysql://username:password@localhost/databasename';
if ($base_url == 'http://localhost:8001') {
 $db_url = 'mysql://username:password@localhost/databasename';
}

Tweak Repository Excludes

If you’re using a repository, make sure you exclude this override file. In GIT that means creating a .gitignore file in the root directory (has your .git folder) and adding this to it:

# Ignore dev settings.
src/sites/default/settings_override.php

There are different ways to do this depending on what you’re using. If you’re not using GIT, look at the documentation for whatever you’re using.

Consistency & Simplicity for Reproduction

After finishing you’ll want to do a few other things for consistency and simplicity for future use:

  • Add this same include code to the bottom of you default.settings.php
  • Create a default.settings_override.php to match your settings_override.php file (And mirror changes to it whenever you add/update it) so that you always have a default copy. This is for the circumstance that you create a clone of your repository, perhaps by another developer you’re working with.

Respond: Leave A Comment | Trackback URL

Entrupeners, Subscribe for the lastest tools, tips, and tutorials.


5 Responses to Development Workflows With Different settings.php Across Environments

  1. Paul Morris

    This was very helpful. One thing: The file name needs to be in quotes at the require_once line:

    require_once ‘settings_override.php’;

    Thanks again.

  2. Why use settings_override.php? Why not just include this code in settings.php?

    • Because when multiple developers work on the same site they may have different local username and database preferences that you wouldnt want to be keeping track of in a “production” settings.php file.

  3. Ricardo

    Hi,

    Thanks for the tip. Is this still working? I tried in a Drupal installation but it’s not working. I have 2 servers (one local and other on web, both for dev) i set the first $base_url to the web server and then the second to local, but the local server is trying to connect to DV with the first credentials.

    Any tip?

    Thanks again.

    • Make sure your required file is being loaded and is after all other code in settings.php. Also do a few echos on $_SERVER['HTTP_HOST'] to ensure it’s catching the if statement in your override file.

Leave a Reply

Custom Theme by Rob Malon | Content & Design © 2010 - RobMalon.Com - Chicago, Illinois