环境变量和PHP [英] Environment variables and PHP
问题描述
我有一个ubuntu服务器,根据 ubuntu社区推荐
I have an ubuntu server with a handful of custom environment set in /etc/environment as per the ubuntu community recommendation
当我从命令行使用php时,我可以使用php的 getenv()
函数访问这个变量。
When I use php from the command line I can use php's getenv()
function to access this variables.
另外,如果我从命令行运行 phpinfo()
ENVIRONMENT部分中的所有变量。
Also, if I run phpinfo()
from the command line I see all of my variables in the ENVIRONMENT section.
HOWEVER:
当尝试访问进程内的相同数据由php5-fpm运行,此数据不可用。我可以在 phpinfo()
的ENVIRONMENT部分中看到:
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:
USER www-data
HOME /var/www
我知道命令行使用这个ini:
I know the command line uses this ini:
/etc/php5/cli/php.ini
并且fpm使用:
/etc/php5/fpm/php.ini
我没有设法找到两者之间的区别,这将解释为什么ENV
I've not managed to find any differences between the two that would explain why the ENV variables are not coming through in both.
另外如果运行:
sudo su www-data
然后回应环境变量,我期待他们确实可用www-data user。
and then echo the environment variables I am expecting they are indeed available to the www-data user.
我需要做些什么来让我的环境变量进入由fpm运行的php进程?
What do I need to do to get my environment variables into the php processes run by fpm?
推荐答案
事实证明,你必须在php-fpm.conf中明确设置ENV var
It turns out that you have to explicitly set the ENV vars in the php-fpm.conf
这是一个例如:
[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'
这篇关于环境变量和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!