I've noticed some hosting providers have Apache setup so that if I create a directory (foo.example.com) Apache will automatically know that the DNS entry for foo.example.com routes to that document root.
What I am to do is setup something like this:
<VirtualHost *:80>
ServerName *.example.com
DocumentRoot /home/user/*.example.com
</VirtualHost>
Where the DocumentRoot would match based on the pattern from the ServerName wild card.
In doing so all I should need to do is create /home/user/foo.example.com
after the configuration is in place and not need to modify anything in Apache. This would allow me to add sub-domains on the fly without needing to restart or reload or even edit anything in Apache.
A use case would be something like this.
mkdir /home/user/baz.example.com/
- ?????
- Profit
Where I wouldn't need to do anything but simply make the directory.
VirtualDocumentRoot
should do the trick.Last website company i worked with did something similar, instead of hardcoding it in the apache configuration, we wanted to store the configuration in the database, allowing us to update it any time we want and it would adjust whenever we needed.
http://blog.straylightrun.net/2009/07/31/storing-apache-virtual-hosts-in-a-database/
is a blog post we used to accomplish it.
http://code.google.com/p/dbd-modules/wiki/mod_vhost_dbd
mod vhost dbd wiki, good info on storing your vhost configurations in the database.
I guess I don't have enough karma to add a comment, so I will add an answer. Steve Madden's answer is correct.
If you want to create a development domain on your local machine (e.g. mycomputer.dev), you can use the
VirtualDocumentRoot
trick along with running a local DNS server to do so. Then, when you want to start a new website, just create the directory. No editing of Apache, no editing of hosts files, no Apache restart.This blog post is a how-to for *nix:
http://blog.sznapka.pl/dynamic-lamp-setup-for-localhost-development/
And a how-to on Windows:
http://blog.straylightrun.net/2010/05/10/throw-away-your-hosts-file-developing-locally-with-bind/
If you "only" want to do what you expressed in the last section of your question, then just do it like that:
And you are set.