Pass environment variables to the PHP CLI and FPM

6,993

From the docs:

By default, nginx removes all environment variables inherited from its parent process except the TZ variable.

As you mentioned, you've tried setting them in the fastcgi config, which is, I think, the best you can do in this situation. For others' benefit this is done like this:

location ~ \.php$ {

    # ...
    fastcgi_param APPLICATION_ENV "production";
    fastcgi_param MY_OTHER_ENV "things";
    include fastcgi_params;

    # ...
}

I understand the aversion to 'copy-paste', and agree! You should look at using a configuration management tool such as Puppet, SaltStack, Ansible to manage your config files. That way you can easily sync your environment variable list between all required locations. Let me know if you need more info about this.

Share:
6,993

Related videos on Youtube

Kolyunya
Author by

Kolyunya

Developer

Updated on September 18, 2022

Comments

  • Kolyunya
    Kolyunya over 1 year

    I want to use environment variables in my PHP applications both CLI and FPM. What I do is I:

    • export some variables in /etc/environment.
    • configure both php.ini (CLI and FPM) to variables_order = "EGPCS".
    • configure FPM www.conf to clear_env = no

    What I expect is that environment variables are available both in CLI and FPM application. In fact that works only for CLI. FPM's $_ENV does not contain those environment variables.

    I noticed that it's possible to define environment variables in www.conf but it's inconvenient since I have to copy-paste all variables from /etc/environment and always keep two files in sync.

    The question is: is it possible to pass all environment variables directly to a PHP-FPM application without copy-pasting them in www.conf?

    • Admin
      Admin about 7 years
      Does getenv('varname') return anything?
    • Admin
      Admin about 7 years
      I'm using nginx. I tried setting fastcgi_params in server config, it kinda works, but it's still a copy-pasting of variables.
    • Admin
      Admin over 6 years
      What linux distro are you on? Not all distros source /etc/environment when starting FPM.
  • Kolyunya
    Kolyunya about 7 years
    I guess there's no way to disable this environment cleaning feature of nginx?
  • Joe Niland
    Joe Niland about 7 years
    I don't believe so without recompiling a custom build
  • Sagi Mann
    Sagi Mann almost 6 years
    but how to do something like fastcgi_param APPLICATION_ENV = $APPLICATION_ENV? (i.e. without copying the actual value of the environment variable from the operating system info the file)?
  • Joe Niland
    Joe Niland almost 6 years