*HARD* Create dynamical subdomains using php.

  • webbmonarken
  • Beginner
  • Beginner
  • User avatar
  • Joined: Nov 02, 2007
  • Posts: 50
  • Loc: Sweden, Luleå
  • Status: Offline

Post July 2nd, 2008, 4:32 pm

Can this be done?
I need this to work with as small delay as possible eg, posting a form and directly after you got a new sub domain!

what changes needs to be done to the server side (if any)?
+ do you got a nice script for this that I can tweak (php) ? =)
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 2nd, 2008, 4:32 pm

Post July 3rd, 2008, 1:52 am

PHP can't help you in doing this. You can use the mod_vhost_alias module for the Apache server.
No Strings Attached: A JavaScript graphics demo.

Post July 3rd, 2008, 2:32 am

Actually, you *CAN* use php to do this, but I wouldn't recommend it. I wrote a script to do this for a former project I was working on, and for the most part it worked fine (because I was the only one who used it) but there were still some rough spots (See problem list below).

Make sure your domain is wildcarded (Accepts requests to any subdomain, regardless of whether it actually exists or not)
create a blank file, called subdomains.conf (or whatever you like) but make sure you don't put it in a web accessible directory (Your home dir should be fine), then chown it to the user that apache runs as (usually apache or www) so it's writable by php.

In you main httpd.conf, add the following line at the bottom:
Code: [ Download ] [ Select ]
Include /path/to/subdomains.conf


In your script that you want to create the subdomain, add code to create the subdirectory for the subdomain, and to add the subdomain vhost entry to subdomains.conf ($name is the subdomain you are going to create):
PHP Code: [ Download ] [ Select ]
    @mkdir ( "/path/to/subdirectory/".$name );
    $vhost = "<VirtualHost *:80>
    ServerAdmin <!-- e -->yourname@yourserver.com<!-- e -->
    DocumentRoot /path/to/subdirectory/".$name."
    ServerName ".$name.".yourserver.com
    ErrorLog /path/to/logs/".$name."_error_log
</VirtualHost>
";
    $fo = fopen ( "/path/to/subdomains.conf", "a" );
    $fw = fwrite ( $fo, $vhost );
    fclose ( $fo );
 
  1.     @mkdir ( "/path/to/subdirectory/".$name );
  2.     $vhost = "<VirtualHost *:80>
  3.     ServerAdmin <!-- e -->yourname@yourserver.com<!-- e -->
  4.     DocumentRoot /path/to/subdirectory/".$name."
  5.     ServerName ".$name.".yourserver.com
  6.     ErrorLog /path/to/logs/".$name."_error_log
  7. </VirtualHost>
  8. ";
  9.     $fo = fopen ( "/path/to/subdomains.conf", "a" );
  10.     $fw = fwrite ( $fo, $vhost );
  11.     fclose ( $fo );
  12.  


There are several potential problems with this approach though:
1. Apache will need to be restarted after each new subdomain is created. You can use php's execute or system commands to run apachectl restart, but this can cause problems if there will be several people creating subdomains. If one person creates a subdomain and submits it, the server will restart and may interrupt someone else creating a subdomain.
2. If something goes wrong with writing the subdomains.conf file, ALL of the subdomains could be lost.
3. You will need to be very strict with your subdomain naming rules, and filter them heavily, and prevent duplicates. If you don't do this, apache could fail to restart and then your whole server will be down.
4. Apache could fail to restart for some unforseeable reason.

A simpler approach would be to wildcard your domain so it responds to any subdomain, then use a header php script to read the $_HOST var, split it up on period (.) determine what the subdomain is and serve the users data that way.
  • spork
  • \x -> x `mod` o
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 5616
  • Loc: Rochester, NY
  • Status: Offline

Post July 3rd, 2008, 6:52 am

Couldn't mod_rewrite handle something like this, avoiding PHP all together?
Working on writing myself a Scheme
me

Post July 3rd, 2008, 11:48 am

spork wrote:
Couldn't mod_rewrite handle something like this, avoiding PHP all together?


Actually, yes:
Something like this should work:
Code: [ Download ] [ Select ]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.yourserver.com
RewriteCond %{HTTP_HOST} ([^.]+)\.yourserver.com
RewriteRule ^([a-zA-Z0-9\+]+)$ /%1
  1. RewriteEngine On
  2. RewriteCond %{HTTP_HOST} !^www\.yourserver.com
  3. RewriteCond %{HTTP_HOST} ([^.]+)\.yourserver.com
  4. RewriteRule ^([a-zA-Z0-9\+]+)$ /%1


Which should rewrite http://whatever.yourserver.com to http://www.yourserver.com/whatever/, and all you would have to do is make sure that /whatever exists. (Check my syntax, I haven't tested it, just typed it from memory)

Post July 4th, 2008, 12:41 am

mod_vhost_alias is supposed to do the same (haven't tested it myself) - it's like mod_rewrite for subdomains. So something like this:
Code: [ Download ] [ Select ]
VirtualDocumentRoot /var/www/html/%1

should redirect requests for subdomain.mysite.com to /var/www/html/subdomain
No Strings Attached: A JavaScript graphics demo.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 192 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2010 Unmelted, LLC. Driven by phpBB © 2010 phpBB Group.