Install $ _ENV (fka $ HTTP_ENV_VARS) with nginx / php-fpm

What is the equivalent of setenv in apache environment? With apache, I can, for example, install env "SOMEENV" and access it in php via $ _ENV ['SOMEENV'] - but I don’t know how to do it with nginx + php-fpm.

Initially, I thought that I just needed to set ENV [SOMENEV] = test to the configuration of my php-fpm pool, but var_dump ($ _ ENV) still returns nothing.

Any clues?

+8
php environment-variables nginx
source share
3 answers

nginx is not able to influence the php environment because it does not embed a PHP interpreter in its process. It passes parameters to php via fastcgi_param . You can simply add the one where you set the rest of the parameters and access it through $ _SERVER:

location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_param SOMEENV test; fastcgi_pass php; } 
+17
source

Remember that the availability of $_ENV variables depends on the variables_order setting in php.ini used by php-fpm. By default, EGPCS used, where E is the environment, however on Ubuntu 12.04 I found that it was GPCS . $_ENV itself carries a warning about $_ENV :

 ; This directive determines which super global arrays are registered when PHP ; starts up. G,P,C,E & S are abbreviations for the following respective super ; globals: GET, POST, COOKIE, ENV and SERVER. There is a performance penalty ; paid for the registration of these arrays and because ENV is not as commonly ; used as the others, ENV is not recommended on productions servers. 

It is recommended that you use getenv() , which is always available. I found that the variables set in the FPM pool can be restored in this way.

+6
source

I set all env [...] = variables in php-fpm.d / www.conf After trying the weekend, I found the only way to access these variables using $ _SERVER, and $ _ENV cannot access these variables. Can anyone give an idea?

0
source

All Articles