36

How can I create subdomains on Amazon EC2?

Is adding virtual host in httpd.conf is enough.. or any other changes also needs to be done?

Thanks

Padmanabha Vn
  • 624
  • 1
  • 14
  • 33

1 Answers1

61

Depends on your server software. But as you mention httpd.conf, chances are good that you run Apache on a Linux distribution. If that's the case then yes, adding a virtual host is enough. Here is one way of doing it:

  1. Purchase a domain. If you have one, skip this, we'll take example.com for this example.
  2. Find the external IP or DNS for your EC2 instance. You probably want to associate an Elastic IP to your instance, otherwise the IP of your instance will change on reboots.
  3. Create a DNS record for your domain, for instance a CNAME record to point to your Elastic IP/DNS name:

    subdomain.example.com => ec2-xx-xxx-xxx-xxx.eu-west-1.compute.amazonaws.com

  4. Make sure your httpd.conf contains a line to allow virtual hosts:

    NameVirtualHost *:80

  5. Create a virtual host directive:

httpd.conf:

<VirtualHost *:80>
  ServerName subdomain.example.com
  ServerAdmin webmaster@subdomain.example.com

  DocumentRoot /var/www/example.com/subdomain

  <Directory /var/www/example.com/subdomain>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>

  ErrorLog /var/log/apache2/subdomain.example.com.error.log
  LogLevel warn
  CustomLog /var/log/apache2/subdomain.example.com.access.log combined
</VirtualHost>

6. Restart Apache

/etc/init.d/apache2 restart
Tathagata
  • 1,955
  • 4
  • 23
  • 39
cvaldemar
  • 6,983
  • 2
  • 24
  • 18
  • 1
    thanks @cvaldemar. i am trying to do the same but i am using ispconfig as control panel. when i declare vhost it creates vhost configuration in sites-available folder of apache2 but it creates entry something like this - `` and subdomains starts reading root site folder instead of desired subdomain folder. when i edit this line and rewrite like this- `` it starts to work perfectly fine. Can you please help me to understand what causing this behaviour ?? Pardon me i am a noob in server administration. – streak Apr 14 '14 at 09:21
  • 1
    Just curious - if I do not have my own domain name yet, can I configure Amazon EC2 to have more than one ec2-xx-xxx-xxx-xxx.region-x.compute.amazonaws.com address pointing to my EC2 instance? Thus I could develop and test multiple web apps with VirtualHosts on single EC2 instance. – JustAMartin Feb 14 '16 at 18:26
  • 1
    **version note:** in Apache version 2.4+ , `NameVirtualHost` has no effect. Also, using `Option Indexes` is dangerous; use `Option -Indexes` to prevent directory listing instead. – Raptor Jul 11 '16 at 04:01
  • How to avoid creating elastic IP? It is not free in AWS. Are there options to have subdomains with a default public DNS and IP only? – Green Jan 10 '17 at 21:14
  • It's worth noting that order of VirtualHosts is also important. You want to place your subdomain VirtualHost settings above the default one. In my case I also had to add the www. VirtualHost as well, because www. was getting caught by the subdomain by default. – GTCrais May 16 '17 at 12:48
  • Assume you have added 100 sub-domains based on above method, when you Restart Apache , it will not affect the sub-domains current request - response process ? i.e assume some important transaction going on in one of the Sub-domain and while adding new sub-domain we restart the Apache. Is there any such risk while restarting Apache on fly ? – Pragnesh Karia May 17 '18 at 12:32
  • I want to ask for more suggestions on "# /etc/init.d/apache2 graceful" will this command will be helpful instead of Apache restart ?? and this need dedicated server , as we need to have root user access to run "graceful" command. Please advice more on the same. – Pragnesh Karia May 17 '18 at 12:43
  • How do I accomplish step 3? What if I already have a site and document root being pointed to by the domain? Do I need to add a DNS record or CNAME for the sub domain again? – Jonathan Ma Mar 15 '21 at 10:57
  • Can you add subdomain without restarting apache server? – reignsly Jul 13 '22 at 02:53