I’ve done a fair amount of web “design” (mostly implementing designs of others) and development in the past, I usually set up a lot of virtual hosts in my local apache. I’ve done that in three different ways.
The quick&dirty hosts file
Point any of your development domains to 127.0.0.1 in the /etc/hosts file. It’s the easiest way, but you need to add them one by one. At some point, mine could look like this:
127.0.0.1 warp.dev 127.0.0.1 ebox-platform.dev 127.0.0.1 ebox-technologies.dev 127.0.0.1 jorgebernal.dev 127.0.0.1 projectA.dev # ... and so on
Getting smart with dnsmasq
This is a more automated method. You install dnsmasq and configure 127.0.0.1 as your DNS server. Then add this to your conf:
address=/.dev/127.0.0.1
This worked well, and acted as a dns cache. But I had some trouble with dynamic dns entries at our old office: projects.warp.es would point to a local address inside the office and our remote IP from outside, so I found myself clearing the cache too often.
42foo: the zero-code web service
So I made it external. I bought 42foo.com and set up a bind zone with this:
@ A 127.0.0.1 * A 127.0.0.1
So warp.42foo.com, ebox-platform.42foo.com or whateveryourprojectis.42foo.com always point to 127.0.0.1
You still have to set up the virtual host, but there is one step less for web development. Feel free to use it, and let me know if you set up something similar with a shorter domain name
Thanks for the notes. I’ve tried to find different ways to handle this. I eventually found the /etc/hosts file easy enough to script adding/removing entries from. I have an addwebhost script that just appends the dns and copies a template vhost file into apache’s config.
I recently wrote a similar tutorial which includes a shell script to set up the /etc/hosts entry and virtual host. If you removed the line that writes to /etc/hosts and used a *.42foo.com domain, it could automate the apache set-up for you.
http://blog.kevinmehall.net/2009/apache-local-vhosts
The problem herein lies that I, and also most people I’ve talked to about this, don’t use 127.0.0.1, but their LAN IP – so that you can have other machines and colleagues access that as well.
Which doesn’t really make it easier, or even possible if you have dhcp with a low lease time, but still I found myself editing the hosts file too often to even start with the 127.0.0.1 approach
Pingback: Local development – multiple vhosts - NiGhTHawK
Very useful, thanks!
Thanks, using 42foo.com in conjunction with this script to create wildcard subdomains:
https://gist.github.com/1593837
Not sure if I follow, but if it does what I think it does, you can use VirtualDocumentRoot for that
http://httpd.apache.org/docs/2.0/vhosts/mass.html
So currently 42foo.com and Apache virutalhosts we can point to many different hosts using subdomains, but since I work on many projects on the fly I didn’t want to have to create all the virtual hosts. So I created that script so we could dynamically use sub domains based on directories.
Examples:
this:
/home/user/projects/project-category/project
becomes:
http://project.project-category.42foo.com
this:
/home/user/projects/project-category/project/branch
becomes:
http://branch.project.project-category.42foo.com