Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Configure Apache on Mac OSX with Environment Variables

Configure Apache Path Environment Variable on Mac OSX

Problem: Apache2/PHP did not find a program to execute on its configured path

Solution: Add a new path of /usr/local/bin to Apache2's path where the program was installed

  1. Edit the Apache2 plist file with whatever editor you like (example using vim):

     $ sudo vim /System/Library/LaunchDaemons/org.apache.httpd.plist
    
  2. 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>
    
  3. Restart Apache2

     $ sudo /usr/sbin/apachectl stop
     $ sudo /usr/sbin/apachectl start
    
@jasonholmberg

This comment has been minimized.

Copy link

jasonholmberg commented Mar 20, 2013

Nice!

@abe4tawa8

This comment has been minimized.

Copy link

abe4tawa8 commented Oct 24, 2013

Thank you.

@fabarea

This comment has been minimized.

Copy link

fabarea commented Feb 26, 2014

It did the trick for me. :)

@neohunter

This comment has been minimized.

Copy link

neohunter commented Oct 1, 2014

This should be added at the end? or inside the main block?

@alex-shamshurin

This comment has been minimized.

Copy link

alex-shamshurin commented Oct 18, 2014

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

@andyman3000

This comment has been minimized.

Copy link

andyman3000 commented Dec 1, 2014

Works fine for me on Yosemite

@gido

This comment has been minimized.

Copy link

gido commented Dec 30, 2014

Like @salex772 it doesn't work for me on 10.10.1. Any idea ?

@bdone

This comment has been minimized.

Copy link

bdone commented Jul 22, 2015

worked perfectly for 10.10.4! thank you!

@alex666s

This comment has been minimized.

Copy link

alex666s commented Oct 25, 2015

Don't work on el Capitan. /System/Library/LaunchDaemons/org.apache.httpd.plist cannot be edited (rootless)

@piotrczyz

This comment has been minimized.

Copy link

piotrczyz commented Nov 14, 2016

It doesn't work for me on el Capitan as well.

@piotrczyz

This comment has been minimized.

Copy link

piotrczyz commented Dec 7, 2016

I had to run command "csrutil disable" in Terminal after rebooting and hitting CMD+R and then I was able to edit this file.

@frazras

This comment has been minimized.

Copy link

frazras commented Jan 18, 2017

@piotrczyz was right, disabling csrutil works for el capital as he instructed

@SubhajitSarkar

This comment has been minimized.

Copy link

SubhajitSarkar commented Jun 21, 2019

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

  1. I have put the environment variables in the ~/.bash_profile
  2. source ~/.bash_profile
  3. sudo apachectl stop
  4. sudo apachectl start

And now it is working properly on Mojave.

@srayhunter

This comment has been minimized.

Copy link
Owner Author

srayhunter commented Jun 21, 2019

@SubhajitSarkar are you trying to add an environment variable for Apache2 to use or for your terminal?

@malhal

This comment has been minimized.

Copy link

malhal commented Feb 9, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.