Quick Description

This project implements code to make Drupal's configuration (settings.php) environment-dependent, so that you can have different sets of configurations in dev, staging and production.

Common (Error-prone) Solution

Commonly Drupal projects do not check-in settings.php and create a set of files like: settings-dev.php, settings-stage.php, settings-prod.php, pointing symlink of settings.php to the proper file in each environment. Such approach is not ideal because:

  • Un-versioned (probably ignored) symlink settings.php is not in version control and is different in each environment, but is intermingled with versioned files. This is error-prone.
  • There is no shared configuration, a lot of settings in various settings files are the same, but they are duplicated, if you make a change in one shared setting - you will have to update all files.

Alternative Approach Implemented by Env Project

  • Put the configuration common to all environments in settings.php
  • Create environment-specific configuration files like: dev.inc, stage.inc, prod.inc etc. which have same exact syntax as settings.php but can override or add to the configuration in settings.php, under sites/default/environments folder
  • Check everything in version control!

Installation/Configuration

  1. Download the environments.inc file from this project and place it under sites/default/ folder right next to the settings.php
  2. At the very end of settings.php require environments.inc with the following code:
    require (__DIR__ . '/environments.inc');
    
  3. Create folder: sites/default/environments
  4. In your environment (server) do the following (following code assumes your environment is called "dev"):
    1. In your virtualhost declare a server variable, e.g. for Apache:
      SetEnv DRUPAL_ENV_NAME dev
    2. If you need command-line tools like Drush to pick-up proper configuration, in your .profile or .bash_rc or .bash_profile script put the following:
      export DRUPAL_ENV_NAME="dev"
  5. Create sites/default/environments/dev.inc and override any settings you need to override for that environment, previously set by settings.php. Most probably it will be MySQL connection properties, but could be many other things.
  6. Put all shared configuration in settings.php, override environment-specific variations in an environment-specific file like dev.inc

Getting current environment from code

If, in addition to customizing settings.php you need to make parts of your module code dependent on the current environment (be careful with this: some users may not have env module installed?), you can get current environment by referring to: DrupalEnvironment::getCurrent();

Env and Domain Access Module

If you are experiencing a problem using the two together, see: Issue #1765786

Credits

Initial code contributed by NPR

See also

  • http://drupal.org/project/environment is a project with similar intent, but it takes a drush-centric approach and does not target/solve the problem of configuration in settings.php specifically.

Project information

  • Created by irakli on , updated
  • shieldStable releases for this project are covered by the security advisory policy.
    Look for the shield icon below.

Releases