Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save srayhunter/5208619 to your computer and use it in GitHub Desktop.
Save srayhunter/5208619 to your computer and use it in GitHub Desktop.
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
Copy link

Nice!

@abe4tawa8
Copy link

Thank you.

@fabarea
Copy link

fabarea commented Feb 26, 2014

It did the trick for me. :)

@neohunter
Copy link

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

@alex-shamshurin
Copy link

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
Copy link

Works fine for me on Yosemite

@gido
Copy link

gido commented Dec 30, 2014

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

@bdone
Copy link

bdone commented Jul 22, 2015

worked perfectly for 10.10.4! thank you!

@alex666s
Copy link

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

@piotrczyz
Copy link

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

@piotrczyz
Copy link

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
Copy link

frazras commented Jan 18, 2017

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

@SubhajitSarkar
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
Copy link
Author

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

@malhal
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

@dzero1
Copy link

dzero1 commented Apr 30, 2021

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.

@hrodrig
Copy link

hrodrig commented Apr 27, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment