{
    "slug": "environment_variables_linux",
    "term": "Environment Variables",
    "category": "linux",
    "difficulty": "intermediate",
    "short": "Key-value pairs inherited by child processes — the standard way to pass configuration, credentials, and runtime settings to PHP applications without hardcoding.",
    "long": "Environment variables are set in the shell (export VAR=value), in process supervisors (systemd Environment=, Supervisor environment=), in Docker (ENV, --env), or in .env files loaded at application startup. In PHP: getenv('VAR'), $_ENV['VAR'], or $_SERVER['VAR']. PHP-FPM does not automatically inherit the server environment — use env[VAR] = $VAR in the FPM pool config or clear_env=no. Variables are process-scoped: a child process cannot modify the parent's environment.",
    "aliases": [
        "env vars",
        "getenv",
        ".env",
        "process environment"
    ],
    "tags": [
        "linux",
        "php",
        "devops",
        "security"
    ],
    "misconception": "PHP-FPM automatically inherits the server's environment variables — PHP-FPM clears the environment by default (clear_env=yes); vars must be explicitly passed in the pool config.",
    "why_it_matters": "Environment variables are the twelve-factor standard for configuration — understanding how they flow from OS to PHP-FPM to application prevents the common 'getenv() returns empty' mystery.",
    "common_mistakes": [
        "clear_env=yes (default) in PHP-FPM — getenv() returns empty for all system env vars.",
        "Storing secrets in .env files committed to git — .env must be in .gitignore.",
        "Using putenv() to set vars in PHP — only affects the current process, not parent or sibling processes.",
        "Not restarting PHP-FPM after changing environment variables — processes inherit env at startup only."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "dotenv",
        "hardcoded_credential",
        "secrets_management",
        "php_fpm"
    ],
    "prerequisites": [
        "linux_processes",
        "configuration_management",
        "php_ini"
    ],
    "refs": [
        "https://www.php.net/manual/en/function.getenv.php"
    ],
    "bad_code": "; PHP-FPM default — clears all environment:\n; /etc/php-fpm.d/www.conf:\n; clear_env = yes  (default)\n\n; PHP code:\necho getenv('DB_HOST'); // Returns false/empty!\n// Mystery: var is set in /etc/environment but FPM cleared it",
    "good_code": "; Option 1 — explicit pass-through in FPM pool:\nclear_env = no  ; Pass all environment vars through\n\n; Option 2 — explicit declarations (more secure):\nenv[DB_HOST] = $DB_HOST\nenv[DB_PASS] = $DB_PASS\nenv[APP_ENV] = production\n\n; PHP:\n$host = getenv('DB_HOST');  // Now works\n$host = $_ENV['DB_HOST'];   // Also works\n$host = $_SERVER['DB_HOST']; // Also works",
    "quick_fix": "Set PHP environment variables in the PHP-FPM pool config (env[APP_ENV] = production) or system-level via /etc/environment — they're inherited by all PHP-FPM child processes automatically",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-04-05",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/environment_variables_linux",
        "html_url": "https://codeclaritylab.com/glossary/environment_variables_linux",
        "json_url": "https://codeclaritylab.com/glossary/environment_variables_linux.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Environment Variables](https://codeclaritylab.com/glossary/environment_variables_linux) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/environment_variables_linux"
            }
        }
    }
}