3

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.

Marco Ceppi
  • 457
  • 3
  • 19
  • 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 05 '11 at 21:14
  • @mailq I've updated my question to reflect your request. – Marco Ceppi Aug 05 '11 at 22:35

4 Answers4

4

VirtualDocumentRoot should do the trick.

<VirtualHost *:80>
    ServerName catchall.invalid
    ServerAlias *
    VirtualDocumentRoot /home/user/%0
</VirtualHost>
Shane Madden
  • 114,520
  • 13
  • 181
  • 251
2

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.

grufftech
  • 6,760
  • 4
  • 37
  • 37
  • 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 06 '11 at 00: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 06 '11 at 03: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 06 '11 at 03:44
0

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/

gerard
  • 101
  • 2
-1

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.

mailq
  • 17,023
  • 2
  • 37
  • 69
  • 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 05 '11 at 23:09