Getting environment variables in PHP-FPM with Nginx

16,838

Solution 1

You can set the environment variable in /etc/php/php-fpm.d/www.conf like this: env[APP_ENV] = development Then you'll be able to get it with getenv('APP_ENV') like you expected.

Solution 2

When you type printenv or php test.php, you see environnement variables because they exist.

When you "try to access the file via HTTP… there is nothing in [your] env". Exactly your environnement variables are not set.

Why would you expect a different behaviour? Files like /etc/environment, /etc/profile and /etc/bashrc are only sourced when you use a shell, not when a daemon is ran.

Share:
16,838

Related videos on Youtube

chindit
Author by

chindit

Updated on September 18, 2022

Comments

  • chindit
    chindit over 1 year

    I've defined some environment variables like APP_ENV in my /etc/environment file, on my ArchLinux.

    If I type printenv, I see them.

    I've created this simple test file called… test.php

    <?php
    
    var_dump(getenv('APP_ENV'));
    var_dump(getenv());
    

    If I run php test.php, everything is OK, I see my ENV variables.

    But when I try to access the file via HTTP… there is nothing in my env!

    Of course, I've changed the config of /etc/php/php-fpm.d/www.conf to set clear_env = no

    These are the affected lines:

    ; Clear environment in FPM workers
    ; Prevents arbitrary environment variables from reaching FPM worker processes
    ; by clearing the environment in workers before env vars specified in this
    ; pool configuration are added.
    ; Setting to "no" will make all environment variables available to PHP code
    ; via getenv(), $_ENV and $_SERVER.
    ; Default Value: yes
    clear_env = no
    

    And I've restarted both php-fpm and nginx services but… still nothing in my env. Script return bool(false).

    So… Am I missing something ?

    This is my php-fpm version:

    php-fpm --version
    PHP 7.2.6 (fpm-fcgi) (built: May 26 2018 07:45:18)
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    

    And my Nginx version

    nginx -v
    nginx version: nginx/1.14.0
    

    What should I do to access my env variables in a PHP-FPM context ?

    Thanks a lot!

    • NickNo
      NickNo almost 6 years
      I am experiencing the same problem.
  • samayo
    samayo over 4 years
    This works, but I don't think the www.conf file is meant to be used like that
  • ben.IT
    ben.IT about 3 years
    how would you centralize env var to use both for php cli scripts and php scripts executed by a webserver?
  • Micah Henning
    Micah Henning about 2 years
    This blows my mind. I'm not going to put my secrets into a web config file!
  • Jesús Bocanegra
    Jesús Bocanegra about 2 years
    I agree. Sensitive information shouldn't go there and my suggestion to add it to a .conf file was a quick way to achieve what they needed because it was a server related setting (APP_ENV).I suspect that the original problem is related to which user PHP-FPM is running as
  • Jesús Bocanegra
    Jesús Bocanegra about 2 years
    The PHP-FPM user is probably not sourcing the environment variables. I can't really come up with another way of doing this. I usually use Kubernetes, which injects the env vars from the pod definition and they do work with all users, in my experience. One thing you can try is to modify the systemctl service to source and export the variables before launching the server, but it's rather hacky.