30

I am trying to get apache/php to recognize the path to my git. I have been researching and looking in all corners of the web and cannot find how to do this. Basically, no matter what I try, when I run echo phpinfo(); the Apache Environment path does not change from /usr/bin:/bin:/usr/sbin:/sbin. And when I run system('echo $PATH'); in PHP, it reads the same.

System Information:

  • Mac OSX (Lion)
  • Apache 2 (running as _www)
  • PHP 5.3.6

Here is what I have tried editing so far:

  • /etc/profile
  • ~/.bash_profile
  • ~/.profile
  • /etc/path
  • /etc/path.d/{NEW_FILE}

Nothing I have tried so far has changed the $PATH variable. Any ideas?

SOLUTION

So here is the final solution. I edited the

/System/Library/LaunchDaemons/org.apache.httpd.plist

and added

<key>EnvironmentVariables</key>
<dict>
    <key>PATH</key>
    <string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin</string>
</dict>
Chuck Burgess
  • 11,600
  • 5
  • 41
  • 74
  • [Have you tried turning it off and turning it on again](http://www.youtube.com/watch?v=nn2FB1P_Mn8)? (Seriously, though, have you restarted Apache since doing this?) – cwallenpoole Jul 26 '11 at 17:22
  • 1
    I was having problems running PHP ``exec()`` where the Apache shell had a different PATH than my Mac shell. This solved my issue. – swt83 Jan 21 '14 at 17:21
  • 1
    I had a similar problem with PHP using the Imagick module. It wouldn't open a PDF file because (under the hood) Imagick uses Ghostscript's `gs` binary, which was on my system but not in Apache's $PATH. Adding the above to the .plist file and restarting apache solved it. – Andrew McCombe Oct 28 '15 at 23:03
  • This worked for me. However, for some reason after I installed Sierra, my edits to org.apache.httpd.plist disappeared. I put them back in and restarted Apache to get it to work again. – ScottyB Jan 02 '17 at 13:38

6 Answers6

33

You can set the PATH environment variable in /System/Library/LaunchDaemons/org.apache.httpd.plist.

More in the docs.

Monolo
  • 18,205
  • 17
  • 69
  • 103
  • Yeah, I read another question on here about this. I tried this too. And it didn't work. I got apache errors with this. – Chuck Burgess Jul 26 '11 at 17:36
  • I just retried this and it worked. Not sure why it didn't before. But this seems to be the solution. – Chuck Burgess Jul 26 '11 at 17:40
  • 2
    TBH, I don't know if it works the same on OS X, but on "normal" *nixes, a restart command to the apache process does not relaunch the process, hence it does not get an updated environment (what it does do is that it re-reads its conf file(s)). Maybe something similar is at play here. – Monolo Jul 26 '11 at 17:42
  • 2
    BTW, I used `sudo apachectl restart` and it loads the new PATH settings from `org.apache.https.plist` just fine in this case. – Chuck Burgess Jul 26 '11 at 17:50
  • Interesting - thanks for that info. I now wonder if was talking rubbish in the comment above... – Monolo Jul 26 '11 at 17:56
4

Did you update the PATH environment variable of user '_www'? Apache will read environment variables from the user runs itself. Or, it looks like you didn't restart apache after updating PATH environment variable.

And if you want to modify environment variable in PHP, getenv() and putenv() can be a better choice.

Community
  • 1
  • 1
lqez
  • 2,898
  • 5
  • 25
  • 55
  • 1
    The `putenv()` is only a good solution if you modify your PHP code directly. It's not a good permanent solution if you have to add that to EVERY php script / app you write. – Chuck Burgess Jul 26 '11 at 18:40
  • 2
    how to edit the PATH var for the _www users that doesnt exist in /users ? – Arnold Roa Oct 01 '14 at 19:59
3

Important note for El Capitan (Apologies for the new answer - I don't have enough Rep to comment)

On OSX 10.11, the /System/Library folder is protected, so the files can't be edited.

You need to:

  • Reboot into Recovery Mode (hold CMD + r after the startup sound)
  • Once in recovery mode, go to Utilities > Terminal
  • Run: csrutil disable
  • Reboot back into OSX - you should now be able to change the files
  • Once done, go back to recovery mode and run csrutil enable

Hope that helps

Jack Tsai
  • 158
  • 2
  • 6
Kiers_M
  • 41
  • 3
1

I created this gist that helped me out from the information above:

https://gist.github.com/srayhunter/5208619

My problem was that PHP was not finding a program that we had installed under /usr/local/bin. Once we did the above it all worked and played nice on mac osx.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51
0

A similar problem to what I was having installing Derby. The way I solved it was by opening TextEdit. Select File > Open at this point press Shift + Command + . , this will allow you to view all the documents. Head to the user directory and search for a file called ".profile" . Open it and add the export VARIABLE= Value line for example:

export DERBY_HOME=/opt/local/share/java/derby/

Save the document and restart your terminal to see if the changes went into affect.

Marek
  • 320
  • 2
  • 5
0

for ubuntu server, in /etc/apache2/envvars, for centos server, in /etc/sysconfig/httpd, to add:

export PATH=<your php cli path>

and restart apache

diyism
  • 12,477
  • 5
  • 46
  • 46