-
Edit the Apache2 plist file with whatever editor you like (example using vim):
$ sudo vim /System/Library/LaunchDaemons/org.apache.httpd.plist
-
Add the following to the the plist file:
<key>EnvironmentVariables</key> <dict> <key>PATH</key> <string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string> </dict>
-
Restart Apache2
$ sudo /usr/sbin/apachectl stop $ sudo /usr/sbin/apachectl start
-
-
Save srayhunter/5208619 to your computer and use it in GitHub Desktop.
This should be added at the end? or inside the main block?
On Yosemite it doesn't help. Here is my config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<true/>
<key>Label</key>
<string>org.apache.httpd</string>
<key>EnvironmentVariables</key>
<dict>
<key>XPC_SERVICES_UNAVAILABLE</key>
<string>1</string>
<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>/usr/sbin/httpd</string>
<string>-D</string>
<string>FOREGROUND</string>
</array>
<key>OnDemand</key>
<false/>
</dict>
</plist>
but "echo getenv("PATH");" shows only /usr/bin:/bin:/usr/sbin:/sbin
Works fine for me on Yosemite
Like @salex772 it doesn't work for me on 10.10.1. Any idea ?
worked perfectly for 10.10.4! thank you!
Don't work on el Capitan. /System/Library/LaunchDaemons/org.apache.httpd.plist cannot be edited (rootless)
It doesn't work for me on el Capitan as well.
I had to run command "csrutil disable" in Terminal after rebooting and hitting CMD+R and then I was able to edit this file.
@piotrczyz was right, disabling csrutil works for el capital as he instructed
On Yosemite it doesn't help. Here is my config
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Disabled</key> <true/> <key>Label</key> <string>org.apache.httpd</string> <key>EnvironmentVariables</key> <dict> <key>XPC_SERVICES_UNAVAILABLE</key> <string>1</string> <key>PATH</key> <string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string> </dict> <key>ProgramArguments</key> <array> <string>/usr/sbin/httpd</string> <string>-D</string> <string>FOREGROUND</string> </array> <key>OnDemand</key> <false/> </dict> </plist>
but "echo getenv("PATH");" shows only /usr/bin:/bin:/usr/sbin:/sbin
I'm getting the same issue.
Solution: For my case
- I have put the environment variables in the ~/.bash_profile
source ~/.bash_profile
sudo apachectl stop
sudo apachectl start
And now it is working properly on Mojave.
@SubhajitSarkar are you trying to add an environment variable for Apache2 to use or for your terminal?
In Catalina this file is now on the new read-only system volume.
I'm not sure if it was there before but I see an empty /private/etc/apache2/env.plist
which is maybe designed as a workaround?
I have the Server app installed, which now has its own instance of apache running, the default apache has this but I'm not sure where to set this env variable:
<IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
Listen 8080
</IfDefine>
<IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
Listen 80
</IfDefine>
I tried setting a SERVER_APP_HAS_DEFAULT_PORTS
true in env.plist
to no avail but it is possible I had the permissions wrong.
In any case apache won't launch because of httpd (pid 82785) already running
and need to stop the Server apps own apache:
sudo launchctl unload -w /Applications/Server.app/Contents/ServerRoot/System/Library/LaunchDaemons/com.apple.serviceproxy.plist
I had to run command "csrutil disable" in Terminal after rebooting and hitting CMD+R and then I was able to edit this file.
In catalina I had to mount the root to read/write using sudo mount -uw /
after disable csrutil.
I'm getting the same issue.
Solution: For my case (same than SubhajitSarkar)
I have put the environment variables in the ~/.profile (I'm using zsh on M1)
brew install httpd # run as non-root user on port 8080
...
nano ~/.profile
...
export MY_VAR="test"
...
source ~/.profile
apachectl stop
apachectl start
# php 8.x code
...
echo getenv('MY_VAR')
...
Output: test
And now it is working properly on Monterrey.
It did the trick for me. :)