phpamazon-web-servicesapiamazon-s3

Using AWS services to create subdomains on the fly to host a site on S3



INFO: I am working on an app built in php which facilitates users to create HTML templates and publish them on web.As the templates would be static I thought of using amazon S3 for storing them as it can host static websites and has good infrastructure overall for the application.br/>

QUERY: I am facing an issue while publishing it to the web.I want the template to be published as a subdomain on my domain,for eg: I own www.ABC.com,I ask the user to name the template,if he names it as mysite,I publish his template as mysite.ABC.com(similar for all users).Now,I can store the template in the S3 bucket using putObjectFile in the aws s3 api,but I am not sure how can I create a subdomain(on the fly) and publish it on that domain.(I want to automate the process for the user). Also,can I make the bucket as hosting static website using the API?
Earlier,I worked with cpanel and cpanel API's allow us to create domains and do a FTP to the domain with the content,I am not clear how can I achieve it here.

RESEARCH: The success till now I have achieved is,I have hosted a site using the S3 console.Using the AWS services I have moved the files to a bucket with the name same as the subdomain of the user.I want now to have the bucket endpoint changed to the subdomain.

REFERENCE: This website does the same,they create a directory like structure and publish the website on web.I don't know if they host it on Amazon,but I want to achieve something similar.Hope I am clear and get some guidance.

Thank you for the attention


Solution

  • I researched a similar scenario some time ago but I was unable to use S3 for this because of a S3 limitation.

    To host a static website on S3 on a custom domain or subdomain, you need to create a bucket with a name that matches that domain. And because each S3 account is limited to 100 buckets, you will only be able to host those many domains or subdomains on a single account.

    Based on the use case you described, I suspect this S3 limitation will also force you to find another solution.

    In my case, the solution was to set up an EC2 instance that proxies requests to S3 after rewriting them. For example if someone requests:

    http://mysite.abc.com/file.html

    that goes through our EC2 server where the request is rewritten and forwarded to S3 as something like:

    http://ourbucket.s3.amazonaws.com/mysite.abc.com/file.html

    UPDATE:

    There are several proxy servers that you could use for this but I would recommend nginx as it worked great in our case. To get started, check out the following nginx docs:

    http://wiki.nginx.org/HttpRewriteModule
    http://wiki.nginx.org/HttpProxyModule