apache环境中setenv的等价物是什么? 使用apache,我可以设置env“SOMEENV”并通过$ _ENV ['SOMEENV']在php中访问它 - 但我不知道如何使用nginx + php-fpm。
我最初认为我只需要在php-fpm池的配置中设置ENV [SOMENEV] = test,但var_dump($ _ ENV)仍然不返回任何内容。
任何提示?
答案 0 :(得分:18)
nginx没有办法影响php的环境,因为它没有将php解释器嵌入到它的进程中。它通过fastcgi_param指令将参数传递给php。您可以在设置其余参数的位置添加一个,并通过$ _SERVER:
访问它location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SOMEENV test;
fastcgi_pass php;
}
答案 1 :(得分:6)
请注意$_ENV
变量的可用性取决于php-fpm使用的php.ini中variables_order
的设置。默认值为EGPCS
,其中E
为环境,但在Ubuntu 12.04上,我发现它是GPCS
。 php.ini本身带有关于$_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.
建议使用始终可用的getenv()
。我发现我在FPM池中设置的变量可以通过这种方式检索。