php 7 $_ENV && getenv() 为空,设置了 EGPCS

php 7 $_ENV && getenv() empty with EGPCS set

我的服务器有以下设置,

ec2 -> centos7

httpd 2.4
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>

php 7 -> 
php.ini
variables_order=EGPCS

以及在 centos 中设置的环境变量

export myTestDir=/home/myuser

[myUser@.....]$ echo $myTestDir
/home/myuser

但是我无法在 php 中获得任何系统环境变量。

echo exec('whoami');                      // returns -> myUser
$_SESSION['test1']=$_SERVER['myTestDir']; // returns -> null
$_SESSION['test2']=$_ENV['myTestDir'];    // returns -> null
$_SESSION['test3']=getenv("myTestDir");   // returns -> false

我是不是漏掉了一个额外的步骤或者 variables_order=EGPCS

做错了什么

PHP 的环境与您在 shell 中使用的用户环境是隔离的。这是设计使然,因为 PHP 要求您将变量显式导入其环境。它不会隐式地这样做,因为这可能会带来巨大的安全风险。

From the manual

These variables are imported into PHP's global namespace from the environment under which the PHP parser is running. Many are provided by the shell under which PHP is running and different systems are likely running different kinds of shells, a definitive list is impossible. Please see your shell's documentation for a list of defined environment variables.

因此,为了将环境变量加载到 PHP 中,您必须在加载 PHP 解释器的进程级别执行此操作。例如,在 Apache httpd 中,使用 mod_php,这将通过在 apache.conf 或虚拟主机文件中指定 SetEnv 指令来完成。

对于 PHP-FPM,这将在您的池配置文件中完成,例如 env[myTestDir] = "/home/myuser"