2

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?

Kolyunya
  • 217
  • 1
  • 3
  • 9

1 Answers1

2

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.

Joe Niland
  • 457
  • 1
  • 5
  • 16