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.
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.
Screenshot is from MAMP Pro v 3.0.3
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
You can set your environment variables in Apache configuration...
/Applications/MAMP/conf/apache/httpd.conf
SetEnv MY_VAR theValue
SetEnv ANOTHER thisIsTheValue
MAMP is looking for "/Applications/MAMP/Library/bin/envvars", if the file doesn't, simply create it and add your environment variables.
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.
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
But it doesn't work with El Capitan. You should disable System Integrity Protection.
original
Related
I have about 150 websites each on its own Apache virtual host running php7 on ubuntu 16. Each site has a config.php in the root dir with constants in it: define('MYVAR','myval');
I want to make a new constant that I will have to update every 2 months or so. I don't want to open each config and edit them. How can I have each site/virtual host be aware of this constant I want to set. I want to be available everywhere just like $_SERVER['REMOTE_ADDRESS']; is.
I do not want to create a file them symlink it to each site, thats messy. I would like to edit the apache config or edit a php config someplace to have a define() in it.
Is this possibe? or am I stuck sym linking a file or editing all my configs?
As pointed out in this link; PHP: possible to set constants in default php.ini file
You can't just invent variables in the PHP file, you'll need to auto-prepend a config file that has all the variables defined.
Ignore Below
You could store some global variables in your php.ini file.
The single php.ini file should be used by all your Apache virtual-hosts.
If you update your php.ini file and put a variable in like
my_custom_var = ABC123
Then you can access the variable with ini_get
echo ini_get('my_custom_var');
See more details on ini_get at http://php.net/manual/en/function.ini-get.php
I think you should try using OS environment variables.
Try setting the variables and then exporting them in your .profile file.
Ideally, this is where you should store sensitive credentials too. This way, even if your code is leaked, no one can get access to your existing server (never store Database or other credentials in your code)
You can either store your system variables in .profile or .bashrc file (I would recommend .profile). You can see how to set system variables here: https://unix.stackexchange.com/a/117470/209405
You can get the variables in PHP with the help of getenv($var) or with $_ENV[$var]. For more info, you can check here: http://php.net/manual/en/function.getenv.php and http://php.net/manual/en/reserved.variables.environment.php
The best non-intrusive way for me was to make sure mod_env was enabled then I added in the conf-enabled dir a conf file with the following in it
SetEnv MYLITTLEVAR thestringIwant
Then in php I can get it by:
print $_SERVER['MYLITTLEVAR'];
Tested and it works for me.
If want to make some variables globally available in PHP you need to add those variable in the apache config file.
if you're using ubuntu open /etc/apache2/apache2.conf file, And if you're using windows open httpd.conf. And add the following line.
SetEnv VARIABLE_NAME 'whatever value you want'
For something that should be so simple, I'm finding it maddeningly difficult to set the path that PHP exec() commands use under MAMP on a Mac running El Capitan.
I need the solution to use environment variables rather than (for example) PHP's putenv() or a hardcoded "PATH=foo && ", as this code will be used on different servers that each may use a different path. I had it working fine on Yosemite (though, to be honest, I can't remember what finally worked), but when I upgraded to El Capitan the path was reset.
I've tried the following, and have restarted the computer after each just to be entirely sure that Apache's path was being reset:
Added to path in /etc/paths and /etc/paths.d
Added export PATH=$PATH:/opt/local/bin to MAMP/Library/bin/_envvars
Even set path in /System/Library/LaunchDaemons/org.apache.httpd.plist after disabling SIP to make it possible.
Edit to add: Also tried a symbolic link to somewhere in the default path, but LaTeX, for example, really needs to live on the path.
After each of these, the path (as indicated by a "exec('echo $PATH', $output);" command in the PHP) remains the same:
\/usr\/bin:\/bin:\/usr\/sbin:\/sbin
I'm at wit's end. If anybody's managed to set the path on a similar setup, I'd love to get input on how it was done.
Thanks!
On Mavericks it worked like this:
- Stop MAMP
- Rename the file /Applications/MAMP/Library/bin/envvars_ to /Applications/MAMP/Library/bin/envvars (without underscore).
- Add these two lines at the end of the renamed file:
PATH="$PATH:/additional/path1:/additional/path2"
export $PATH
- Start MAMP and have a look into phpinfo() Output
I have a question about setting "$PATH" variables for PHP scripts in AMPPS
(On OSX 10.10 Yosemite).
The PHP in AMPPS seems to run as my user "danny", however the $PATH it sees
is different. Here's what I see from my terminal:
Dannys-MacBook-Air:AMPPS danny$ echo $PATH;
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
And here is what I get within a PHP script by printing out $_ENV:
["PATH"]=> string(29) "/usr/bin:/bin:/usr/sbin:/sbin" ["USER"]=> string(5)
"danny"
I tried doing "putenv()", tried doing "shell_exec" with a different $PATH,
I tried doing SetEnv in .htaccess and in the Apache config file. I tried
editing the systemwide /etc/.bashrc , and my users ~/.bash_profile. Neither
helped so far.
All I need is to have /usr/local/bin as part of my $PATH.
I know this is an older post, but this helped me, (doing this with Ampps, not MAMP, but it should work the same)
From here
Create a variable with the path to your PHP binary (in terminal)
they recommend
export AMPPS_PHP=/Applications/AMPPS/php/bin
mine was export AMPPS_PHP=/Applications/AMPPS/php-7.1/bin
then just
export PATH="$AMPPS_PHP:$PATH"
Test it out with which php or php -v
I don't know if it is even possible, but I wonder why do you need this, i.e. do you need really to change your PATH env variable? Or it would be enough to you using this function and setting the variabile only for the created process?
You can setup your Apache Server to run with your credential and resolve definitively the $PATH problem.
However, you can also do this and it should works :
<?php
putenv('PATH', getenv('PATH') . ';/usr/local/bin');
var_dump(getenv('PATH'));
?>
The modification of $PATH will still available only the duration of your PHP execution, there are other ways to set environment variable permanently
Hope that helps :)
This may not be well received, but I have been in your spot and have come close to throwing my machine across the room trying to properly set the PHP path for MAMP in OSX.
I started using Vagrant and developing from a VM, and found it to be way easier, especially if you want to change languages for other projects. Trust me, it will save you time in the long run.
Download VirtualBox
Download Vagrant
Select what type of box you want via PuPHPet
cd into the puphpet directory and run vagrant up (may take a while!)
The inital setup will take a little bit of time, but be very fast moving forward and makes it really easy to share environments with others.
Cheers
EDIT: I think its a very real solution for the future, but for now what worked for me was adding:
export PATH=/usr/local/php5/bin:$PATH to my .bash_profile
on OSX Yosemite. I believe the folder structure changed in Yosemite.
I've looked at this post about a similar issue:
CakePHP: Can't access MySQL database
and I've tried everything they mentioned in there including:
Changing my database.php so that the 'port' attribute for both $default and $test are the location of my mysqld.sock file
Changing the 'port' attribute to the actual integer that represents the port in my my.cnf mysql config
Changing the mysql socket locations in php.ini to the location of my mysqld.sock file
I'm using ubuntu 11.04, apache 2.2.17, mysql 5.1.54, and CakePHP 1.3.10. My install of mysql and apache don't seem to match any conventions, as in, all the config files are there, they are all just in really weird places--I'm not sure why that is, but I've tried reinstalling both programs multiple times with the same results...
At any rate, I can log into mysql from the terminal and use it normally, and apache is working because I can see the CakePHP default homepage. I just can't get it to change the message 'Cake is NOT able to connect to the database'.
SOLVED: Figured it out, had to change php.ini so that extension_dir pointed to the correct directory and had to add a line extension=mysql.so.
I just got my cake php to work. OMG it sucked. I'm going to make a video tutorial on my blog when I get a chance to make it easy to install and show all the errors and how to fix them.
go into apache and turn this on in the apache modules: rewrite_module. For me in wamp, I just go to apache -> apache modules -> rewrite_module.
Then restart. I had to do this 3 times before it worked.
Then go into the php.ini and turn this on: extension=php_pdo_mysql.dll, by getting rid of the semicolon in the front of it. Then restart again.
My Environmental Variables (& I crosschecked every possible place in the Registry) show my PHP include path as: "C:\PHP\" --and PHP (5.2.9-2) works fine on the system, so that is correct. However, running
ini_get("include_path")
shows the path as "C:\PHP5\". This causes problems with PEAR, etc. I've searched the system (i.e. php.ini files, httpd.conf, & everywhere else I can think of--there are no .htaccess files) & can find nothing that points at PHP5. Where in the world is this coming from??
Try searching for the literal PHP5 using a grep search tool (like windows grep http://www.wingrep.com/) it will take a while, but I think it's the best approach after you've checked all config files and environment variables.
Cheers
If you run a script with a call to phpinfo() PHP will tell you where the values come from:
<php
phpinfo();
?>
The top table will tell you what php.ini files it's reading and you'll find the value of the include_path directive in the Core table.
Something could be setting it using ini_set.
Or, after running the aforementioned grep on the whole system, you can try these:
Debug PHP to see what causes this.
Use Filemon from Sysinternals to see what files PHP is accessing.
This isn't really an answer to your problem however if you install WAMP or XAMPP you can usually avoid all of these annoying configuration issues.