Using Route 53 with EC2 Public DNS and Virtual Hosts

amazon-route53amazon-web-servicesdomain-name-systemvirtualhost

I would like subdomains of my EC2 Instance Public URL to point to different document trees in on my machine so foo.ec2-012-345-678-901.eu-west-1.compute.amazonaws.com has a separate DocumentRoot to bar.ec2-012-345-678-901.eu-west-1.compute.amazonaws.com

My AWS EC2 instance has an elastic IP and a standard Amazon Public URL:

http://ec2-012-345-678-901.eu-west-1.compute.amazonaws.com

I can visit that URL and I see my Hello World test page.

I've set up two VirtualHosts.

<VirtualHost *:80>
  DocumentRoot /var/www/foo/public
  ServerName foo.ec2-012-345-678-901.eu-west-1.compute.amazonaws.com
  ServerAlias ec2-012-345-678-901.eu-west-1.compute.amazonaws.com
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot /var/www/bar/public
  ServerName bar.ec2-012-345-678-901.eu-west-1.compute.amazonaws.com
</VirtualHost>

In AWS Route 53 I've set up a hosted zone with the domain name
ec2-012-345-678-901.eu-west-1.compute.amazonaws.com

In the record sets I have added two CNAME records:

1)

  • Name: foo.ec2-012-345-678-901.eu-west-1.compute.amazonaws.com
  • TTL: 60
  • Value: ec2-012-345-678-901.eu-west-1.compute.amazonaws.com
  • Routing Policy: Simple
  • Alias: No

2)

  • Name: bar.ec2-012-345-678-901.eu-west-1.compute.amazonaws.com
  • TTL: 60
  • Value: ec2-012-345-678-901.eu-west-1.compute.amazonaws.com
  • Routing Policy: Simple
  • Alias: No

My browser can't find either of the URLs defined in the virtual hosts. I'm using using OpenDNS, but I can SSH into a few other machines and try curl and wget and always receive the error "Name or service not known" or "couldn't resolve host".

I have been playing around with these settings a bit today, but as well as I can remember this is exactly how I set them up 24 hours ago.

What do I need to do to get my virtual hosts to resolve to the directories I've defined?

Note: I'm on RHEL, with SELinux enabled. I don't think it's relevant, but it's been the cause of a few issues already.

Cheers,

Best Answer

You need to set Apache's ServerName for the virtual host to the actual hostname that you want to serve, such as foo.example.com, and NOT the EC2 internal hostname (though you could use a ServerAlias for that if you wanted it to be accessible).

Unfortunately you can't use your EC2 hostname as a domain within Route 53; you have to use your own domain name.

Related Topic