php 在 MAMP 中设置环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11256008/
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
Setting Environment Variables in MAMP?
提问by jhnnycrvr
I assume that MAMP environment variables can be set in ~/.bash_profile, but it seems that setting them there does not always take effect right away. For instance, I just set a variable in my .bash_profile and have tried restarting MAMP servers, restarting MAMP itself, and even logging out of my computer and nothing seems to work. The variable is still not accessible from within my environment. Is there another way to set MAMP env vars that I'm missing or is there a cache that I need to clear?
我假设 MAMP 环境变量可以在 ~/.bash_profile 中设置,但似乎在那里设置它们并不总是立即生效。例如,我只是在我的 .bash_profile 中设置了一个变量,并尝试重新启动 MAMP 服务器,重新启动 MAMP 本身,甚至注销我的计算机,但似乎没有任何效果。在我的环境中仍然无法访问该变量。是否有另一种方法来设置我丢失的 MAMP 环境变量,或者是否有我需要清除的缓存?
Thanks in advance.
提前致谢。
回答by Mike Grace
You can also set PHP environment variables in MAMP Pro (not available in MAMP) in the additional parameters for <VirtualHost>:section found in the extended tab of the hosts settings panel.
您还可以在additional parameters for <VirtualHost>:主机设置面板的扩展选项卡中的部分中在 MAMP Pro(在 MAMP 中不可用)中设置 PHP 环境变量。


Screenshot is from MAMP Pro v 3.0.3
截图来自 MAMP Pro v 3.0.3
回答by sxalexander
MAMP will load the contents of /Applications/MAMP/Library/bin/envvarsinto its environment variables when starting apache. So you can edit the file and add:
MAMP 将/Applications/MAMP/Library/bin/envvars在启动 apache 时将 的内容加载到其环境变量中。所以你可以编辑文件并添加:
MY_VAR="foo"
export MY_VAR
Then access that variable like so:
然后像这样访问该变量:
echo $_ENV["MY_VAR"];
prints:
印刷:
foo
Update for OSX Yosemite:
OSX Yosemite 更新:
rename envvarsto _envvars
重命名envvars为_envvars
See: MAMP Pro 3.05 on Mavericks updated to Yosemite - Apache does not start
回答by doublejosh
You can set your environment variables in Apache configuration...
您可以在 Apache 配置中设置环境变量...
/Applications/MAMP/conf/apache/httpd.conf
SetEnv MY_VAR theValue
SetEnv ANOTHER thisIsTheValue
回答by martinedwards
MAMP is looking for "/Applications/MAMP/Library/bin/envvars", if the file doesn't, simply create it and add your environment variables.
MAMP 正在寻找“/Applications/MAMP/Library/bin/envvars”,如果该文件没有,只需创建它并添加您的环境变量。
回答by fdrv
But it doesn't work with El Capitan. You should disable System Integrity Protection.
但它不适用于 El Capitan。您应该禁用系统完整性保护。
回答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
回答by TNC
I'm assuming you're talking about a local server?
我假设您在谈论本地服务器?
I've noticed, if you look at the processes that apache can have multiple instance running that you might need to check. If they are, quit out of them individually, as the restart won't take the new changes until all instances are restarted. Something to try.
我注意到,如果您查看 apache 可以运行多个实例的进程,您可能需要检查这些进程。如果是,请单独退出它们,因为在所有实例重新启动之前,重新启动不会进行新的更改。有什么可以尝试的。

