LAMP on Mountain Lion with Homebrew

I got my Retina Macbook Pro last week and migrated from my [t]rusty 2007 Whitebook. I had been using MacPorts on that machine and decided to give a try to Homebrew instead. To setup the LAMP stack with homebrew, the best guide to follow is Roland Atvar’s guide .

Then, Mountain Lion happened. And my LAMP stack started to act up.

Because Web Sharing is Gone

The handy web sharing option inside System Settings is gone, or more exactly OS X Server has swallowed it. You can get it for 20 bucks but just to start Apache is not worth it.

Being a smartass, I just went to the terminal and started apache:

sudo apachectl start

But for some reason apache was trying to load files from /usr/htdocs instead of the DocumentRoot I had setup at /Users/joaquin/Sites, from the apache error logs:

[Wed Aug 01 13:09:54 2012] [error] [client ::1] File does not exist: /usr/htdocs

After poking around in apache’s configuration, I noticed that the DocumentRoot configuration was enclosed in IfDefine tags:

<IfDefine !MACOSXSERVER>
<IfDefine WEBSHARING_ON>
    [...]
</IfDefine>
</IfDefine>

After reading a bit about Apache’s IfDefine I noticed that they check for parameters set while launching Apache:

The parameter-name argument is a define as given on the httpd command line via -Dparameter- , at the time the server was started.

So launching apache in the following way solved the issue:

sudo apachectl -D WEBSHARING_ON -k start

If you want apache to load automatically on login, you’ll need to modify the file /System/Library/LaunchDaemons/org.apache.httpd.plist to send the parameter:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//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>
    </dict>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/sbin/httpd</string>
        <string>-D</string>
        <string>FOREGROUND</string>
        <string>-D</string>
        <string>WEBSHARING_ON</string>
    </array>
    <key>OnDemand</key>
    <false/>
    <key>SHAuthorizationRight</key>
    <string>system.preferences</string>
</dict>
</plist>

Setting up PHP with Homebrew

Following the instructions in José Gonzalez’s Homebrew-PHP GitHub repo worked, mostly.

Mostly?

Another piece of software that Apple removed from OS X is X11 which Homebrew requires (or likes):

Warning: X11 not installed.

You don’t have X11 installed as part of your OS X installation.

This is not required for all formulae, but is expected by some.

Apple directs you to install XQuartz as a replacement .

After this, the Homebrew-PHP instructions worked. To get mysql support in PHP, do:

brew install php53 --with-apache --with-mysql