自动子域创建亚马逊ec2

发布于 2024-10-05 21:24:40 字数 111 浏览 2 评论 0原文

我正在使用 amazon ec2 ....我想要一个在用户注册时创建子域的脚本,或者用户可以选择根据需要创建子域..?

任何人都可以为此建议一个脚本..最好是 php..

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对岸观火 2024-10-12 21:24:40

如果您使用 apache Web 服务器和 fedora linux,则必须执行以下操作

在您的 dns 中添加通配符条目。假设您的域名是 demo.com,dns 条目如

*.demo.com IN A 192.168.1.100

将 ip 替换为您的 ip。

然后,anything.demo.com 将到达您的服务器。

我们必须配置 apache 来处理子域。

对于每个子域我们要

  • 创建一个文档根文件夹
  • 创建一个虚拟主机文件并
  • 重新启动apache

下面的php符合上面的想法。

    <?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();
    ?>

您必须以 root 用户身份运行此脚本。在虚拟主机配置文件中您可以添加更多选项。您还可以查看类似问题问题的答案。

希望有帮助。

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.

你与昨日 2024-10-12 21:24:40

您实际上可以利用 RewriteMap 来实现它。它还允许您映射域名(如果您也想扩展到该领域)。

像这样的东西应该可以工作 -

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

现在您可以使用 PHP 或任何东西来创建这个domain.map 文件,该文件将遵循格式 -

<domain name> <targeted path on the server>

域/子域和服务器上的目标路径。

所以对于例如。

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

现在您只需将 example.com 指向您的服务器即可!

此方法允许您保持子域文件夹名称独立于子域的名称(完全取决于您的用例)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文