Easier vHost setup

Starting development on a new web project is a pretty mundane task: Probably cloning some existent repository or creating the initial folder structure and then setting up a local virtual host, so each project got it’s own development domain, like fancyproject.dev.

Especially the last part is quite annoying: Manually editing the hosts-file and adding a new vHost in the Apache config files.

After years of just going with this workflow, I found a way to cut this whole process. Using dnsmasq and wildcards in the Apache config do the same thing without having to change a single line of config files.


dnsmasq is a little DNS server and I only use it to route all request targeted at *.dev to the local machine and thereby make editing the /etc/hosts file redundant.

For installing it on a Mac I suggest using Homebrew, following these steps (mostly in the terminal of your choice):

$ brew install dnsmasq

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

Editing /usr/local/etc/dnsmasq.conf and adding at the end:

# dnsmasq.conf
# ...

Then adding dnsmasq to the startup elements by executing $ sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons

To start it this time, execute $ sudo launchctl start homebrew.mxcl.dnsmasq

It might be necessary to add as the first DNS-Server in your Network Settings.

Apache Config

As it turns out you can use wildcards in the Apache config to automatically map all <project-name>.dev to .../projects/<project-name>/. Far better than editing the httpd-vhosts.conf all the time.

The single VirtualHost entry necessary is this one (Keep in mind to change the paths):

NameVirtualHost *:80

<VirtualHost *:80>
    <Directory /<path-to-your-projects-root>/>
        Options Indexes MultiViews FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    UseCanonicalName off
    ServerName localhost
    ServerAlias *.dev
    VirtualDocumentRoot /<path-to-your-projects-root>/%1/

Voilá! Now all I need to do is create a new directory and drop the files in. The vHost comes automatically.