如何让PHP能够读取系统环境变量 [英] How to get PHP to be able to read system environment variables
问题描述
我在Centos上使用PHP作为PHP-FPM。我想遵循将设置存储在环境变量中的 http://12factor.net/ 指南。 / p>
我在/etc/profile.d中创建了一个文件,该文件设置了我想要的环境变量,当在CLI中通过Bash测试时,环境变量出现,即运行bash脚本:
echo $ SOME_SERVER_SETTING
显示正确的输出。
我已将clear_env设置为false,并将variables_order设置为 EGPCS
,但是,我设置的变量不会在PHP中显示 getenv('SOME_SERVER_SETTING')
或者执行 var_dump($ _ ENV)
需要设置什么其他设置,以允许PHP-FPM接收所有服务器环境变量,安全原因: - )
请参阅 /etc/php5/fpm/pool.d/www.conf
(debian位置,可能与CentOs不同)
;清晰的FPM工作环境
;防止任意环境变量访问FPM工作进程
;通过在此
中指定的env vars之前清除工作环境;池配置。
;设置为no将使所有环境变量可用于PHP代码
;通过getenv(),$ _ENV和$ _SERVER。
;默认值:yes
; clear_env = no
;传递如LD_LIBRARY_PATH的环境变量。所有$ VARIABLEs取自
;当前环境。
;默认值:clean env
; env [HOSTNAME] = $ HOSTNAME
; env [PATH] = / usr / local / bin:/ usr / bin:/ bin
; env [TMP] = / tmp
; env [TMPDIR] = / tmp
; env [TEMP] = / tmp
I am using PHP as PHP-FPM on Centos. I am trying to follow the http://12factor.net/ guidelines of having the settings be stored in environment variables.
I have created a file in /etc/profile.d that sets the environment variables I want, and the environment variables appear when tested in the CLI via Bash i.e. running the bash script:
echo $SOME_SERVER_SETTING
shows the correct output.
I have set the clear_env setting to false and variables_order to EGPCS
, however, the variable I have set does not show up in PHP either getenv('SOME_SERVER_SETTING')
or doing var_dump($_ENV)
What other setting needs to be set to allow PHP-FPM to receive all of the server environment variables, and in particular those set through a shell script in /etc/profiles.d on Centos?
Security reasons :-)
See /etc/php5/fpm/pool.d/www.conf
(debian location, may be different on CentOs)
; Clear environment in FPM workers
; Prevents arbitrary environment variables from reaching FPM worker processes
; by clearing the environment in workers before env vars specified in this
; pool configuration are added.
; Setting to "no" will make all environment variables available to PHP code
; via getenv(), $_ENV and $_SERVER.
; Default Value: yes
;clear_env = no
; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp
这篇关于如何让PHP能够读取系统环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!