Local *.dev and *.app via Dnsmasq on Mac OS X

I am in the process of setting up a development environment on a new machine and one of the things I decided to do is route all .dev domains to my local (built-in) Apache web server while I route all .app domains to my Laravel Homestead Vagrant environment.

To never have to monkey with your hosts file, Apache can be configured to serve any .dev domain that matches an existing folder name from a base directory. The Homestead.yaml file handles domain-to-directory mapping explicitly for the Laravel Vagrant environment.

This ended up being really easy – the things I needed to know were:

  1. what to put into the dnsmasq config
  2. how to stop and start dnsmasq

All of the “install Dnsmasq mac” tutorials out there seem to work fine. Assuming, you’ve already installed Homebrew, it’s as simple as:

brew install dnsmasq

From there, you’ll follow the recommended step from the installer output to create your config file:

cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf

Now open that file in your favorite editor and replace its contents with:

# General sites (Apache)

# Laravel Homestead (Vagrant)

Lastly, stop and start Dnsmasq to apply the changes in the config:

sudo launchctl stop homebrew.mxcl.dnsmasq
sudo launchctl start homebrew.mxcl.dnsmasq

That’s it! Or at least that’s all it took for me. I hope your experience is as easy as mine was.

Now to set up Apache to handle anything with a .dev suffix from a matching directory from my Sites dir. Add this to your vhost configuration:

<Directory "/Users/example/Sites/">
  AllowOverride All
  Options Indexes MultiViews FollowSymLinks
  Require all granted

<VirtualHost *:80>
  ServerName local.dev
  ServerAlias *.dev
  UseCanonicalName off
  VirtualDocumentRoot /Users/example/Sites/%-2.0.dev/

Restart Apache with errors returned to find errors in your config:

sudo apachectl -e info -k restart

If you get a 403 error when loading your site in a browser, make sure that every dir in the chain to the web root has the “execute” permission. Add it via:

chmod +x dirname

That should do it.

Improvements/corrections to the above solution are welcome. Enjoy!

Comments Off on Local *.dev and *.app via Dnsmasq on Mac OS X

Categories Development