How do i setup subdomains for my droplet?

January 21, 2013 58.7k views
Hi, I've set up a LAMP server on an Ubuntu VPS, installed webmin, created DNS records in DigitalOcean panel as instructed by the tutorials. Everything works just fine on the main domain name, but at this point I want to make a test subdomain: text.example.com to lead to /var/www/test/ I've been trying for a while now and I don't know how to do it. Got lost in CNAME records, A records and settting up virtual hosts in webmin. Does anybody have any pointers or any good tutorial on it? Thanks.
19 Answers
Not sure if this answer has been answered yet, but here's my solution. I'm using the latest Ubuntu on my droplet with LAMP, etc.

1) Under your DNS settings in the Digital Ocean CP, add an "A" record for the sub-domain. The values should be as follows:
Hostname --> sub.mydomain.com.
IP --> Use the droplet's IP
IMPORTANT: Make sure to add the period at the end of the hostname like I've done!

2) Open your /etc/apache2/sites-available/default file. I'm assuming you already have the VirtualHost setup correctly for the primary domain, etc. For the subdomain, simply add:

ServerName sub.mydomain.com
DocumentRoot /var/www/the-folder-of-your-choice
CustomLog /var/www/logs/sub-mydomain.log combined
ErrorLog /var/www/logs/sub-mydomain.log

Your VirtualHost info may differ a little based on the setup of your choice. Restart Apache and check to see if you sub-domain works. I had to wait a little before the subdomain worked in my browser.

Hope this helps!
We need to add a helper section to the DNS manager which will give a brief write-up on how to use DNS.

A lot of new customers that aren't familiar with some of the DNS settings don't know these things and it would help them out in getting the configs right the first time.
Configure all VirtualHosts explicitly with a ServerName directive inside of the VirtualHost directive that way its more explicit and will be easier for you to troubleshoot.
You would need to create another VirtualHost entry, you can copy the main domain configs and update the following:
Servername sub.domain.com
DocumentRoot /var/www/sub.domain.com

Make sure that DNS is properly setup and restart Apache and you should be good to go.
Thanks a lot for the answer. That's what I though. And tried. But i must have configured something wrong.

Let me tell you my configuration:

DigitalOcean DNS Records:
@ IN A 185.14.185.157
www CNAME @
test IN A 185.14.185.157

2 Virtual Hosts:
1) [default - has no name, responds to all addresses]
Handles the name-based server on address *.
Address Any
Port 80 Server Name Automatic
Document Root /var/www

--
DocumentRoot /var/www

Options FollowSymLinks
AllowOverride None


Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all


2) [test.domain.com, responds to address test.domain.com]
Handles the name-based server test.domain.com on address test.domain.com.
Address test.domain.com
Port 80 Server Name test.domain.com
Document Root /var/www/test

--
DocumentRoot "/var/www/test"
ServerName test.domain.com

allow from all
Options +Indexes


---------------
When I don't have the test.domain.com virtual host, domain.com works fine. When I add that virtual host, domain.com also gets redirected to /var/www/test

I don't know where the problem is.
I am having the same issue. My DNS records are similar, I have two virtual hosts defined, one for my main domain and one for the sub domain, the main domain works fine but the sub domain does not. I get a server not found error in the browser.
Sounds like you most likely added a CNAME record such as "www" and that was pointed to "domain.com" however DNS will autoappend your name to the DNS record unless its terminated with a "."

So a CNAME record:
"www" -> "domain.com" resolves to: domain.com.domain.com

Instead a CNAME record should be created as:
"www" -> "domain.com." resolves to: domain.com

The only difference is the "." at the end.

You can also create a CNAME record that references the domain itself with the following:
"www" -> "@"

If that doesn't work please open a support ticket and we can troubleshoot the issue with you.

Thanks
This was really great information and will come in quite handy.

I did not know about adding the . to the end, saved me a lot of time
+1 for DNS Tutorial. I could really use that.
Hi all,
I really struggled with this at first but found a great tutorial on how to setup virtual hosts with Webmin.
http://gregrickaby.com/2011/06/how-to-create-virtual-servers-in-apache-using-webmin.html
Turn out I hadn't completed a couple of steps. Worked for me immediately :D

Jason
Hi all,
I want to setup a sub domain under my Ubuntu and Ghost blog I am using DigitalOcean.
I am very new to all this so can someone give me a step by step on the things I should do.

Thanks Kerry Rogers

@Info: Hello, please create a new question (https://www.digitalocean.com/community/questions/new) mentioning the droplet image you used. Thanks!
do i need to create another virtual host for my sub domains
Question:
When setting up a subdomain on LAMP, does each subdomain have its own conf file
eg:
/etc/apache2/sites-available/site.com
/etc/apache2/sites-available/sub.site.com

or do all the rules go in the same conf file for site.com?
eg.

ServerName site.com
DocumentRoot ...


ServerName sub.site.com
DocumentRoot ...
@alvarix: It doesn't matter. Having a separate config file for each virtualhost is clearer, though.
I have sub.domain.com and domain.com on the same same machine, but i want to have sub.domain.com on other directory. How can I make it possible?

shivpatelspp suggests we add the new subdomain settings to the file:

"/etc/apache2/sites-available/default file"

if I want to add, let's say, the subdomain "stage.mydomain.com" , can I add it to the
"/etc/apache2/sites-available/mydomain.com.conf" file ? I actually tried that and apache could not restart and I got the error "Syntax error on line 219 of /etc/apache2/apache2.conf" . The error went away when I removed the subdomain settings from "mydomain.com.conf".

So apparently I cannot have my subdomain in the same conf file as my domain?

Can/should I create a new .conf file for my subdomain or should it be in the 000-default.conf file?

Thank you very much

Have another answer? Share your knowledge.