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

查看:149
本文介绍了如何为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. 直接从php与 putenv()(这似乎是无用的,因为我想避免使用.htaccess的
  2. c $ c> 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(并且选择性地应用到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.

etc / environment,etc / profile.d,.bash_profile由于某些进程/用户限制,Apache中的.profile等文件不能被PHP读取。您可以bash,smash,崩溃所有您想要的变量,并看到它们设置在您的终端,但除非您通过其中一个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.

一个很好的阅读节点:$ b​​ $ b http://www.hacksparrow.com/running-express-js-in-production-mode.html

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

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

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