How to host multiple domains and subdomains on single AWS EC2 instance

24,981

Solution 1

Ok, so after about 2 hours of reading up various sites and tinkering, I am all set. Here is how to do this.

Basically, you should not have more than 1 hosted zone (HZ) per domain name, otherwise things are really going to be bad. If you have more than 1 HZ for a domain name, please delete the one that was created for the subdomain.

Each HZ will have 4 records -

Following two records are created by default. Do not edit/delete them.
NS - This is the name server record. If AWS Route53 is not your registrar, use ns-servernumber.awsdns-number.com. and other three (4 total) records to change name servers for your registrar.
SOA - Let this record be. DO NOT TOUCH THIS.

Create following two Record Set (blue button).
A - Leave Name blank. Select A-IPv4 address for Type. In Value enter the IP address for your Elastic Load Balancer or EC2 instance.
CNAME - Add * (asterisks/wildcard) in the name field. Select CNAME from the drop down for Type In Value enter the domain name.

Now create the http.conf file and structure virtual hosts like I have in the question.

Things should work now :)

Solution 2

You can follow the tutorial on this link: http://brianshim.com/webtricks/host-multiple-sites-amazon-ec2/

A common error, according to the link, is:

Did it work? If not, here is one possible cause. There might be another configuration file interfering with this one. Check for another .conf file in /etc/httpd/conf.d. Often your primary httpd.conf will include another .conf file in this directory. Make sure it doesn’t have some Virtual Host settings which are interfering with yours.

After you set the configurations, you should run:

sudo service httpd restart
Share:
24,981
pradyotghate
Author by

pradyotghate

Updated on August 05, 2022

Comments

  • pradyotghate
    pradyotghate over 1 year

    I am trying to set up 2 domains (domain1.com and domain2.com) with a few subdomains (app.domain1.com) in AWS and run them on single instance (Amazon Linux, PHP, MySQL).

    I have set up 3 hosted zones in AWS Route53 with following configurations.

    Hosted zone 1:
    domain1.com
    Type A
    52.108.XX.YY

    Hosted Zone 2
    domain2.com
    Type A
    52.108.XX.YY

    Hosted Zone 3
    app.domain1.com
    Type A
    52.108.XX.YY

    Additionally, I have added following code to the http.conf file in VirtualHost tag.

    <VirtualHost *:80>   
         ServerName domain1.com   
         DocumentRoot "/var/www/html/domain1"   
         ErrorLog "logs/domain1-error_log"  
         CustomLog "logs/domain1-access_log" common  
         </VirtualHost>
         
         <VirtualHost *:80>   
         ServerName domain2.com   
         DocumentRoot "/var/www/html/domain2"  
         ErrorLog "logs/domain2-error_log"  
         CustomLog "logs/domain2-access_log" common  
         </VirtualHost>
         
         <VirtualHost *:80>  
         ServerName app.domain1.com   
         DocumentRoot "/var/www/html/app"  
         ErrorLog "logs/app.domain1-error_log"  
         CustomLog "logs/app.domain1-access_log" common  
         </VirtualHost>
    

    However, only domain1.com and domain2.com are getting resolved. When I visit app.domain1.com, it gives me a "can't find server" error. Please help how to setup the subdomain - is there problem in Hosted Zone setup or httpd.conf?

  • pradyotghate
    pradyotghate almost 8 years
    Thanks for sharing sharing the article. Read it. But it seems like a pre-Route 53 article. It prescribes directly pointing domain to IP address, not a recommended practice by AWS since it is not scalable with Elastic Load Balancers in future. Need guidance on making this work with Route 53 managing NS.
  • pradyotghate
    pradyotghate almost 8 years
    No other virtual host setting files. Did restart Apache.
  • Mark B
    Mark B almost 8 years
    @PradyotGhate In your question you say you are setting up A records in Route53 to point directly to IP addresses. So there is no difference here in using Route53 versus any other DNS service. Your comment about Route53 being "scalable with Elastic Load Balancers in future" doesn't really seem to be relevant to the discussion. Yes you can use Route53 aliases to point root domains to ELBs in the future, but right now you are just using A records like you would on any other DNS service like Godaddy which is referenced in the linked article.
  • pradyotghate
    pradyotghate almost 8 years
    Correct, it is not relevant today. But I want to keep that option (add ELB and point the Type A to EBL instead of EC2 instance) open. That's why using Route53 and not directly resolving domain name to IP address. Please tell me if I am not thinking about this correctly.