Questions regarding DNS configuration for multiple subdomain under 1 IP address?

February 23, 2014 2.4k views
Thank you for help in advance. I recently registered a domain name and wish to create multiple subdomains, which are to be hosted on a droplet under a single IP address using virtual hosts. The following are my desired configuration. Domain name: example.com, subdomain 1: test, subdomain 2: forum. Droplet IP: Alias "test.example.com" point to subdomain 1 and "forum.example.com" point to subdomain 2. Current DNS Configuration: (each field sepated by "|") A Record: @ | A Record: forum | A Record: test | CNAME: * | @ CNAME: *.forum | forum.example.com. CNAME: *test | test.example.com. NS: ns1.digitalocean.com NS: ns2.digitalocean.com NS: ns3.digitalocean.com I created additional CNAME record as follow to point anything other than the alias specified to www.example.com CNAME: * | www.example.com. The CNAME entry worked and it pointed example.com and fdasas.example.com to www.example.com, however, I got error messages on the control panel page after adding the above entry. My other questions are regarding to virtual host configuration: Do I need to create a separate virtual host configuration file for each virtual host under /etc/apache2/sites-available or I simply add a block on the existing default configuration? Do each virtual host need to listen to a different port for them to work? My config are as follow: /etc/apache2/sites-available/default ServerAdmin webmaster@localhost DocumentRoot /var/www Options FollowSymLinks AllowOverride None /etc/apache2/sites-available/test.example.com #NameVirtualHost *:80 #uncomment lead to apache2 startup error ServerAdmin webmaster@localhost ServerName test.example.com ServerAlias test.example.com DocumentRoot /var/www/forum.example.com/public_html Options FollowSymLinks AllowOverride None Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all I followed tutorial on these setup, but I am unsure if I need to have 1 config file for each virtual host or 1 config file for multiple virtual host. I apologise if the question seem obscure, please let me know so I can explain better. Thank you for your help!
1 Answer
First off there are quite a few ways you could set this up but I will explain how I would do it. From what I have gathered you basically want to have 3 separate sites www.example.com, test.example.com, and forum.example.com and I will assume that that each will reside in seperate folders in the /var/www/ dir. The best way (in my opinion of course) would be to create 3 separate a records, "@" for www.example.com, "test" for test.example.com, and "forum" for forrum.example.com and point them all to the IP address of your server/droplet. The create one cname "www" that points to example.com. Then create 3 separate vhost files in /etc/apache2/sites-available/ (a simple way to do this is to run the command "mv default test.example.com" file extinsions are not important to LINUX so I prefer to name the vhost's as the domain to keep things simple, then update the vhost file with this line ServerName test.example.com. The ServerAlias directive is only important when using cname's so you only need it on the vhost file for the main site example.com in which it would be ServerAlias www.example.com. Then update the corresponding folders in the /var/www/ to mach the names you put in the vhost file and run the command for each of the new sites "a2ensite test.example.com" replacing the last part for each site of course. Then finally if you have done everything else correctly, you need to restart apache with the following command "service apache2 restart" and then you show be able to go to each of the separate domains in your browser. As a side note when you initially setup the A records and cname it will probably take some time for the DNS to propagate so you might want to take a break for an hour or 2 after that step...

Have another answer? Share your knowledge.