75th Trombone 75th Trombone - 1 year ago 72
PHP Question

MAMP: Environment variables being deleted/replaced between envvars and runtime

Using MAMP + MAMP Pro 3.5 for local development. Using the

file, I'm supplying my own PATH, that is different from the PATH for any other user or purpose. Let's pretend it's

I put the following file on my local development site:

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

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?


gets me this and nothing more:


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


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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download