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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19696230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:52:32  来源:igfitidea点击:

How to set global environment variables for PHP

phpapacheenvironment-variables

提问by alyda

I have read the question/answers herebut 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?).

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

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:

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

  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)
  1. 直接从 php with putenv()(这似乎没用,因为我想避免试图找出文件所在服务器的逻辑)
  2. 使用 .htaccess SetEnv ENVIRONMENT 'local'(这需要我为每个服务器复制这个文件/代码,不理想)
  3. 使用虚拟主机指令SetEnv ENVIRONMENT 'local'(如果我使用的是虚拟主机,几乎在所有情况下我都是这样,但再次要求我一遍又一遍地复制/粘贴代码)
  4. 在 httpd-conf 中SetEnv ENVIRONMENT 'local'(这只适用于 apache,我希望它也适用于 NodeJS 服务器)

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:

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

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'));

回答by nopuck4you

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

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

Include conf.d/*.conf

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

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

server-environment-dev.conf example:

server-environment-dev.conf 示例:

SetEnv ENVIRONMENT "local"

server-environment-prod.conf example:

server-environment-prod.conf 示例:

SetEnv ENVIRONMENT "production"

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.

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

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.

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

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.

对于 NodeJS,您可以通过传入您的环境变量来启动应用程序,或者您可以通过多种方式设置 NODE_ENV,包括您的 .bash_profile 和可能的 etc/environment 文件,如果您想成为用户不可知的。

A good read for Node: http://www.hacksparrow.com/running-express-js-in-production-mode.html

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

回答by Machavity

So I would assume you have a global config file somewhere. Why not put a constant in that file that you can change? Would be far easier that trying to set something on the server level.

所以我假设你在某处有一个全局配置文件。为什么不在该文件中放置一个可以更改的常量?尝试在服务器级别设置一些东西会容易得多。

define('ENVIRONMENT', 'testing');
if(ENVIRONMENT == 'testing') {
   echo 'We\'re just testing';
}

回答by Sebastien Horin

If you still can't get your environment variables:

如果您仍然无法获取环境变量:

you may need to edit your realhttpd.conf in

你可能需要编辑你真正的httpd.conf

~/Library/Application Support/appsolute/MAMP PRO/

~/库/应用程序支持/appsolute/MAMP PRO/

instead of

代替

/Applications/MAMP/conf/apache/

/应用程序/MAMP/conf/apache/

Also you may need to use getenv()instead of $_ENV

此外,您可能需要使用getenv()而不是$_ENV