Blog

OSX Apache Not Working

With the rise in popularity of HTML5, javascript and CSS, I’ve found myself working on a fair number of Web-related projects recently.  I spend roughly half of my time in Windows-based environments and the rest in OSX or various Linux distros.  Unsurprisingly, most of these projects use LAMP as the primary technology stack.

Disclaimer: Before trudging down the [potentially] painful, and somewhat manual, path of setting up a full LAMP stack on OSX, it’s worth pointing out that there are a number of products – most notably MAMP – that can greatly simply this for you.  I like understanding the various components, and how to configure them (as I’ll most likely need to do this on our production boxes anyway…), but doing so isn’t always a prudent use of time/energy, particuarly when your development environment differs from staging and production.

On this latest project, I happened to be on an OSX (Mavericks) kick and simply wanted to do my development there, using Apache/PHP.  Fortunately, OSX comes with Apache support, out-of-the-box.  Sure enough, following some basic tutorials yielded an “It’s working!” page from http://localhost.  Awesome.

At this point, I typically make edits to my virtual hosts configuration, to support simultaneous development via different hostnames and/or ports.  Sure enough, I added another virtual host configuration and when I tried to hit that hostname in my browser, nothing…. zip, zilch, nada.

I’m well used to Apache failing for a variety of reasons, but typically you get something back – an HTTP error response – 500, 403, etc. – or the error log will have something interesting.  In this case, it almost seemed like Apache simply wasn’t running or listening for requests.  However, a query to the service indicated that it was, in fact, running. Something isn’t right here…

This was frustrating on a number of levels; I was just trying to serve up static HTML.  I diligently reviewed both my httpd.conf and httpd-vhosts.conf settings and all looked well.  As with debugging anything, I started peeling back changes I had made.  One of the first was to disable my vhosts configuration.  When I did that, wham, everything worked again.  So I had narrowed it down to my virtual hosts configuration.  The vhosts config was extremely basic; nothing seemed amiss.

After some further digging, I found two indispensable commands.  The first is incredibly basic:

httpd -V

Unlike the simple ‘-v’ argument, which simply outputs version information, ‘-V’ outputs a plethora of service configuration, which is handy for basic sanity checking. In my case:

Server version: Apache/2.2.24 (Unix)
Server built: Aug 24 2013 21:10:43
Server's Module Magic Number: 20051115:31
Server loaded: APR 1.4.5, APR-Util 1.3.12
Compiled using: APR 1.4.5, APR-Util 1.3.12
Architecture: 64-bit
Server MPM: Prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_FLOCK_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=128
-D HTTPD_ROOT="/usr"
-D SUEXEC_BIN="/usr/bin/suexec"
-D DEFAULT_PIDLOG="/private/var/run/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="/private/var/run/accept.lock"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="/private/etc/apache2/mime.types"
-D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"

Well, everything looks good there. Still, there’s definitely something not right with my virtual hosts configuration. Other Linux distros typically tip me off when there’s a warning/error in the file, when apache is restarted, but not so on OSX. After some more digging, I found the magic command:

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

This has the effect of displaying any error output from apache that isn’t written to the log. Sure enough, this special sauce saved my bacon. Running the above command yielded:

+ ARGV='-k start'
+ HTTPD=/usr/sbin/httpd
+ test -f /usr/sbin/envvars
+ . /usr/sbin/envvars
++ test x '!=' x
++ DYLD_LIBRARY_PATH=/usr/lib
++ export DYLD_LIBRARY_PATH
+ LYNX='lynx -dump'
+ STATUSURL=http://localhost:80/server-status
+ ULIMIT_MAX_FILES=
+ LAUNCHCTL=/bin/launchctl
+ LAUNCHD_JOB=/System/Library/LaunchDaemons/org.apache.httpd.plist
+ '[' x '!=' x ']'
+ ERROR=0
+ '[' 'x-k start' = x ']'
+ case $ARGV in
+ /usr/sbin/httpd -k start
Warning: DocumentRoot [/usr/docs/dummy-host.example.com] does not exist
Warning: DocumentRoot [/usr/docs/dummy-host2.example.com] does not exist
Syntax error on line 52 of /private/etc/apache2/extra/httpd-vhosts.conf:
order takes one argument, 'allow,deny', 'deny,allow', or 'mutual-failure'
+ ERROR=1
+ exit 1

Bingo, according to this, line 52 in my vhosts file was incorrect. Sure enough, after subsequent review, my line included “Order allow, deny”, instead of “Order allow,deny”. Wow. One space (and an apachectl restart) later and everything is right as rain. It really is the small stuff that gets you.

One Comment

  • Alvaro Cavalcanti on Jul 09, 2014 Reply

    Thank you so much, saved my life. 🙂

Leave Reply