There are plenty of tools for doing local development, however some of them place some inconvenient limits on what you can do. If you aren’t afraid to edit a few config files then you can have an incredibly flexible local development in just a few steps.

At the end of this tutorial you’ll be able to create a folder, following the simple ~/Sites/MYFOLDERNAME.dev pattern, and have it just work in the browser.

Setting up Apache

OSX ships with Apache pre-installed, but in it’s default configuration it’s not very useful. Additionally at some point in the near past, Apple removed the ability to toggle Apache’s state from System Preferences, we’ll be solving that as well.

These instructions assume that you’ve never made any changes to the Apache configuration. If you have, your mileage may vary.

Create a Sites directory in your home directory if you don’t already have one.

Create a blank httpd-vhosts.conf file, we’ll come back to this in a bit.

Create a symbolic link for our previously created conf file.

Setup the logs directory, and set it’s permissions. These are probably a little too permissive, feel free to adjust as needed.

Add the following to the previously created httpd-vhosts.conf file. Make sure to replace USERNAME with your own user name.

Obviously feel free to change around the log format to best fit your application. The key to this working is the %0 at the end of VirtualDocumentRoot, acting as a placeholder for the domains you’ll be using locally.

At this point you should restart Apache to the previous configuration changes will be applied.

Setting up Dnsmasq

You could potentially stop without executing any of the following steps and have dynamic vhosts. However, every time you created a new domain you’ll need to update your hosts file. That’s no fun. Installing Dnsmasq is relatively straightforward.

Install Dnsmasq.

Copy the default configuration to its new home.

We’re going to be using the .dev domain for all local domains. Add the configuration to the newly created dnsmasq.conf file. If you want the .dev domains to point to a different address (i.e. A Vagrant instance), other than your local, just change 127.0.0.1 to your preferred location.

Now we need a place to let Dnsmasq know about our new resolvers. Additionally we’ll add our first resolver, .dev. Again, don’t forget to change 127.0.0.1 to the address you specified in the previous step if you intend to use something different.

Dnsmasq needs to start, and we want it to start at startup.

If you need to restart Dnsmasq run the following.

Great! You can check to see that resolver is working with scutil --dns. You should see an additional resolver at the bottom, your exact number might vary.

Additionally you can ping any domain ending in .dev and it should resolve to 127.0.0.1

Final setup

You can now create any series of folders ending in .dev in your sites directory. The contents of those folders will be served on the corresponding domain. If you have a folder called test.dev then your domain will also be test.dev

Enjoy your new automatic local development environment.