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

2022-01-15 00:00:00 environment-variables php apache

我已阅读问题/答案 这里,但我不明白如何在/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

相关文章