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?).
我在这里阅读了问题/答案,但我不明白如何在/ 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)我可以通过以下方式设置环境变量:
- 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) - 直接来自php与putenv()(这似乎无用,因为我想避免逻辑,试图找出文件所在的服务器)
- using .htaccess
SetEnv ENVIRONMENT 'local'
(this would require me to duplicate this file/code for every server, not ideal) - 使用.htaccess SetEnv ENVIRONMENT'local'(这需要我为每个服务器复制这个文件/代码,不理想)
- 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) - 使用虚拟主机指令SetEnv ENVIRONMENT'local'(如果我使用虚拟主机,几乎在所有情况下我都是这样,但又要求我一遍又一遍地复制/粘贴代码)
- in httpd-conf
SetEnv ENVIRONMENT 'local'
(this will only apply to apache, and I would like it to apply to NodeJS servers as well) - 在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'));
3 个解决方案
#1
41
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等文件。你可以对所有你想要的变量进行打击,粉碎,崩溃,并在终端中看到它们设置,但除非你通过其中一个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
#2
1
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';
}
#3
0
If you still can't get your environment variables:
如果仍然无法获取环境变量:
you may need to edit your real httpd.conf in
您可能需要编辑真正的httpd.conf
~/Library/Application Support/appsolute/MAMP PRO/
〜/ Library / Application Support / appolute / MAMP PRO /
instead of
代替
/Applications/MAMP/conf/apache/
/应用程序/ MAMP / conf目录/阿帕奇/
Also you may need to use getenv() instead of $_ENV
此外,您可能需要使用getenv()而不是$ _ENV
#1
41
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等文件。你可以对所有你想要的变量进行打击,粉碎,崩溃,并在终端中看到它们设置,但除非你通过其中一个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
#2
1
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';
}
#3
0
If you still can't get your environment variables:
如果仍然无法获取环境变量:
you may need to edit your real httpd.conf in
您可能需要编辑真正的httpd.conf
~/Library/Application Support/appsolute/MAMP PRO/
〜/ Library / Application Support / appolute / MAMP PRO /
instead of
代替
/Applications/MAMP/conf/apache/
/应用程序/ MAMP / conf目录/阿帕奇/
Also you may need to use getenv() instead of $_ENV
此外,您可能需要使用getenv()而不是$ _ENV