Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

apache - How to get system environment variables into PHP while running CLI & Apache2Handler?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 [email protected]

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') . "
" .
      getenv('MY_PROJECT_ENV') . "
" .
      getenv('MY_PROJECT_MAIL') . "
";
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...