Archive

When I first started using a web server on my local machine for development, I used to create a separate virtual host for every site I worked on. Then I learned that you can use a ServerAlias with a wildcard to handle multiple sites with a single virtual host. Since then, I’ve been using the following simple configuration to test most of my projects:

Apache

1

2

3

4

5

6

7

8

<VirtualHost*>

ServerNametmp.code.dev

ServerAlias*.code.dev

VirtualDocumentRoot/path/to/projects/%1

<Directory/>

AllowOverrideAll

</Directory>

</VirtualHost>

This tells Apache that any domain ending with .code.dev should be served by the directory in /path/to/projects/ that has the same name as the beginning of the domain. For example, foo.code.dev would be served from /path/to/projects/foo/. This reduces my per-project setup to just adding an entry in /etc/hosts to map the custom domain to my local machine, e.g., 127.0.0.1 foo.code.dev. Unfortunately, wildcards can’t be used in the hosts file, so this step needs to occur for every project. That’s a little annoying, but it’s easy enough that I was willing to live with it.

As I started working on more sites (as opposed to libraries or frameworks that happen to need a web server), I created another VirtualHost to create nicer domains and handle subdomains. If I’m working on foo.com, I create a directory named /foo.com/ in my projects directory and use the domain local-dev.foo.com for testing.

Apache

1

2

3

4

5

6

7

8

<VirtualHost*>

ServerNamelocal-dev

ServerAliaslocal-dev.*

VirtualDocumentRoot/path/to/projects/%2+

<Directory/>

AllowOverrideAll

</Directory>

</VirtualHost>

Using %2+ tells Apache to use every part of the domain after the first part. You can read more about how the VirtualDocumentRoot interpolation works in the mode_vhost_alias documentation. And of course, I would add 127.0.0.1 local-dev.foo.com to my hosts file.

Testing from physical devices

Unfortunately, there is a big limitation to this setup. When testing from a physical device, such as a phone or tablet, the custom domain won’t map to your local web server. Since the physical device doesn’t know anything about the hosts file on the machine with the web server, trying to load foo.code.dev from the device’s browser isn’t going to work. This generally discouraged me from testing on physical devices because I never had a great solution to this problem. Whenever the need would arise, I’d create another virtual host using my machine’s LAN IP and a random port:

Apache

1

2

3

4

5

6

7

<VirtualHost192.168.1.2:1234>

ServerNamelocal-dev.foo.com

DocumentRoot/path/to/projects/foo.com

<Directory/>

AllowOverrideAll

</Directory>

</VirtualHost>

Then I can connect to 192.168.1.2:1234 instead of local-dev.foo.com from any physical device connected to my network. But I’ve lost the benefit of the wildcard aliasing and now I need to remember which port to use. This can be quite burdensome as the number of sites increases, especially since each site needs to use a different port.

xip.io to the rescue

xip.io is a free service from 37signals, which provides wildcard DNS for any IP address. Just like we can map *.code.dev to the appropriate directory on our local machine, xip.io can map *.xip.io to the appropriate IP for local testing. The URL structure is nice and simple, just prepend the IP address you want the domain to resolve to; the xip.io DNS server handles the rest. In this case, we’d use 192.168.1.2.xip.io to map the domain to 192.168.1.2.

This solves the DNS issue and removes the need to edit the hosts file for each site. But given a domain of 192.168.1.2.xip.io, we still don’t know which directory to server the site from. To address this, xip.io allows adding as many subdomains as you want. So we can use foo.com.192.168.1.2.xip.io and then create a virtual host that knows how to handle xip.io domains.

Apache

1

2

3

4

5

6

7

8

<VirtualHost*>

ServerNamexip

ServerAlias*.xip.io

VirtualDocumentRoot/path/to/projects/%-7+

<Directory/>

AllowOverrideAll

</Directory>

</VirtualHost>

Handling DNS on your local machine

Although the xip.io setup does allow for zero-configuration setup, there are a few caveats.

The URLs are long and a bit ugly. I’ve created a simple page, bit.ly/xipio, to generate the xip.io URLs so I don’t need to type the full URL in the tiny address bar on my devices. I also kept my local-dev.* virtual host enabled so that I can use shorter, nicer URLs if I want, though this does require adding an entry in the hosts file as mentioned above, and doesn’t allow testing from physical devices.

The URLs don’t work offline. Obviously if you can’t can’t connect to xip.io, the whole thing is going to fall apart.

The URLs aren’t shareable. Since the domain contains your personal LAN IP, sharing links with other developers working on the same site from their local machine won’t work (unless you happen to have the same LAN IP). However, it’s quite easy for the other person to change the IP after you’ve shared it with them.

More on link sharing

Link sharing is really useful when working on a project with other people, so it’s worth discussing specifically. As I mentioned above, while xip.io URLs can’t just be copy/pasted, they’re fairly easy to work with. However, having a common setup, such as local-dev.* for your projects makes link sharing much easier. If everyone working on the project is using local-dev.foo.com when testing from the main machine, then link sharing just works.

To avoid having everyone edit their hosts file to get the domain mapping to work, another option is to actually create a DNS record for local-dev.foo.com. By creating an A record that points local-dev.foo.com to 127.0.0.1, you can get back to a zero-configuration setup. However, you do have the same offline problem as using xip.io, but that can be solved via the hosts file for any developer that needs offline access to work.