环境变量和 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15428872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:13:17  来源:igfitidea点击:

Environment variables and PHP

environment-variablesphp

提问by sungiant

I have an ubuntu server with a handful of custom environment variables set in /etc/environment as per the ubuntu community recommendation

我有一个 ubuntu 服务器,根据ubuntu 社区的建议,在 /etc/environment 中设置了一些自定义环境变量

When I use php from the command line I can use php's getenv()function to access this variables.

当我从命令行使用 php 时,我可以使用 php 的getenv()函数来访问这个变量。

Also, if I run phpinfo()from the command line I see all of my variables in the ENVIRONMENT section.

另外,如果我从命令行运行phpinfo(),我会在 ENVIRONMENT 部分看到我的所有变量。

However, when trying to access the same data inside processes being run by php5-fpm this data is not available. All I can see in the ENVIRONMENT section of phpinfo()is:

但是,当尝试访问由 php5-fpm 运行的进程内的相同数据时,此数据不可用。我在环境部分可以看到的phpinfo()是:

USER    www-data
HOME    /var/www

I know the command line uses this ini:

我知道命令行使用这个ini:

/etc/php5/cli/php.ini

And fpm uses:

而 fpm 使用:

/etc/php5/fpm/php.ini

I've not managed to find any differences between the two that would explain why the ENV variables are not coming through in both.

我没有设法找到两者之间的任何差异来解释为什么 ENV 变量在两者中都没有通过。

Also if run:

另外如果运行:

sudo su www-data

and then echo the environment variables I am expecting they are indeed available to the www-data user.

然后回显我希望 www-data 用户确实可以使用环境变量。

What do I need to do to get my environment variables into the php processes run by fpm?

我需要做什么才能让我的环境变量进入 fpm 运行的 php 进程?

回答by sungiant

It turns out that you have to explicitly set the ENV vars in the php-fpm.conf

事实证明,您必须在 php-fpm.conf 中明确设置 ENV 变量

Here's an example:

下面是一个例子:

[global]
pid = /var/run/php5-fpm.pid
error_log = /var/log/php5-fpm.log

[www]
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
env[MY_ENV_VAR_1] = 'value1'
env[MY_ENV_VAR_2] = 'value2'

回答by Andrej Kouril

1. Setting environment variables automatically in php-fpm.conf

1.在php-fpm.conf中自动设置环境变量

clear_env = no


2. Setting environment variables manually in php-fpm.conf


2.在php-fpm.conf中手动设置环境变量

env[MY_ENV_VAR_1] = 'value1'
env[MY_ENV_VAR_2] = 'value2'


! Both methods are described in php-fpm.conf:


!这两种方法都在 php-fpm.conf 中描述:

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

在 FPM 工作器中清除环境 通过在添加此池配置中指定的环境变量之前清除工作器中的环境来防止任意环境变量到达 FPM 工作进程。设置为“no”将使所有环境变量通过 getenv()、$_ENV 和 $_SERVER 可用于 PHP 代码。默认值:是

clear_env = no



Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from the current environment. Default Value: clean env

传递环境变量,如 LD_LIBRARY_PATH。所有 $VARIABLE 均取自当前环境。默认值:干净的环境

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp


I found solution in this github discussion.


我在这个github 讨论中找到了解决方案。

回答by Magic

The problem is when you run the php-fpm. The process not load the environment.
You can load it in the startup script.
My php-fpm is install by apt-get.
So modify the

问题是当您运行 php-fpm 时。进程不加载环境。
您可以在启动脚本中加载它。
我的 php-fpm 是通过 apt-get 安装的。
所以修改

/etc/init.d/php5-fpm

/etc/init.d/php5-fpm

and add (beware the space between the dot and the slash)

并添加(注意点和斜线之间的空格)

. /etc/profile

. /etc/配置文件

and modify the /etc/profile to add

并修改 /etc/profile 以添加

. /home/user/env.sh

. /home/user/env.sh

In the env.sh. You can exportthe environment whatever you need.

在 env.sh. 您可以export根据需要设置环境。

Then modify

然后修改

php-fpm.conf

php-fpm.conf

add env[MY_ENV_VAR_1] = 'value1'under the [www]section.
Last. restart the php-fpm. You'll get the environment load by the fpm.

env[MY_ENV_VAR_1] = 'value1'在该[www]部分下添加。
最后的。重新启动 php-fpm。您将通过 fpm 获得环境负载。

回答by theBuzzyCoder

Adding on to the answers above, I was running php-fpm7and nginxin an alpine:3.8docker container. The problem that I faced was the env variables of USER myuserwas not getting copied into the USER root

添加到上面的答案,我正在运行php-fpm7nginx在一个alpine:3.8docker 容器中。我面临的问题是 env 变量USER myuser没有被复制到USER root

My entrypoint for docker was

我的 docker 入口点是

sudo nginx  # Runs nginx as daemon
sudo php-fpm7 -F -O  # Runs php-fpm7 in foreground

The solution for this was

对此的解决方案是

sudo -E nginx
sudo -E php-fpm7 -F -O

-Eoption of sudo copies all env variables of current user to the root

-Esudo 选项将当前用户的所有环境变量复制到根目录

Of course, your php-fpm.d/www.conffile should have clear_env=no

当然,你的php-fpm.d/www.conf文件应该有clear_env=no

And FYI, if you're using a daemon service like supervisordthey have their own settings to copy the env. For example, supervisordhas setting called copy_env=True

仅供参考,如果您使用守护程序服务,就像supervisord他们有自己的设置来复制环境。例如,supervisord设置名为copy_env=True