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?

  • Does getenv('varname') return anything?
    – Joe Niland
    Apr 13 '17 at 7:47
  • @JoeNiland it returns false for me. Can you confirms it works as I expect it on your system?
    – Kolyunya
    Apr 13 '17 at 9:36
  • Are you using nginx or Apache?
    – Joe Niland
    Apr 13 '17 at 9:52
  • I'm using nginx. I tried setting fastcgi_params in server config, it kinda works, but it's still a copy-pasting of variables.
    – Kolyunya
    Apr 13 '17 at 11:12
  • What linux distro are you on? Not all distros source /etc/environment when starting FPM.
    – Kyle
    Dec 8 '17 at 20:58

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.

  • I guess there's no way to disable this environment cleaning feature of nginx?
    – Kolyunya
    Apr 18 '17 at 9:16
  • I don't believe so without recompiling a custom build
    – Joe Niland
    Apr 18 '17 at 11:35
  • 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)?
    – Sagi Mann
    Jun 24 '18 at 14:48
  • @SagiMann See stackoverflow.com/questions/21866477/…
    – Joe Niland
    Jun 25 '18 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.