开发者

automated subdomain creation amazon ec2

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 scrip开发者_StackOverflow中文版t 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

  • create a document root folder
  • create a virtual host file and
  • restart apache

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.


You can take a look at the new feature of Amazon AWS:

http://aws.amazon.com/route53/

http://aws.typepad.com/aws/2010/12/amazon-route-53-the-aws-domain-name-service.html


You can actually utilize RewriteMap for it.. It would allow you to map domain names as well (in case you want to branch out to that as well).

Something like this should work -

RewriteMap lowercase int:tolower
RewriteMap domainname txt:/var/conf/domain.map
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${domainname:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/$1 [L,NC,QSA]

Now you can use PHP or anything to create this domain.map file which would follow the format -

<domain name> <targeted path on the server>

Domain/Subdomain and the targeted path on the server.

So for eg.

example.com /myvirtualhostingserver/examplesite
subdomain.demo.com /myvirtualhostingserver/subdomain

Now you just need to point example.com to your server!

This method allows you to keep the subdmain folder name independent of the name of the subdomain (depends totally on your use case).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜