如何获得系统环境变量到PHP中,同时运行CLI和放大器; Apache2Handler? [英] How to get system environment variables into PHP while running CLI & Apache2Handler?
问题描述
我的系统是的的Ubuntu 。我将我的环境变量的/ etc /环境
。
My system is Ubuntu and I have set my environment variables in /etc/environment
.
如果我使用的 CLI 运行 PHP 脚本 - 从环境变量在/ etc /环境
的认可。
If I'm running PHP script using CLI - environment variables from /etc/environment
are recognized.
但是,如果我去执行 PHP 脚本直通 HTTP://domain/test.php
(即 apache2handler )完全相同的脚本打印出空,从的/ etc /环境
不加载。
But, if I go to execute PHP script thru http://domain/test.php
(that is apache2handler) exactly the same script prints out NULL, meaning environment variables from /etc/environment
are not loaded.
我做的/ etc / apache2的/ envvars中
并解决了这个问题。
The fix I did was adding variables in /etc/apache2/envvars
and that solved the problem.
但是,这是两个不同的文件,然后必须保持同步。
But that is two different files, which then have to be kept in sync.
我怎样才能让 PHP / 的Apache 从载荷和认识环境变量在/ etc /环境
(系统) ?
How can I make PHP / Apache load and recognize environment variables from /etc/environment
(system)?
编辑:为了澄清的事情,当我说'没有加载到PHP这意味着从变量的/ etc /环境
未在 $设置_ SERVER
, $ _ ENV
,的getenv()
和做不存在 $ GLOBALS
。换句话说不会被加载到PHP。
To clarify things, when I say 'not loaded into PHP' it means variables from /etc/environment
are not set in $_SERVER
, $_ENV
, getenv()
and do not exists in $GLOBALS
. In other words 'are not loaded into PHP'.
推荐答案
我有完全相同的问题。为了解决这个问题,我只是来源的的/ etc /环境中的的/ etc / apache2的/ envvars中的
I had exactly the same problem. To solve it, I just sourced /etc/environment inside /etc/apache2/envvars.
的的/ etc /环境中的含量的:
export MY_PROJECT_PATH=/var/www/my-project
export MY_PROJECT_ENV=production
export MY_PROJECT_MAIL=support@my-project.com
的内容的的/ etc / apache2的/ envvars中的:
# Load all the system environment variables.
. /etc/environment
现在,我可以在Apache虚拟主机配置文件,并在PHP中使用这些变量。
Now, I'm able to use these variables in the Apache Virtual Host config files and in PHP.
下面是一个Apache虚拟主机的例子:
Here's an example of an Apache virtual host:
<VirtualHost *:80>
ServerName my-project.com
ServerAlias www.my-project.com
ServerAdmin ${MY_PROJECT_MAIL}
UseCanonicalName On
DocumentRoot ${MY_PROJECT_PATH}/www
# Error log.
ErrorLog ${APACHE_LOG_DIR}/my-project.com_error.log
LogLevel warn
# Access log.
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%m %>U%q\" %>s %b %D" clean_url_log_format
CustomLog ${APACHE_LOG_DIR}/my-project.com_access.log clean_url_log_format
</IfModule>
# DocumentRoot directory
<Directory ${MY_PROJECT_PATH}/www>
# Disable .htaccess rules completely, for better performance.
AllowOverride None
Options FollowSymLinks Includes
Order deny,allow
Allow from All
Include ${MY_PROJECT_PATH}/config/apache/inc.mime-types.conf
Include ${MY_PROJECT_PATH}/config/apache/inc.cache-control.conf
# Rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Include all the common rewrite rules (for http and https).
Include ${MY_PROJECT_PATH}/config/apache/inc.rewriterules-shared.conf
</IfModule>
</Directory>
</VirtualHost>
这将是如何使用PHP访问它们的例子:
And this would be an example of how to access them with PHP:
<?php
header('Content-Type: text/plain; charset=utf-8');
print getenv('MY_PROJECT_PATH') . "\n" .
getenv('MY_PROJECT_ENV') . "\n" .
getenv('MY_PROJECT_MAIL') . "\n";
?>
希望这可以帮助一些人...
Hope this can help some people...
这篇关于如何获得系统环境变量到PHP中,同时运行CLI和放大器; Apache2Handler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!