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

前端 未结 4 527
梦如初夏
梦如初夏 2020-11-28 05:13

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

If I\'m running PHP script using

4条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 06:11

    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:

    
      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.
      
        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
      
    
      # DocumentRoot directory
      
        # 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.
        
          RewriteEngine on
          RewriteBase /
    
          # Include all the common rewrite rules (for http and https).
          Include ${MY_PROJECT_PATH}/config/apache/inc.rewriterules-shared.conf
        
      
    
    

    And this is an example of how to access them with PHP:

    
    

提交回复
热议问题