Skip to content
← Back to articles

Article

12 factors: config outside the code

Every team has seen a commit with the message “remove password”. The problem is that Git does not forget: the password is still in the history, within reach of anyone who clones the repository. And it almost always got there for the same reason: someone wrote the config straight into the code.

This is the third article in the Twelve factors in practice series. The first one covered factor I, codebase. The second one covered factor II, dependencies. Now it is time for factor III, config: what it is, where it should live and how to read it in plain PHP, with no framework.

Before we start: three words

What factor III says

The original text is direct: the app keeps its config separate from the code, in environment variables.

The idea behind it is that the code is the same in every deploy. Production, staging and the developer’s machine run the same code. What changes between them is only the config. If the config is inside the code, each deploy needs different code, and factor I stops holding.

An analogy helps. Think of the phone charger you take on a trip. The charger is the same in any country. What changes is the plug adapter. Nobody opens the charger to solder a new plug on every trip: they swap the adapter. The code is the charger, the config is the adapter.

What is config, and what is not

Not everything in a file called config is config in the factor III sense. The question that tells them apart is: does this change from one deploy to another?

Config (changes per deploy)Not config (same in every deploy)
Database address and passwordThe app routes (/login calls LoginController)
Payment API keyWhich class implements which interface
Mail server addressValidation rules for a form
Log level, cache timeDatabase table names

The right side is internal app config. It can and should stay in the code, versioned in Git, because it is part of how the app works. If the routes changed between production and staging, you would be testing a different app from the one that goes live.

The open source test

The original text suggests a simple test: could the code be made open source right now, without leaking any credentials?

If the answer is “no”, some config is in the wrong place. The lab for this article has an example of the wrong way, a file with the password written in the code:

<?php
// Wrong way: config written in the code.
$config = [
    'db_host'     => 'localhost',
    'db_password' => 'super-secret-123',
    'mail_from'   => 'noreply@example.com',
];

The lab also has a .env file with another password, only for local development (more on it later). The test searched for both passwords in everything Git tracks, with git grep:

$ git grep -n -E 'super-secret-123|local-only-pass'
src/01-hardcoded.php:5:    'db_password' => 'super-secret-123',

The password written in the code showed up. The one in .env did not, because that file is in .gitignore and never enters the repository. The goal is for this search to find nothing at all. Tools like gitleaks run this search automatically, with patterns for well-known passwords and keys, and can run in CI on every commit.

Reading environment variables in PHP

PHP offers three ways to read an environment variable: the getenv() function, the $_ENV array and the $_SERVER array. They look the same, but the lab showed they are not.

The test ran a script that reads DB_HOST in all three ways:

$ DB_HOST=db.internal php src/02-getenv-vs-env.php
variables_order=GPCS
getenv(DB_HOST)='db.internal'
$_ENV[DB_HOST]=NULL
$_SERVER[DB_HOST]='db.internal'

$_ENV came back empty. The reason is a php.ini directive called variables_order, which says which arrays PHP fills in. The default in common installs, like the Debian one used in the lab, is GPCS, and the letter E (for environment) is not there. With the letter back, all three agree:

$ DB_HOST=db.internal php -d variables_order=EGPCS src/02-getenv-vs-env.php
variables_order=EGPCS
getenv(DB_HOST)='db.internal'
$_ENV[DB_HOST]='db.internal'
$_SERVER[DB_HOST]='db.internal'

In practice: to read straight from the environment, getenv() is the most predictable path. Anyone using $_ENV needs to check the server’s variables_order.

One build, two environments

With reading sorted out, the lab built a config.php that takes everything from the environment, with defaults for what is optional:

<?php
return [
    'db_host'     => env_required('DB_HOST'),
    'db_password' => env_required('DB_PASSWORD'),
    'log_level'   => env_optional('LOG_LEVEL', 'info'),
    'cache_ttl'   => (int) env_optional('CACHE_TTL', '60'),
];

The env_required() and env_optional() functions are small and appear in the next section. The same code ran twice, first with development values and then with production values:

$ DB_HOST=localhost DB_PASSWORD=dev-pass LOG_LEVEL=debug php src/03-app.php
connecting to localhost
password length=8
log_level=debug cache_ttl=60

$ DB_HOST=db.prod.internal DB_PASSWORD=Pr0d-s3cret CACHE_TTL=3600 php src/03-app.php
connecting to db.prod.internal
password length=11
log_level=info cache_ttl=3600

No file was edited between the two runs. Only the environment changed, and with it the database, the log level and the cache time. Notice also that the app prints only the password length, never the password. Secret config does not show up in logs.

Diagram: the same code, one build with no passwords, runs in two deploys. With the dev env, DB_HOST=localhost, it becomes the dev deploy, with a local database and debug log. With the prod env, DB_HOST=db.prod.internal, it becomes the production deploy, with the production database and info log. The code goes to Git; the values live in the environment of each deploy and never in the repository

Fail fast when something is missing

What if someone forgets to set the password in production? The worst outcome is the app starting, looking healthy and only breaking on the first request that needs the database, minutes or hours later.

That is why the lab’s config.php separates what is required from what is optional:

<?php
function env_required(string $name): string
{
    $value = getenv($name);
    if ($value === false || $value === '') {
        fwrite(STDERR, "Config error: required environment variable {$name} is not set\n");
        exit(1);
    }
    return $value;
}

function env_optional(string $name, string $default): string
{
    $value = getenv($name);
    return ($value === false || $value === '') ? $default : $value;
}

The test ran the app without the password and then with an empty password:

$ DB_HOST=db.prod.internal php src/03-app.php
Config error: required environment variable DB_PASSWORD is not set
[exit 1]

$ DB_HOST=db.prod.internal DB_PASSWORD= php src/03-app.php
Config error: required environment variable DB_PASSWORD is not set
[exit 1]

In both cases the app stopped right away, with a message that says exactly what is missing, and with exit code 1. That code matters: it is what tells the deploy, the CI or the orchestrator that something went wrong, so the new version does not go live. This behavior has a name: fail fast.

The .env file, only for your machine

Setting five or ten variables by hand every time you open a terminal gets old. For local development, the common answer is a .env file at the project root, read by a library. In PHP, the most used one is phpdotenv, installed with Composer:

composer require vlucas/phpdotenv

The lab used it like this:

<?php
require __DIR__ . '/../vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
$dotenv->safeLoad();
$dotenv->required(['DB_HOST', 'DB_PASSWORD'])->notEmpty();

echo 'DB_HOST=' . $_SERVER['DB_HOST'] . "\n";

Three details in this code:

The test showed all three behaviors:

$ php src/04-dotenv.php
DB_HOST=127.0.0.1

$ DB_HOST=db.from.real.env php src/04-dotenv.php
DB_HOST=db.from.real.env

$ php src/04-dotenv.php        # no .env and no variables
PHP Fatal error:  Uncaught Dotenv\Exception\ValidationException: One or more
environment variables failed assertions: DB_HOST is missing, DB_PASSWORD is missing.

First, the value came from .env. Then the real variable beat the file. Finally, with neither, the validation stopped the app.

A trap showed up along the way. The first version of the script read $_ENV['DB_HOST'], and when the variable came from the real environment, this was the result:

$ DB_HOST=db.from.real.env php src/04a-dotenv-env-trap.php
PHP Warning:  Undefined array key "DB_HOST" in .../src/04a-dotenv-env-trap.php on line 9
DB_HOST=

It is variables_order again: PHP did not put the real variable in $_ENV, and immutable mode stopped the library from putting it there. Reading from $_SERVER, as in the final version, fixed it.

What goes to Git and what does not

.env holds real values, so it never goes to the repository. A .env.example goes instead, with the variable names and sample values, and no secrets:

# .env.example: copy to .env for local development. Never commit .env.
DB_HOST=127.0.0.1
DB_PASSWORD=
LOG_LEVEL=debug

And .gitignore takes care of the rest:

/vendor/
.env

In the lab, Git confirmed it:

$ git check-ignore -v .env
.gitignore:2:.env	.env

$ git ls-files | grep -E '^\.env'
.env.example

.env.example is also documentation: whoever joins the project sees, in one place, every setting the app needs. Frameworks like Hyperf ship with this setup ready, with a .env read at startup and an env() function to use in config files.

Per environment or per variable?

A common way to organize config is one file per environment:

<?php
// config/production.php
return [
    'db_host'   => 'db.prod.internal',
    'log_level' => 'info',
    'cache_ttl' => 3600,
];

Plus a config/staging.php, and a config/development.php. The app picks the file through a single variable, like APP_ENV=production.

The original text advises against this setup, for two reasons:

  1. It does not scale. Each new environment, like a staging deploy for one specific client, becomes another file. Over time, nobody knows which differences between them are on purpose.
  2. It pulls secrets into the code. The production file is the natural place for someone to put the production password, and then it goes to Git.

The factor III way is granular variables: one variable per thing (DB_HOST, LOG_LEVEL, CACHE_TTL), independent from each other. Each deploy sets its own, with no environment name tying them together. This example is only illustrative and was not run in the lab.

The PHP-FPM trap

So far, everything ran from the command line. On the web, PHP usually runs through PHP-FPM, a manager that keeps PHP processes ready to handle the requests coming from the web server. And PHP-FPM has a setting that catches many people off guard: clear_env.

With clear_env = yes, the default, PHP-FPM wipes the environment variables before handing the request to PHP. Your app reads getenv('DB_HOST') and gets false, even with the variable set on the server.

The lab tested it with a real php-fpm 8.4: it started PHP-FPM with APP_SECRET and DB_HOST set and made a request to a page that only prints both. With the default config:

getenv(APP_SECRET)=false
getenv(DB_HOST)=false

There are two ways out. The first is to turn the wipe off in the pool file (on Debian, /etc/php/8.4/fpm/pool.d/www.conf):

clear_env = no

The second, more controlled, is to keep the wipe and pass only the variables the app needs, one by one:

env[APP_SECRET] = $APP_SECRET
env[DB_HOST] = $DB_HOST

In both cases, the same request started seeing the values:

getenv(APP_SECRET)='from-shell'
getenv(DB_HOST)='db.prod.internal'

The second option is usually the better one: the app gets only what it needs, not every variable on the server. One detail: the test used a minimal pool started by hand, and the requests went straight over the FastCGI protocol with cgi-fcgi, with no web server in front.

And in production, where do the values come from?

Outside your machine, the platform where the app runs sets the variables:

Docker was not available on the lab machine, so none of these examples ran here. The idea is the same in all of them: there is one image or one codebase, and the values come in from outside at run time.

One last caution: an environment variable is not a vault. A forgotten phpinfo() call on a public page shows all of them. Secrets do not appear in logs, error pages or phpinfo().

How to tell if your project follows factor III

A few quick questions:

If any answer is “no”, that is a good place to start. And there is an ownership angle here too. When config is explicit, with a .env.example that lists everything and an app that complains about what is missing, anyone on the team can bring up a new environment, rotate a leaked password or understand why production behaves differently. Config stops being a secret held by whoever did the first deploy and becomes the team’s responsibility.

Next article

The next article in the series covers factor IV, backing services: why the database, the queue and the cache should be treated as attachable resources, swapped by changing only the config.

Glossary

Sources

Share LinkedIn WhatsApp X

Get new articles by email

One email when a new article comes out, in English and Portuguese. No spam, unsubscribe anytime.

Comments