本文介绍了MAMP:环境变量在envvar和运行时之间被删除/替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MAMP + MAMP Pro 3.5进行本地开发.使用envvars文件,我提供了自己的PATH,该PATH与针对任何其他用户或目的的PATH不同.假设它是/my/unique/custom/path.

Using MAMP + MAMP Pro 3.5 for local development. Using the envvars file, I'm supplying my own PATH, that is different from the PATH for any other user or purpose. Let's pretend it's /my/unique/custom/path.

我将以下文件放在本地开发站点上:

I put the following file on my local development site:

<ol>
    <li><?php print $_SERVER['PATH']; ?></li>
    <li><?php print $_ENV['PATH']; ?></li>
    <li><?php print exec('echo $PATH'); ?></li>
</ol>

这是结果:

  1. /my/unique/custom/path
  2. /my/unique/custom/path
  3. /usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.
  1. /my/unique/custom/path
  2. /my/unique/custom/path
  3. /usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.

因此PHP认为PATH环境变量应该是我的,但在某种程度上,它不是实际用于运行命令的变量.

So PHP sees that the PATH environment variable is supposed to be mine, but somehow it's not the one actually used to run commands.

可能是什么原因造成的?显然,#3 PATH是bash的默认路径,如果您不设置它的话,我认为呢?鉴于PHP能够正确看到它,是什么导致它被删除或从不设置?

What could be causing this? Apparently that #3 PATH is the default path for bash, if you don't set one, I think? What could be causing it to get either deleted or never set, given that PHP sees it correctly?

更新1:

运行print_r(shell_exec('env'))可以使我得到这一点,

Running print_r(shell_exec('env')) gets me this and nothing more:

__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
PHP_FCGI_CHILDREN=4
PWD=/path/to/website/files
SHLVL=1
PHP_FCGI_MAX_REQUESTS=200
_=/usr/bin/env

光秃秃的.似乎$ _变量是一个大提示,但是我还不知道是什么.

Pretty bare. Seems like that $_ variable is a big clue, but I don't yet know to what.

更新2:

好的,所以最终结果是一个裸露的环境是正常的.这就是为什么首先要在envvars中设置环境变量的原因.但是envvars显然可以正常工作,否则该路径将不会出现在$_SERVER中.因此,在envvars运行到运行我自己的代码的时间之间发生了一些事情.

Okay, so it ends up that a bare environment is normal. This is why you set environment variables in envvars in the first place. But envvars is clearly working, or else that path wouldn't show up in $_SERVER. So something is happening between the time envvars is run and the time I run my own code.

推荐答案

最终的罪魁祸首是FastCGI.默认情况下,它将清除PHP脚本中的所有环境变量.我猜是安全功能.

The culprit here ended up being FastCGI. By default, it clears all environment variables from your PHP scripts. Security feature, I guess.

使用MAMP,没有办法全局关闭clear_env设置.但是,如果您编辑适当的/Applications/MAMP/fcgi-bin/phpX.Y.Z.fcgi文件,则可以添加如下一行:

With MAMP, there's not a way to turn that clear_env setting off globally. But if you edit the appropriate /Applications/MAMP/fcgi-bin/phpX.Y.Z.fcgi file, you can add a line like so:

export PATH='/my/unique/custom/path'

...而您正在做生意.

...and you're in business.

请确保您在上方行中添加该行,该行已在文件中以exec开头的最后一行.

Make sure you add that line above the last line already in the file that begins with exec.

这篇关于MAMP:环境变量在envvar和运行时之间被删除/替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:01
查看更多