read

This article explains 3 steps to set-up your environment to handle dynamic virtual
hosts. You'll never again need to touch vhosts, or /etc/hosts file
to set up any new project.

At the end of these steps, you will be able to access
http://your-project-name.dev for any project in your project's path.

If you're on Linux, there is a complete guide for you here.

Step 1: dnsmasq

You'll need homebrew to install dnsmasq. Go to the website and
install it if you don't have it installed.

# update homebrew
brew up
# install dnsmasq.
# BE SURE TO FOLLOW THE FINAL INSTRUCTIONS AT THE END OF THE INSTALLATION PROCESS
brew install dnsmasq

After following the instructions to copy dnsmasq.conf and LaunchDaemon files into your
system, let's configure /usr/local/etc/dnsmasq.conf to respond for .dev
top-level-domains:

# /usr/local/etc/dnsmasq.conf
address=/dev/127.0.0.1

Step 2: DNS resolver

sudo mkdir -p /etc/resolver
sudo tee /etc/resolver/dev >/dev/null <<EOF
nameserver 127.0.0.1
EOF

To test if dnsmasq + resolver are working, try to ping any host .dev

# You shouldn't have broken your DNS
ping www.google.com
64 bytes from 173.194.118.51: icmp_seq=0 ttl=58 time=45.689 ms
# .dev TLD should refer to 127.0.0.1
ping my.custom.domain.dev
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.045 ms

Step 3: webserver virtual-host wildcard

# /etc/apache2/users/<my-user>.conf
<Virtualhost *:80>
    VirtualDocumentRoot "/Users/<my-user>/Projects/%1/public"
    ServerName vhosts.dev
    ServerAlias *.dev
    UseCanonicalName Off
    <Directory "/Users/<my-user>/Projects/*">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</Virtualhost>

Reboot your apache, and you're done!

References:

Blog Logo

Endel Dreyer

Full-stack developer. Loves Ruby and JavaScript.


Published

Image

bugfixer / @endel

About programming, tools and solving problems.

Back to Overview