About IT and Sports

My Web development experiences and sports encounters

Flower

Posts Tagged ‘setting.php’

Handling settings.php in Drupal

When working with Drupal there is a moment where you have to think about the way you want to manage settings.php. This file contains important configuration settings like the database credentials. It is most often also used to add custom configuration settings. Drupal comes with a default version of this file and during installation Drupal will write the database settings to this file.

The problem
Many projects run in a DTAP (Development, Testing, Acceptance and Production) development process, using a SCM tool like git or svn in a multi developer and multi location environment. The following problems/requirement should have your attention:
1. Each DTAP environment have their own specific settings as does every developer. For instance the database settings. Changes to settings.php will probably lead to SCM conflicts.
2. From a security point of view you don’t want database passwords in version control. Especially when you are using an online service like github.com.
3. You want to be able to make environment specific settings (develop, test, acceptance, production). For instance when you have a Varnish instance which only runs on acceptance and production.
4. General configuration settings for all environments should be possible
5. You want to prevent working with local changes or unversioned files on any environments

The solution
To handle the requirement above, we have worked out way to achieve this:
- Database settings are taken out of settings.php and moved to a separate file env.settings.inc. This file is included from settings.php
- The file env.settings.inc is not added to version control. In git you can use .gitignore to keep it out of version control.
- In env.settings.inc a variable is included to define the environment:
<?php
$environment = “develop”; // Possible values: develop, acceptance, production

- Per environment a settings file is created: develop.settings.inc, etc. These files are included in settings.php and added to version control.
- General configuration settings which are needed for all environments are added to settings.php. Settings.php is also added to version control.

In settings.php we add the following:

<?php
if (file_exists (‘./’ .conf_path(). ‘/env.settings.inc’)) {
include_once(‘./’. conf_path(). ‘/env.settings.inc’);
}

if (file_exists (‘./’. conf_path(). ‘/’. $environment . ‘.settings.inc’)) {
include_once(‘./’. conf_path(). ‘/’. $environment . ‘.settings.inc’);
}

This solution meets all the requirements. It’s clean and and well-ordered. And most importantly it prevents a lot of irritations, because you don’t have conflicts on settings.php anymore. If you found any flaws in this configuration or if you have another strategy on settings.php, please share them.

This article was originaly posted on the Colours blog.