Pass environment variables to the PHP CLI and FPM
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.
Related videos on Youtube
Comments
-
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) tovariables_order = "EGPCS"
. - configure FPM
www.conf
toclear_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 about 7 yearsDoes getenv('varname') return anything?
-
Admin about 7 yearsI'm using nginx. I tried setting
fastcgi_param
s in server config, it kinda works, but it's still a copy-pasting of variables. -
Admin over 6 yearsWhat linux distro are you on? Not all distros source
/etc/environment
when starting FPM.
- export some variables in
-
Kolyunya about 7 yearsI guess there's no way to disable this environment cleaning feature of
nginx
? -
Joe Niland about 7 yearsI don't believe so without recompiling a custom build
-
Sagi Mann almost 6 yearsbut 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 almost 6 years@SagiMann See stackoverflow.com/questions/21866477/…