pradyotghate
pradyotghate

Reputation: 487

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?

Upvotes: 12

Views: 29679

Answers (3)

pradyotghate
pradyotghate

Reputation: 487

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

Upvotes: 9

NiRUS
NiRUS

Reputation: 4269

This can be done installing Apache HTTP Server on AWS-EC2 instance and configuring VirtualHost for each DNS or Sub-DNS as suggested by an Amazonian

For brevity read this thread discussion, this official example and techrepublic post.


Hope this helped!

Upvotes: 0

filipebarretto
filipebarretto

Reputation: 1952

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

Upvotes: 1

Related Questions