Apache & DNS Wildcard Hosting with Mac OS X

Problem: You want to host all of your web development sites in ~/Sites, and you don’t want to fuck around with DNS and Apache configuration every time you start something new.

Solution: DNS wildcards and Apache Virtual Hosting!

The easier bit: get dnsmasq going

Follow the directions here. Briefly:

  1. sudo port install dnsmasq
  2. edit /opt/local/etc/dnsmasq.conf
    Add address=/.dev/
  3. sudo port load dnsmasq
  4. Add to the operating system’s DNS list.
  5. sudo dscacheutil -flushcache
  6. ping foo.dev to test it.

The easy bit: configure Apache

Next up, configure Apache to respond to any request that matches *.dev/foo/bar.

Edit apache’s httpd.conf file and enable the virtual host include by uncommenting the include line:

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

And then edit the httpd-vhosts.conf file. Comment out all the stuff that’s there (it’s useful as an example) and add the following:


    VirtualDocumentRoot /Path/to/Sites/%-2+


“%-2+” is magic: it means the part of the domain name before the last part. For foo.bar.dev it would be foo.bar.

Note that if you have Apache’s mod_unique_id enabled you might be getting errors in Apache’s log file that look something like this:

[Tue May 14 15:00:02 2013] [alert] (EAI 8)nodename nor servname provided, or not known: mod_unique_id: unable to find IPv4 address of "bernard"
Configuration Failed

Bernard is the local name of my computer. I added an entry in /etc/hosts for bernard, and everything worked out. Magic.    bernard