The ‘Apache on OS X Mountain Lion’ problem

Whoah, I just dived into severe problems with the apache web server on my MacBook Air, running OS X 10.8 – Moutain Lion.

In preparation of my sessions “JavaScript” and “HTML5” at the EKON 16 conference in november I wanted to set up the web server that is in fact included in the OS X installation.

In previous versions of OS X there was a ‘Web sharing’ option in the system preferences, but this was removed in Mountain Lion. There are several posts out there in the wild showing how to manually enable apache and PHP. I found this instruction on the intertubes and read (but not exactly followed) it.

In my megalomania I went: “I already did that, it can’t be that difficult now. Lets go.” (several years ago I set up apache with PHP 3.something on a Linux system).

So this is what I did:
I skipped the first part of the instruction (starting apache) and directly went to the part where I enabled the user-specific /Sites directory. I set up the directory with all required options, allo/deny rules etc. and THEN I tried to start the web server.

Guess what? It didn’t work.

So the next thing was to look for the error logs (remembering I once had experience with Linux and error logs were a good hint on where to look for my stupid errors). Sadly, the error log folder was empty. So I double-checked the apache config for alternate log folder configurations – and found none.

A experienced apache administrator would guess that there is something wrong with the config file itself, so that apache would not even know about a configured error folder to put its logs into – but I again needed to search for starting problems with apache: Port already taken? Nope. Wrong host name? Nope. It was quite a bit of trial and error until I found this blog post about troubleshooting apache on Mountain Lion. It hinted me to this little command:

sudo bash -x /usr/sbin/apachectl -k start

Starting apache this way prints the messages directly onto the console – and so I could see where apache failed to start. It was in the user-specific /Sites config. The problem was a simple typo in the closing tag (I hacked in ‘diectory’).

So far, so good. Apache launched, my user website worked at my http://localhost/~Sebastian url – but it responded with a 403 – ‘Not authorized’ every time I accessed the folder. Strangely enough, it delivered files when I directly navigated to them (i.e. localhost/~Sebastian/test.html).

In several other attempts I found that the default configuration for the root folder disallowed directory listings and did not allow overwriting this, and how to enable PHP (that commented out entry was hidden very well).

Struggling with apache configs distributed to several locations, unix command lines, nano and a extremely bloated apache documentation I am really grateful that I usually work with IIS in my day to day business. Gladly I don’t have to set up mod_mono on my MacBook Air (yet).