Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm in the process of writing a script which I'm going to roll out on a load of web servers. This will allow me to host thousands of sites on each one of them... Part of the script is adding new domains and subdomains into a database. While I'm doing this I either need to [a] add each domain to my virtualhosts using php (I don't know how to do that), or [b] add some kind of wildcard DNS where any domain pointed to my server using an A record will go to the same file/directory, where it will be sorted using htaccess and PHP (the better option I think). Any ideas?

It needs to be completely database driven. So that when you go to it will serve up which will pull and display some content from MySql.

It needs to be like that for reasons which aren't worth getting in to, so presumably I will need to do some vhost stuff to make everything go to and do the rest with htacess/modrewrite?

share|improve this question
up vote 1 down vote accepted

Have you looked at mod_vhost_alias?

You might not need to reinvent the wheel.

share|improve this answer

I have somewhat a similar setup allowing me to roll thousands of domains within a specific platform which I have developed.

(Take note that if you'd like to have multiple dynamic subdomain this can accomplished with a wildcard DNS entry * pointing to an IP and a ServerAlias in one of your virtualhost * - but since this is pretty simple I won't go any further and explain how a setup on dynamic domain can be setup)

I am not going to tell you what is the best way since I believe there is no such things, there are multiple ways to do what you're trying to achieve: I just hope that by shedding some light over my setup, it might help you.

Adding the domain to the DNS server should be automated by some script (I prefer having that functionality in a bash script and keep that separated from PHP or any other language).

The script that take care of everything is too long but the rational is simple:

Add a domain
1. Calculate the Serial number (date + some incremental number)
2. Check if domain exists
3. Validate the IP (if you are going to use multiple IP across your system, if not you can have this bit static)
4. Add to named.conf the domain zone reference
5. Create the zone.db file

Remove a domain
1. Decrease the serial
2. Remove the domain zone reference from named.conf
3. Remove the zone.db file

Since Serverfault doesn't really show bash script in a good manner (and my script is long anyway), I have posted this to pastebin, you can find the script here

Then you need to take care of the virtualhost, it's pretty easy if you're using apache 2.2, you just need to write a script that create a $domain.conf file where all the directive are specified.

The third stage would be to use some kind of RewriteRule that will pass the domain to an index.php where you'll be able to internally redirect that domain to the relevant structure.

I believe in this schema since this is a schema that will allow you to easily do changes and tweaks in the future.

Hope that helped.

share|improve this answer

If you want the same settings to apply to any web request, regardless of the hostname, then you can just set the configuration parameters you want outside of a vhost stanza, which means it applies "by default" for any request that doesn't match any of the defined vhosts.

share|improve this answer

Name based vhosting in combination with wildcard DNS can be a very powerful combination. I had a server with 4500 websites on it, and the config for it was maybe 5 lines longer than a normal httpd.conf, plus the wildcard setup in BIND. The best part was that once it's set up, you don't need to reconfigure anything anymore, just make another directory named like the domain you want it to be called, put some content in there, and you're done.

share|improve this answer
see edited post :) – zuk1 Jul 28 '09 at 14:05

Your Answer


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.