28

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?

Thanks in advance.

jhnnycrvr
  • 767
  • 2
  • 7
  • 13

7 Answers7

42

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.

mamp setting php environment variable

Screenshot is from MAMP Pro v 3.0.3

Aerendir
  • 6,152
  • 9
  • 55
  • 108
Mike Grace
  • 16,636
  • 8
  • 59
  • 79
  • 4
    This assumes you use MAMP Pro. – Adrien Apr 06 '14 at 11:40
  • 1
    you may add comments too: # Set local environment variable SetEnv APP_ENV local – Sinan Eldem Jun 25 '14 at 14:16
  • 1
    How to change default libraries path? I have now /user/bin but my library in /usr/local/bin/ffmpeg so to change it I should write it like SetEnv APPLICATION_ENV /usr/local/bin ? – fdrv Mar 22 '16 at 10:59
37

MAMP will load the contents of /Applications/MAMP/Library/bin/envvars into its environment variables when starting apache. So you can edit the file and add:

MY_VAR="foo"
export MY_VAR

Then access that variable like so:

echo $_ENV["MY_VAR"];

prints:

foo

Update for OSX Yosemite:

rename envvars to _envvars

See: MAMP Pro 3.05 on Mavericks updated to Yosemite - Apache does not start

Community
  • 1
  • 1
sxalexander
  • 728
  • 6
  • 11
  • This didn't work for me on MAMP Pro 3.0.6. I tried editing this file as well as other adjacent files `envvars_` and `envvars-std` restarting MAMP after each change. Is there a configuration file I can dig into to see where this is being loaded? Are there any other prerequisites for this to work? I am looking to add a few ENVs across all of my hosts which makes this solution more appealing than the per-host one below. – Evan Mattson Oct 07 '14 at 19:15
  • 1
    @EvanMattson I don't have MAMP Pro installed, but if you can find the `apachectl` script that MAMP Pro utilizes, you can read that source to find the answer. In mine, the path to the correct envvars file was located under a comment of `# pick up any necessary environment variables` – sxalexander Oct 09 '14 at 06:13
  • Thanks for pointing out that file. I'm probably just misunderstanding this. I was checking for the presence of these using PHP, but these are probably Apache environment variables right? The `SetEnv` method works, but like I said, I want to define variables across all of my hosts, without defining them for each one. – Evan Mattson Oct 09 '14 at 19:28
  • If the `envvars` file isn't working for you SetEnv may be what you need. But you may want to read up here on more details for that answer: http://httpd.apache.org/docs/2.2/env.html – sxalexander Oct 09 '14 at 20:53
  • From a while back but still relevant to me. I looked up the `apachectl` script as @sxalexander suggested, and the path was still `/Applications/MAMP/Library/bin/envvars` yet this file does not exist. Im on Mavericks, and `_envvars` and `envvars-std_` exist, but the code within both state "# This file is generated from envvars-std.in" so where is that file?? Confused as to what to edit. – redfox05 Oct 29 '15 at 12:12
  • @redfox05 For older versions of MAMP just create the "envvars" file and it'll work. Hope that helps :) Actually, I'll make this an answer. – martinedwards Mar 20 '16 at 14:43
  • @fdrv You'll have problems modifying PATH if you're running FastCGI. I have [a question an answer](http://stackoverflow.com/questions/36377687/mamp-environment-variables-being-deleted-replaced-between-envvars-and-runtime) that addresses this in particular. – 75th Trombone Nov 30 '16 at 20:38
  • 1
    I tried this locally and it does work. You need to create `envvars` if it doesn't work.
    Try this to find out the right path for the file: `$ cat /Applications/MAMP/Library/bin/apachectl | grep envvars` In my case its `/Applications/MAMP/Library/bin/envvars`, which didn't exist, so I created it:
    `vim /Applications/MAMP/Library/bin/envvars`.
    Look in `/Applications/MAMP/Library/bin/envars_` for a syntax example.
    – cam8001 Mar 02 '17 at 15:05
  • MAMP PRO 4.1 and Sierra, btw. – cam8001 Mar 02 '17 at 15:12
7

You can set your environment variables in Apache configuration...

/Applications/MAMP/conf/apache/httpd.conf

SetEnv MY_VAR theValue
SetEnv ANOTHER thisIsTheValue
doublejosh
  • 5,548
  • 4
  • 39
  • 45
3

MAMP is looking for "/Applications/MAMP/Library/bin/envvars", if the file doesn't, simply create it and add your environment variables.

martinedwards
  • 5,577
  • 1
  • 33
  • 35
0

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.

TNC
  • 5,388
  • 1
  • 26
  • 28
0

If you still can't get your environment variables:

you may need to edit your real httpd.conf in

~/Library/Application Support/appsolute/MAMP PRO/

instead of

/Applications/MAMP/conf/apache/

Also you may need to use getenv() instead of $_ENV

Sebastien Horin
  • 10,803
  • 4
  • 52
  • 54
0

But it doesn't work with El Capitan. You should disable System Integrity Protection.

original

Community
  • 1
  • 1
fdrv
  • 852
  • 1
  • 11
  • 21