Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

  1. mkdir /home/user/baz.example.com/
  2. ?????
  3. Profit

Where I wouldn't need to do anything but simply make the directory.

share|improve this question
    
How should that work? Do you really have a directory /home/user/slksdjfkjk.example.com/? Or do you have only a defined subset of valid names for *? –  mailq Aug 5 '11 at 21:14
    
@mailq I've updated my question to reflect your request. –  Marco Ceppi Aug 5 '11 at 22:35

4 Answers 4

up vote 4 down vote accepted

VirtualDocumentRoot should do the trick.

<VirtualHost *:80>
    ServerName catchall.invalid
    ServerAlias *
    VirtualDocumentRoot /home/user/%0
</VirtualHost>
share|improve this answer
    
Brilliant thank you! –  Marco Ceppi Aug 6 '11 at 5:58
    
Works perfectly on Mac OSX 10.7. Thanks again. –  Fedir Mar 6 '12 at 21:58

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.

share|improve this answer
    
I personally am against storing configuration in databases. Flat files seem superior! What if the database crashes, etc? I appreciate the answer but this isn't exactly what I'm looking for. –  Marco Ceppi Aug 6 '11 at 0:17
1  
It all depends on your point of view. What if Apache crashes,etc? Or what if too many typos are made in flat files? One size does not fit all. –  Aaron Aug 6 '11 at 3:16
    
If not a database, then could this be modified to use XML or another structured format? (But then, wouldn't that be just as complicated and inflexible as httpd.conf?) –  Stefan Lasiewski Aug 6 '11 at 3:44

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/

share|improve this answer
    
Thanks, I've accepted that answer as indicated by the check mark! –  Marco Ceppi Oct 13 '11 at 20:43

If you "only" want to do what you expressed in the last section of your question, then just do it like that:

<VirtualHost *:80>
   ServerName *.example.com
   DocumentRoot /home/user/foo.example.com
</VirtualHost>

And you are set.

share|improve this answer
1  
No I want to be able to create bar.example.com, baz.example.com, etc on the fly without changing apache. Hence why the wild card is in the documentroot definition as well. I've seen (mt) do this on their GridServer service. Curious if it's possible. –  Marco Ceppi Aug 5 '11 at 23:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.