如何为PHP全局环境变量 [英] How to set global environment variables for PHP

查看:187
本文介绍了如何为PHP全局环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读的问题/答案<一个href=\"http://stackoverflow.com/questions/13568191/how-to-get-system-environment-variables-into-php-while-running-cli-apache2hand#_=_\">here但我不明白如何在的/ etc /环境变量设置即可。如果我编辑的文件,我需要重新启动我的机器或者干脆注销我的当前用户(或登录一个新的?)。

我想设置一个全局变量来表示我的机器上的网站是在发展或测试模式。我不希望有这种设置每个项目(无论使用PHP,Java的/ Tomcat的,的NodeJS等)。我知道,(Apache的),我可以设置环境变量在以下几个方面:


  1. 直接从PHP与 putenv()函数(这似乎是无用的,因为我想避免这种逻辑,试图找出这些文件在什么服务器)

  2. 使用的.htaccess SETENV环境局部(这需要我复制这个文件/ code为每个服务器,而不是理想)

  3. 使用虚拟主机的指令 SETENV环境局部(如果我使用的是虚拟主机,这在几乎所有情况下的我,但同样需要我复制/粘贴code一遍又一遍)

  4. 在的httpd-conf的 SETENV环境局部(这将只适用于Apache的,我想它应用到的NodeJS服务器以及)

我不是说我不能做#4(和应用#3选择性地向服务器的NodeJS)。但我想,这是一个很好的理由来使用/ etc /环境。正如我前面所说,我已经编辑的文件(后首先创建它),并尝试了以下组合,其中没有一个似乎工作:

 环境=本地
环境=本地
出口环境=本地
出口环境=本地

我说,他们没有工作,因为我没有找到从输出变量:

 的print_r($ _ SERVER);
的print_r($ _ ENV);
回声(GETENV('环境'));


解决方案

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

 包括conf.d / *。CONF

在conf.d文件夹,您将您的特定环境的配置文件。

服务器环境dev.conf例如:

  SETENV环境局部

服务器环境prod.conf例如:

  SETENV环境生产

这些设置将在你的PHP code可用环境变量出现。这种方法可以让你保持你的虚拟主机文件的.htaccess,和你的其他配置文件不可知环境。

等/环境等/的profile.d,.bash_profile中,.profile文件等文件不能由PHP在Apache中可读由于一些进程/用户限​​制。你可以抨击,粉碎,崩溃,所有你想要的变量,并看到他们在你的终端设置,但他们不会在phpinfo()函数,除非你通过Apache配置文件之一将其显示出来。

有关你的NodeJS可以启动应用程序传递环境变量,也可以设置NODE_ENV以多种方式包括你的.bash_profile,并可能etc / environment文件,如果你想成为用户无关。

一个很好看的节点:
<一href=\"http://www.hacksparrow.com/running-ex$p$pss-js-in-production-mode.html\">http://www.hacksparrow.com/running-ex$p$pss-js-in-production-mode.html

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?).

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. 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)

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

解决方案

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

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

server-environment-dev.conf example:

SetEnv ENVIRONMENT "local"

server-environment-prod.conf example:

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.

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.

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.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆