Search code examples
apacheamazon-web-servicesamazon-ec2httpd.confamazon-route53

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


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?


Solution

  • 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 :)