Menu

One of the most painful things about working on multiple projects is setting up your virtual hosts. Constantly digging into httpd-vhosts.conf and /etc/hosts can get really annoying. To add to that pain, my environment for personal projects is much different to the environment I use for work projects. Here is a guide on how I managed to overcome these issues and now setting up for a new project with it’s own virtual host is as easy as running one command.

VirtualDocumentRoot

A common approach to this issue is to use a sort of “catch-all” virtual host that uses the VirtualDocumentRoot to change the document root of the virtual host based on the domain name.

Below are a couple of problems with this approach:

  • You have to resort to some wizardry to set the correct path in the $_SERVER[‘DOCUMENT_ROOT’] super global.
  • It locks you into using the same parent directory for all your projects.
  • It is not extremely compatible with different frameworks. For example Laravel uses the public directory and Symfony uses the web directory.

My Current Setup

I ended up using 3 tools to achieve my current setup. Together they work amazingly well and makes creating new virtual hosts extremely easy and extremely quick.

dnsmasq

The first issue I needed to solve was to get all .dev domain names to resolve to my local IP address. This is where dnsmasq comes in. As per their website:

Dnsmasq provides network infrastructure for small networks: DNS, DHCP, router advertisement and network boot.

The installation is quite easy on mac and can be done by running one command:

Once it has been installed Homebrew will give you further instructions and commands to run to start the service at boot and also set up the config file. If you somehow missed the commands you can always run the following to see them again:

The last step to complete the dnsmasq setup is to change the following in the config file:

In my setup it was around line 68 in the config file. The changes simply tells dnsmasq to respond with 127.0.0.1 every time a .dev domain has to be resolved.

/etc/resolver/dev

Now that our DNS server is in place we need to tell our system to use this DNS server for all .dev domains. Most unix based systems come with a very handy /etc/resolv.conf file. This file tells your system which DNS servers to use. However, most modern unix systems also allows you to specify additional config files in the /etc/resolver directory.

The setup is quite easy and can be done with the following command:

When the system gets a request for a .dev domain it will find the /etc/resolver/dev file and use the name server defined in it. In this case 127.0.0.1.

mkvhost

The last part of this setup is one of my own little scripts. You can find installation and usage instructions on it’s GitHub page.

Conclusion

With all the steps in place you can now set up a new virtual host by simply running the mkvhost command. The script allows for various options such as the DirectoryIndex the web directory etc. This can allow you set up a virtual host specific to your project without all the hassle of changing numerous files.

In my case I created aliases for work and personal projects. Now I can simply run the following and access the URL from my browser:

Comments (0)

Leave a comment

Your email address will not be published. Required fields are marked *

9 − 2 =