<bdo id='WcFq6'></bdo><ul id='WcFq6'></ul>
    <i id='WcFq6'><tr id='WcFq6'><dt id='WcFq6'><q id='WcFq6'><span id='WcFq6'><b id='WcFq6'><form id='WcFq6'><ins id='WcFq6'></ins><ul id='WcFq6'></ul><sub id='WcFq6'></sub></form><legend id='WcFq6'></legend><bdo id='WcFq6'><pre id='WcFq6'><center id='WcFq6'></center></pre></bdo></b><th id='WcFq6'></th></span></q></dt></tr></i><div id='WcFq6'><tfoot id='WcFq6'></tfoot><dl id='WcFq6'><fieldset id='WcFq6'></fieldset></dl></div>

    1. <small id='WcFq6'></small><noframes id='WcFq6'>

      <legend id='WcFq6'><style id='WcFq6'><dir id='WcFq6'><q id='WcFq6'></q></dir></style></legend>

      <tfoot id='WcFq6'></tfoot>
    2. 如何在运行 CLI &amp; 时将系统环境变量导入 PHPApache2处理程序?

      时间:2024-08-10
        <tbody id='JjgL4'></tbody>

        1. <tfoot id='JjgL4'></tfoot>

            <bdo id='JjgL4'></bdo><ul id='JjgL4'></ul>

              <small id='JjgL4'></small><noframes id='JjgL4'>

                <legend id='JjgL4'><style id='JjgL4'><dir id='JjgL4'><q id='JjgL4'></q></dir></style></legend>
                <i id='JjgL4'><tr id='JjgL4'><dt id='JjgL4'><q id='JjgL4'><span id='JjgL4'><b id='JjgL4'><form id='JjgL4'><ins id='JjgL4'></ins><ul id='JjgL4'></ul><sub id='JjgL4'></sub></form><legend id='JjgL4'></legend><bdo id='JjgL4'><pre id='JjgL4'><center id='JjgL4'></center></pre></bdo></b><th id='JjgL4'></th></span></q></dt></tr></i><div id='JjgL4'><tfoot id='JjgL4'></tfoot><dl id='JjgL4'><fieldset id='JjgL4'></fieldset></dl></div>
              • 本文介绍了如何在运行 CLI &amp; 时将系统环境变量导入 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处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 Apache 时在 PHP 中设置用于访问的环境变量 下一篇:如何在 laravel 中以编程方式即时设置 .env 值

                相关文章

                1. <legend id='VjZ2z'><style id='VjZ2z'><dir id='VjZ2z'><q id='VjZ2z'></q></dir></style></legend>
                  <i id='VjZ2z'><tr id='VjZ2z'><dt id='VjZ2z'><q id='VjZ2z'><span id='VjZ2z'><b id='VjZ2z'><form id='VjZ2z'><ins id='VjZ2z'></ins><ul id='VjZ2z'></ul><sub id='VjZ2z'></sub></form><legend id='VjZ2z'></legend><bdo id='VjZ2z'><pre id='VjZ2z'><center id='VjZ2z'></center></pre></bdo></b><th id='VjZ2z'></th></span></q></dt></tr></i><div id='VjZ2z'><tfoot id='VjZ2z'></tfoot><dl id='VjZ2z'><fieldset id='VjZ2z'></fieldset></dl></div>
                    <tfoot id='VjZ2z'></tfoot>

                      <bdo id='VjZ2z'></bdo><ul id='VjZ2z'></ul>
                  1. <small id='VjZ2z'></small><noframes id='VjZ2z'>