I am using amazon ec2 .... I want a script which creates subdomain when an user registers or user has the option to create subdomin as needed..?
can anyone suggest a script for this.. preferably php..
Thanks
If you are using apache web server and and a fedora linux, you have to do the following things
In your dns add a wild card entry. Suppose your domain name is demo.com the dns entry like
*.demo.com IN A 192.168.1.100
Replace the ip with your ip.
Then anything.demo.com will come to your server.
We have to configure apache to handle the subdomain.
For each subdomain we have to
The following php is in line with the above idea.
<?php
define('DOMAIN','demo.com');
define('DOCROOT','/home/username/www/');
define('CONF_FOLDER','/etc/httpd/conf.d/');
/*
* Function to create conf file in conf.d folder
*/
function createNewVhostFile($subdomain) {
$filename = CONF_FOLDER.$subdomain.'.conf';
$fh = fopen($filename, 'w') or die("can't open file");
$servername = $subdomain.".".DOMAIN;
$docroot = DOCROOT.$subdomain;
$virtualhost = <<<HEREDOC
<VirtualHost $servername >
DocumentRoot $docroot
ServerName $servername
ServerAlias $servername
</VirtualHost>
HEREDOC;
fwrite($fh, $virtualhost);
fclose($fh);
}
/*
* Function to restart apache
*/
function restartApache() {
$configtest = `apachectl configtest 2>&1`;
echo $configtest;
if(strtolower(trim($configtest)) == 'syntax ok'){
$restart = `/etc/init.d/httpd restart 2>&1`;
echo $restart;
}
}
/*
* Create document root folder.
*/
function createDocRoot($subdomain){
$docroot = DOCROOT.$subdomain;
if(is_dir($subdomain)){
echo "Document root allready exists.";
}else{
mkdir($docroot,644);
}
}
$subdomain = "sub";
createDocRoot($subdomain);
createNewVhostFile($subdomain);
restartApache();
?>
You have to run this script as root user. In the virtual host configuration file you can add more options. You can also check the answer to a similar question question.
Hope that helps.