1. <legend id='xWfrl'><style id='xWfrl'><dir id='xWfrl'><q id='xWfrl'></q></dir></style></legend>
    2. <tfoot id='xWfrl'></tfoot>
    3. <small id='xWfrl'></small><noframes id='xWfrl'>

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

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

        如何为 PHP 设置全局环境变量

        How to set global environment variables for PHP(如何为 PHP 设置全局环境变量)
          <i id='QheGw'><tr id='QheGw'><dt id='QheGw'><q id='QheGw'><span id='QheGw'><b id='QheGw'><form id='QheGw'><ins id='QheGw'></ins><ul id='QheGw'></ul><sub id='QheGw'></sub></form><legend id='QheGw'></legend><bdo id='QheGw'><pre id='QheGw'><center id='QheGw'></center></pre></bdo></b><th id='QheGw'></th></span></q></dt></tr></i><div id='QheGw'><tfoot id='QheGw'></tfoot><dl id='QheGw'><fieldset id='QheGw'></fieldset></dl></div>

            <tfoot id='QheGw'></tfoot>
          1. <small id='QheGw'></small><noframes id='QheGw'>

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

              • <legend id='QheGw'><style id='QheGw'><dir id='QheGw'><q id='QheGw'></q></dir></style></legend>

                    <tbody id='QheGw'></tbody>
                • 本文介绍了如何为 PHP 设置全局环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已阅读问题/答案 这里,但我不明白如何在/etc/environment中设置变量.如果我编辑文件,我需要重新启动我的机器还是简单地注销我当前的用户(或登录一个新用户?).

                  I have read the question/answers here but I don't understand how to set variables in /etc/environment. If I edit the file, do I need to restart my machine or simply log out my current user (or log in a new one?).

                  我想设置一个全局变量来表示我机器上的网站处于开发"或测试"模式.我不想为每个项目都设置这个(无论是使用 PHP、Java/Tomcat、NodeJS 等).我知道(对于 Apache)我可以通过以下方式设置环境变量:

                  I want to set a global variable to denote that websites on my machine are in 'development' or 'testing' mode. I don't want to have to set this for every project (whether it uses PHP, Java/Tomcat, NodeJS, etc). I'm aware that (for Apache) I can set the environment variable in the following ways:

                  1. 使用 putenv() 直接从 php(这似乎没用,因为我想避免试图找出文件在哪个服务器上的逻辑)
                  2. 使用 .htaccess SetEnv ENVIRONMENT 'local'(这需要我为每个服务器复制这个文件/代码,不理想)
                  3. 使用虚拟主机指令 SetEnv ENVIRONMENT 'local'(如果我使用的是虚拟主机,几乎在所有情况下我都是,但再次要求我一遍又一遍地复制/粘贴代码再次)
                  4. 在 httpd-conf SetEnv ENVIRONMENT 'local' (这只适用于 apache,我希望它也适用于 NodeJS 服务器)
                  1. directly from php with putenv() (this seems useless since I want to avoid logic that tries to figure out what server the files are on)
                  2. using .htaccess SetEnv ENVIRONMENT 'local' (this would require me to duplicate this file/code for every server, not ideal)
                  3. using a Virtual Host directive SetEnv ENVIRONMENT 'local' (if I'm using a virtual host, which in nearly all cases I am, but again requires me to copy/paste code over and over again)
                  4. in httpd-conf SetEnv ENVIRONMENT 'local' (this will only apply to apache, and I would like it to apply to NodeJS servers as well)

                  我并不是说我不能做 #4(并且有选择地将 #3 应用于 NodeJS 服务器).但我认为这是使用/etc/environment 的一个很好的理由.正如我上面所说,我已经编辑了文件(在第一次创建它之后)并尝试了以下组合,这些组合似乎都不起作用:

                  I'm not saying I can't do #4 (and apply #3 selectively to NodeJS servers). But I'm thinking that this is a good reason to use /etc/environment. As I said above, I have edited the file (after first creating it) and tried the following combinations, none of which seemed to work:

                  ENVIRONMENT='local'
                  ENVIRONMENT=local
                  export ENVIRONMENT='local'
                  export ENVIRONMENT=local
                  

                  我说它们都不起作用,因为我没有在输出中找到变量:

                  I say that none of them worked because I did not find the variable in output from:

                  print_r($_SERVER);
                  print_r($_ENV);
                  echo(getenv('ENVIRONMENT'));
                  

                  推荐答案

                  您要做的是使用 Apache 配置文件.您将需要访问配置文件夹和 httpd.conf 文件(或修改版本).然后,您可以使用这种方法配置 httpd.conf 以动态加载配置文件

                  What you want to do is use an Apache configuration file. You will need access to a configuration folder and the httpd.conf file (or modified version). You can then configure the httpd.conf to dynamically load configuration files using this approach

                  Include conf.d/*.conf
                  

                  在 conf.d 文件夹中放置特定的环境配置文件.

                  Inside the conf.d folder you place your specific environment configuration files.

                  server-environment-dev.conf 示例:

                  server-environment-dev.conf example:

                  SetEnv ENVIRONMENT "local"
                  

                  server-environment-prod.conf 示例:

                  server-environment-prod.conf example:

                  SetEnv ENVIRONMENT "production"
                  

                  这些设置将作为可用的环境变量显示在您的 php 代码中.这种方法允许您保持您的 vhost 文件、.htaccess 和其他配置文件与环境无关.

                  These settings will show up in your php code as available environment variables. This approach allows you to keep your vhost files, .htaccess, and your other configuration files agnostic of the environment.

                  由于某些进程/用户限制,Apache 中的 PHP 无法读取 etc/environment、etc/profile.d、.bash_profile、.profile 等文件.您可以随心所欲地对变量进行 bash、粉碎、崩溃,并在终端中查看它们的设置,但除非您通过 Apache 配置文件之一进行设置,否则它们不会显示在 phpinfo() 中.

                  etc/environment, etc/profile.d, .bash_profile, .profile, etc files are not readable by PHP in Apache due to some process/user limitations. You can bash, smash, crash the variables all you want and see them set in your terminal but they will not show up in phpinfo() unless you set it via one of the Apache configuration files.

                  对于 NodeJS,您可以启动应用程序并传入您的环境变量,或者您可以通过多种方式设置 NODE_ENV,包括您的 .bash_profile 和可能的 etc/environment 文件(如果您想与用户无关).

                  For NodeJS you can start the app passing in your environment variable or you can set the NODE_ENV in multiple ways include your .bash_profile and possibly etc/environment file if you want to be user agnostic.

                  Node 的好读物:http://www.hacksparrow.com/running-express-js-in-production-mode.html

                  这篇关于如何为 PHP 设置全局环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  When should use doctrine ORM and when zend-db-table?(什么时候应该使用学说 ORM,什么时候应该使用 zend-db-table?)
                  Doctrine - self-referencing entity - disable fetching of children(Doctrine - 自引用实体 - 禁用获取子项)
                  Doctrine 2, query inside entities(原则 2,实体内部查询)
                  Complex WHERE clauses using the PHP Doctrine ORM(使用 PHP Doctrine ORM 的复杂 WHERE 子句)
                  Doctrine - OneToMany relation, all result row doesn#39;t fetch in object(Doctrine - OneToMany 关系,所有结果行不获取对象)
                  Doctrine and unrefreshed relationships(教义和未更新的关系)
                  <legend id='ehShw'><style id='ehShw'><dir id='ehShw'><q id='ehShw'></q></dir></style></legend>

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

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