A lot of my personal projects are built with PHP. It’s very easy to prototype and get something moving quickly. I still love working with it, but one of its biggest pain points is that it’s almost always a server module on something like Apache or Lighttpd, and thus applications are run out of directories in your document root. That becomes a problem when working with resources and links, and I used to have to create configuration files and link helpers for each project. Wouldn’t it be nicer to just treat everything like it’s being run right out of the document root? A simple approach is to just make an entry in /etc/hosts for each project, but that gets tiresome. A less tedious approach follows:
To get started, download dnsmasq and go through the untar, make and make install rigmarole. We’re going to use it to set up our own top-level domain, dev, and map domains to folders in the document root, e.g., http://project.dev will map to /var/www/project/. Once dnsmasq is built and installed, open up /etc/dnsmasq.conf and add this line:
address=/.dev/127.0.0.1
Once that’s added, open up /etc/resolv.conf and make this the first entry:
nameserver 127.0.0.1
After you’ve updated your dnsmasq and resolv.conf configuration, you need to update your HTTP server to react to these incoming requests. I use Lighttpd, and mod_evhost makes this pretty easy:
# Enable *.dev subdomain to directory mapping
$HTTP["host"] =~ "\.dev" {
evhost.path-pattern = "/var/www/%2/"
}
Once that’s all done, restart your server. Now it’s time to run dnsmasq itself. To make sure it works, enter this on your command line to start it up and force it to not run as a daemon (as it would by default):
# dnsmasq -d -a 127.0.0.1
Now give it a shot! If your project lives in /var/www/foo/, navigate your browser to http://foo.dev and it should work just fine as a top-level entity. If it works, you can kill that dnsmasq process and start it without the -d switch to run it as a daemon.
For the curious, this is all happening on an Ubuntu machine running Lighttpd 1.4.19 and dnsmasq 2.55. If anyone has the configuration for other servers, I’ll gladly add it.