MAMP: Environment variables being deleted/replaced

2019-09-09 14:08发布

问题:

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>

And this is the result:

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

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.

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?

UPDATE 1:

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.

UPDATE 2:

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.

回答1:

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

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.

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