php设置$_ENV值的几种方法

WBOY
Release: 2016-06-23 13:39:14
Original
2386 people have browsed it

作者:zhanhailiang 日期:2015-03-04
Copy after login

$_ENV 值是从 PHP 解析器的运行环境导入到 PHP 的全局命名空间。比如,php-cli模式执行,读取当前用户环境变量;php-fpm模式初始化时,读取当前用户环境变量;

1. 通过 修改系统级、用户级配置文件 设置环境变量

通过 /etc/profile, /etc/environment, ~/.profile,~/.bashrc 设置环境变量;注意,php-fpm模式初始化时就已经设置了相关环境变量,所以对于新设置的环境变量需要重启php-fpm;

2. 通过 php-fpm.conf 设置环境变量

; 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] = /tmpenv[TEST_VAR_1] = $TEST_VAR
Copy after login

该方法支持灵活的环境变量配置,比如,通常的php环境运维策略是:

首先, 编写独立的环境变量设置脚本:

export TEST_VAR='hello world'
Copy after login

然后,设置php-fpm.conf配置:

env[TEST_VAR_1] = $TEST_VAR
Copy after login

最后,重启php-fpm;

测试结果如下:

[root@/usr/local/nginx/html]# curl 'http://localhost/test.php'array(25) {  ["TEST_VAR_1"]=>  string(11) "hello world"
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template