如何让 PHP 能够读取系统环境变量
我在 Centos 上使用 PHP 作为 PHP-FPM.我正在尝试遵循将设置存储在环境变量中的 http://12factor.net/ 准则.
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.
我在/etc/profile.d 中创建了一个文件,用于设置我想要的环境变量,并且当通过 Bash 在 CLI 中进行测试(即运行 bash 脚本)时会出现环境变量:
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
显示正确的输出.
我已将 clear_env 设置为 false 并将 variables_order 设置为 EGPCS
,但是,我设置的变量也未显示在 PHP 中 getenv('SOME_SERVER_SETTING')
或做 var_dump($_ENV)
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)
需要设置哪些其他设置以允许 PHP-FPM 接收所有服务器环境变量,尤其是通过 Centos 上/etc/profiles.d 中的 shell 脚本设置的那些?
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?
推荐答案
安全原因:-)
参见 /etc/php5/fpm/pool.d/www.conf
(debian 位置,在 CentO 上可能不同)
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
相关文章