Customize Drupal's settings.php file for Safer and More Streamlined Development

Have you ever started clicking around in Drupal's back-end on one of your development sites, making little changes, maybe running an experiment or two, and then discovered you had somehow gotten switched over to the live version of the site? There's nothing like that panic feeling when you discover you're changing a live site when you thought you were on a development site.

Yes, it can happen. At least one site I've taken over in recent memory has had hard-coded links. Hard-coded links don't change when the site is pulled off the live server into development. Click one of these little land mines and you'll find yourself on the live site, and if your session is still alive on the live site you can do all sorts of damage and be none the wiser until you look at the URL in your browser's address field.

Fortunately this can be avoided very easily on any Drupal site; and you don't even need a fancy module to do it. Just add a few lines of code to your sites' settings.php file and you're good to go:

<?php
if ($_SERVER['SERVER_NAME'] == 'example.dev') {
   
$conf['site_name'] = 'Example.com Development Site';
   
$conf['file_temporary_path'] = '/tmp';
   
$conf['error_level'] = 0;
}
?>

What it does
The code above tests whether the SERVER_NAME matches your local Drupal development image of your site. SERVER_NAME is the same value you set in your hosts file and in the VirtualHost directive for the site in your httpd.conf file. If the values match we'll override a few important configuration variables to assist with development:

  • Change the name of the site to something that's obviously not the site name shown on the live site
  • Change the temporary file path to an appropriate tmp directory. You may or may not need this, depending on where on your local machin you host your Drupal development sites.
  • Tell Drupal to go ahead and write errors to the screen, rather than just log them. After all we're in development so we want to know when we have an error in our code, right?

So why do this? Well for one thing it's easy to do. With a quick copy/paste and a few keystrokes you can have this up and working. You don't need to download and install a module, so it's one less thing to maintain. It can live anywhere, even on a live server, and it won't affect a thing because it won't go into effect in your live environment. And maybe best of all, it doesn't change any of your live site's settings. The database values remain unchanged by this method.

But maybe the best single reason for doing it is to greatly reduce the risk of damaging a live site you got onto by pure accident.

Powered by Drupal, an open source content management system