Allow configuring the environment via an environment variable

Created on 7 June 2015, over 10 years ago
Updated 8 September 2025, about 2 months ago

Problem/Motivation

Something which is pretty common in other frameworks and standalone applications is getting the current environment from an environment variable. Rather than a hardcoded 'prod' we should be able to easily configure this value from the environment.

Proposed resolution

A call to getenv() will give us a value, I would suggest 'DRUPAL_ENV' maybe? So you could set this in the following ways:

- In your actual environment: export DRUPAL_ENV=staging
- Apache: SetEnv DRUPAL_ENV staging
- Nginx: fastcgi_param DRUPAL_ENV staging
- Using putenv() (settings.php etc..)
- Using Symfony's dotenv component or phpdotenv library

Everything else is in place to dump an environment specific container. The 'kernel.environment' variable will also be available to use (already there too).

So E.g. you could set this in a test like: putenv('DRUPAL_ENV=test') too.

Remaining tasks

Document the new DRUPAL_ENV environment variable.

User interface changes

None.

API changes

Facilitates an environment variable: DRUPAL_ENV

📌 Task
Status

Active

Version

11.0 🔥

Component

base system

Created by

🇬🇧United Kingdom damiankloip

Live updates comments and jobs are added and updated live.
  • Needs documentation

    A documentation change is requested elsewhere. For Drupal core (and possibly other projects), once the change has been committed, this status should be recorded in a change record node.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇱Netherlands kingdutch

    In my recent blogpost (Web Archive Link) I've outlined how this comes essentially for free with Use symfony/runtime for less bespoke bootstrap/compatibility with varied runtime environments Active while providing end-users an easy way to swap out the logic if needed.

    [The Symfony Runtime Component usage] provides us with a standardised way to get the environment name regardless of what kind of application you're building. For the initial Runtime implementation we may even resolve the $environment variable statically to prod to mimic the current behavior. That would allow work to be done in [this issue] to determine the name of the variable that sets the environment and then provide this as an update to Drupal websites without having to touch their front-controllers again. Alternatively if they're unhappy with how Drupal determines the environment, then they can overwrite the runtime to resolve this variable themselves, without having to touch the front-controller.

Production build 0.71.5 2024