如何在运行CLI& Apache2Handler? [英] How to get system environment variables into PHP while running CLI & Apache2Handler?

查看:190
本文介绍了如何在运行CLI& Apache2Handler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统是 Ubuntu ,我已将环境变量设置为 / etc / environment



如果我使用 CLI 运行 PHP 脚本 - / etc / environment 中的环境变量是



但是,如果我执行 PHP 脚本通过 http://domain/test.php (即 apache2handler )完全相同的脚本打印出NULL,意味着 / etc / environment 中的环境变量未加载



我所做的修复是在 / etc / apache2 / envvars 中添加变量,解决了这个问题。 / p>

但这是两个不同的文件,然后必须保持同步。



我如何使 PHP / Apache 加载并识别来自 / etc / environment (系统)的环境变量?



编辑:澄清事情,当我说'没有加载到P HP'表示 / etc / environment 中的变量未在 $ _ SERVER $中设置_ code>, getenv(),并且不存在于 $ GLOBALS 中。换句话说没有加载到PHP。

解决方案

我有完全相同的问题。为了解决这个问题,我刚刚在 / etc / apache2 / envvars 中采购了 / etc / environment 。 b
$ b

/ etc / environment 的内容:

 code> export MY_PROJECT_PATH = / var / www / my-project 
export MY_PROJECT_ENV = production
export MY_PROJECT_MAIL=support@my-project.com
/ pre>

/ etc / apache2 / envvars 的内容:

 #加载所有系统环境变量。 
。 / etc / environment

现在,我可以在Apache Virtual Host配置文件中使用这些变量在PHP中。



以下是Apache虚拟主机的示例:

 < VirtualHost *:80> 
ServerName my-project.com
ServerAlias www.my-project.com
ServerAdmin $ {MY_PROJECT_MAIL}
UseCanonicalName

DocumentRoot $ {MY_PROJECT_PATH} / www

#错误日志。
ErrorLog $ {APACHE_LOG_DIR} /my-project.com_error.log
LogLevel warn

#访问日志。
< IfModule log_config_module>
LogFormat%h%l%u%t \%m%> U%q\%> s%b%Dclean_url_log_format
CustomLog $ {APACHE_LOG_DIR} / my- project.com_access.log clean_url_log_format
< / IfModule>

#DocumentRoot目录
<目录$ {MY_PROJECT_PATH} / www>
#完全禁用.htaccess规则,以获得更好的性能。
AllowOverride无
选项FollowSymLinks包含
订单拒绝,允许
从全部

中包含$ {MY_PROJECT_PATH} /config/apache/inc.mime- types.conf
包含$ {MY_PROJECT_PATH} /config/apache/inc.cache-control.conf

#重写规则。
< IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

#包含所有常见的重写规则(用于http和https)。
包含$ {MY_PROJECT_PATH} /config/apache/inc.rewriterules-shared.conf
< / IfModule>
< / Directory>
< / VirtualHost>

这是一个如何使用PHP访问它们的示例:

 <?php 
header('Content-Type:text / plain; charset = utf-8');
print getenv('MY_PROJECT_PATH')。 \\\

getenv('MY_PROJECT_ENV')。 \\\

getenv('MY_PROJECT_MAIL')。 \\\
;
?>


My system is Ubuntu and I have set my environment variables in /etc/environment.

If I'm running PHP script using CLI - environment variables from /etc/environment are recognized.

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.

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.

How can I make PHP / Apache load and recognize environment variables from /etc/environment (system)?

EDIT: 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'.

解决方案

I had exactly the same problem. To solve it, I just sourced /etc/environment inside /etc/apache2/envvars.

The content of /etc/environment:

export MY_PROJECT_PATH=/var/www/my-project
export MY_PROJECT_ENV=production
export MY_PROJECT_MAIL=support@my-project.com

The content of /etc/apache2/envvars:

# Load all the system environment variables.
. /etc/environment

Now, I'm able to use these variables in the Apache Virtual Host config files and in PHP.

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>

And this is 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";
?>

这篇关于如何在运行CLI&amp; Apache2Handler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆