沃梦达 / 编程问答 / php问题 / 正文

如何在运行 CLI & 时将系统环境变量导入 PHPApache2处理程序?

How to get system environment variables into PHP while running CLI amp; Apache2Handler?(如何在运行 CLI amp; 时将系统环境变量导入 PHPApache2处理程序?)

本文介绍了如何在运行 CLI & 时将系统环境变量导入 PHPApache2处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统是Ubuntu,我已经在/etc/environment中设置了我的环境变量.

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

如果我使用 CLI 运行 PHP 脚本 - 来自 /etc/environment 的环境变量会被识别.

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

但是,如果我通过 http://domain/test.php(即 apache2handler)执行 PHP 脚本,那么同样的脚本打印出 NULL,这意味着来自 /etc/environment 的环境变量没有被加载.

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/environment(系统)的环境变量?

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

为了澄清事情,当我说未加载到 PHP 中"时,这意味着来自 /etc/environment 的变量未在 $_SERVER 中设置$_ENVgetenv() 并且不存在于 $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/apache2/envvars 中获取了 /etc/environment.

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

/etc/environment的内容:

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 is an example of how to access them with PHP:

<?php
header('Content-Type: text/plain; charset=utf-8');
print getenv('MY_PROJECT_PATH') . "
" .
      getenv('MY_PROJECT_ENV') . "
" .
      getenv('MY_PROJECT_MAIL') . "
";
?>

这篇关于如何在运行 CLI &amp; 时将系统环境变量导入 PHPApache2处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在运行 CLI &amp; 时将系统环境变量导入 PHPApache2处理程序?

基础教程推荐