Quantcast

Setting Up Subdomains in Linode and Apache

by David on February 11, 2010

I’ve been using Linode for a few months to host about a dozen websites and I’ve been very happy with the experience. I have found the documentation at Linode to be clear and concise, especially when it comes to setting up multiple sites using a standard, Ubuntu-based LAMP (Linux, Apache, MySQL, PHP) configuration.

I decided to take one of the sites to the next level and add a subdomain. (An example of a primary domain is davidpodley.com while a subdomain would be the ‘blog’ in blog.davidpodley.com.) Unfortunately, Linode’s library and other documents was less than clear about all of the steps.

My goal here is to explain, step by step, how to add a subdomain to your Linode service. I suppose this could work for any Ubuntu-based LAMP stack, but this may have some Linode-specific information.

**NOTE: My DNS services are managed at Linode, so I’ll assume you have done the same thing yourself.

First, we will tell the server what to do with traffic coming to the address.

  • Log into the Members section of Linode (linode.com/members) and find the Linode you want to add a subdomain to. Make a note of the IP Address (you will use this later). If you’re like most users, you will only have one Linode listed.
  • Next, open the DNS Manager at Linode by clicking ‘DNS Manager’ at the top. This should take you to a page that has at least one domain listed.
  • Select the domain you want to add a subdomain to. This will take you to a screen that lists several options. Mine lists SOA Record, then NS Records, MX Recods, then A/AAAA Records. Under the listed A/AAAA Records, click ‘Add a new A/AAAA Record’. **(UPDATE: I’ve read that one user had a problem getting this to work with an A Record, but was able to get it to work using the CNAME Record. I don’t believe it should matter, but if you’re having problems, you may want to use the CNAME.)**
  • You will be taken to a page with a form that says, ‘Add/Edit an A Record’. In the ‘Hostname:’ field, enter the name of your subdomain ‘blog’, ‘shop’, or ‘xyz’ could be examples. In the ‘IP Address:’ field, enter the IP address you made note of in the first step of this document. Click ‘Save’.
  • You will be taken back to the previous page. I click ‘Render’ at the bottom. I don’t know that it does anything, but it doesn’t seem to hurt. (***UPDATE*** Since this was written, it seems as though Linode has changed their DNS system. I don’t see the ‘Render’ button any more. Also, the look of the DNS system has changed. The end result should be the same, but you may see something slightly different than what I mention here.)

That wasn’t that difficult, was it? Well, now we get to go into dealing with actually setting up the subdomain on the Apache side.

Setting Up Apache
Setting up a subdomain is similar to setting up a normal domain following the guide found at pod.li/0f, but there are a few changes that need to be made.
You will need to open an SSH connection to your Linode. I assume you know how to do this because if you didn’t, you probably wouldn’t be using Linode for your hosting solution.
Once you’ve connected via SSH, navigate to Apache’s sites-available directory. In the example given by Linode (and the default location upon a standard Apache2 installation), you can type (the # is just a prompt, do not type the # sign):

# cd /etc/apache2/sites-available
If you run the command

# ls

You will see the sites you’ve defined so far. You will need to create another site titled xyz.YOURDOMAINNAME.com, where xyz is the subdomain of your choosing. You can do this by either copying an existing, working site definition or by creating a new file. I usually copy an old site using:

# cp example.com xyz.YOURDOMAINNAME.com

Now enter the command

# nano xyz.YOURDOMAINNAME.com

You should see a box with text similar to:

<VirtualHost *:80>
ServerAdmin admin@YOURDOMAINNAME.com
ServerName YOURDOMAINNAME.com
ServerAlias www.YOURDOMAINNAME.com
DocumentRoot /srv/www/YOURDOMAINNAME.com/public_html/
ErrorLog /srv/www/YOURDOMAINNAME.com/logs/error.log
CustomLog /srv/www/YOURDOMAINNAME.com/logs/access.log combined
</VirtualHost>
Change the following lines as such:
ServerName xyz.YOURDOMAINNAME.com
ServerAlias www.xyz.YOURDOMAINNAME.com
DocumentRoot /srv/www/YOURDOMAINNAME.com/public_html/xyz/
It should now look like this:
<VirtualHost *:80>
ServerAdmin admin@hiredhired.com
ServerName xyz.YOURDOMAINNAME.com
ServerAlias www.YOURDOMAINNAME.com (EDITED FOR AN ERROR – USE THE LINE BELOW)
ServerAlias www.xyz.YOURDOMAINNAME.com
DocumentRoot /srv/www/YOURDOMAINNAME.com/public_html/xyz/
ErrorLog /srv/www/YOURDOMAINNAME.com/logs/error.log
CustomLog /srv/www/YOURDOMAINNAME.com/logs/access.log combined
</VirtualHost>
What we have done is created a new server that notices that a user is coming to xyz.YOURDOMAINNAME.com and it now points the user to a different directory (note xyz at the end of the line in the DocumentRoot section).
Exit out of that screen by typing control-x. You will be prompted to save the file, so click Y for Yes, then hit enter when it prompts you with the file name (it should be your subdomain.domainname.com).
Now we create the directory the subdomain will read from:
# mkdir -p /srv/www/yourdomainname.com/public_html/xyz
Now we need to tell Apache to enable the site. Type:
# a2ensite xyz.yourdomainname.com
Then refresh Apache to know what’s going on:
# /etc/init.d/apache2 reload
You’re Done!

It may take anywhere from an hour or so to a day for the subdomain to propagate through the DNS system. I errantly assumed it would be immediate because DNS was managed by Linode, but it took about an hour for it to respond to my browser.

Help Keep This Site Going
If you found value to this article, please consider donating a dollar or two to help cover the cost of managing this site.


Alternately, if you’re interested in becoming a Linode user, please use this affiliate link before creating a Linode account.

{ 29 comments… read them below or add one }

Zander March 18, 2010 at 3:33 pm

Thanks David, this was very handy.

David March 18, 2010 at 3:38 pm

Zander,
I’m glad it was helpful. This is one of those things that is so frustrating to figure out, but once you know how to do it, you shake your head and think, “what was all the fuss about?”

Zander March 18, 2010 at 4:41 pm

Yes I know exactly what you mean, luckily I had this and a very helpful brother-in-law to call upon.

Damian Nicholson April 18, 2010 at 6:33 am

Both this and your other post on setting up hosted email solutions with Linode have been a huge help to me. Thanks David.

Aziz Light August 8, 2010 at 10:46 pm

Thanks a lot for this guide! It is exactly what I was looking for. This should be on the Linode Library by the way, I am sure a lot of Linode users will benefit from it.

Anyway, good job David.

Theresa August 21, 2010 at 2:33 pm

So helpful. Thanks for posting this!

z December 7, 2010 at 2:17 am

Unbelievably helpful! I had no idea how to point a folder to a sub-domain. Previously I could only find out how to create the sub-domain or do some weird redirection from a folder to sub-domain with htaccess…did that even make sense? Regardless….your walkthrough was perfect..Thanks Mr Podley!

Umut December 26, 2010 at 6:52 am

Very helpful David. Thanks a lot!

soup_sense December 27, 2010 at 10:58 am

Very helpful. Thank you so much for writing this. :)

mark January 30, 2011 at 2:13 pm

Exactly what I was looking for. I have just done it so have to wait for it to propagate to see if it works. I do have a quick question which I was not sure about.
you put this
ServerAlias http://www.xyz.YOURDOMAINNAME.com
but then when you said “it should look like this” you put

ServerAlias http://www.YOURDOMAINNAME.com

In my file i just put the subdomain.domain.com as I was unsure which was correct. It was a tad confusing at that moment but apart form that it was great.
Thanks.

David February 1, 2011 at 7:58 pm

Thanks, Mark.
I’ve edited the post to give a bit more clarity and to remove an error.

rikkit February 11, 2011 at 1:32 pm

Thanks, this was helpful. Having just recently purchased a VPS, I had no idea how subdomains were handled – your post helped me realise I had to edit the zone files with my registrar. Obvious, but different to how I used to have to do it!

ron March 12, 2011 at 6:40 pm

Hello David,

As far as I know I the setup I have now is exactly as you have described. The only difference is that I used other sites (which were definitly not as clear as yours).

When I go to: “www.red.rock.info” , The page can not be found and i go directly to a search page with some search results

However when i go to: “http://red.rock.info” it works perfectly.
To make it even weirder, the main domain: “rock.info” works perfectly in: “www.rock.info” and “http://rock.info” , and is defined in exactly the same way as: “red.rock.info” is defined.

SUBdomain: red.rock.info (in a seperate “red.rock.info” file in: “/etc/apache2/sites-available/” )

ServerAdmin r.rock@gmail.com
ServerName red.rock.info
ServerAlias http://www.red.rock.info
DocumentRoot /srv/www/red.rock.info/public_html/
ErrorLog /srv/www/red.rock.info/logs/error.log
CustomLog /srv/www/red.rock.info/logs/access.log combined

The main domain: “rock.info” is defined exactly as above in “/etc/apache2/sites-available/” only the name is “rock.info” and of cours in stead of “red.rock.info” is “rock.info”

The domain name is registerd @ godaddy, in the godaddy zone-file “A-record” I routed the “rock” subdomain to “111.222.333.444″

Again “http://red…” works perfect BUT “www.red…” does NOT.

Do you have any idea? (thanks for the turorial!)

ron March 12, 2011 at 6:43 pm

Sorry, in my post above somehow the brackets where omitted:

…content as described above

ron March 12, 2011 at 6:45 pm

( VirtualHost 111.222.333.444 )

…content as described above

(/VirtualHost)

replace paranteses whith brackets

Sorry for “spamming”, there is no edit button…again my apolagies

David March 15, 2011 at 4:53 pm

I would suggest adding a CNAME entry with your DNS service. Have you tried pointing http://www.red.rock.info to red.rock.info in the CNAME record?

Mike April 2, 2011 at 11:38 am

Thanks for the help! This was very easy to follow, and it worked perfectly.

Christopher Castleman April 14, 2011 at 6:04 pm

This post helped me immensely. I am new to server configuration, and was having an issue where my subdomains where conflicting with my regular site. Both sites would work, but people typing “domain.com” would be routed to “sub.domain.com” unless they used the “www”. It ended up being that I was setting the virtual host configuration as:

ServerName domain.com
ServerAlias sub.domain.com

instead of what it should be:

ServerName sub.domain.com
ServerAlias http://www.sub.domain.com

Thanks David!

crossan April 15, 2011 at 6:34 am

Thank you! Great post – nice and clear. Only just started using linode the other day. Really pleased with it so far!

Kay May 7, 2011 at 5:53 pm

Thank you so much for this guide! Spent half an hour going through the library and forums and your post got me through the whole thing in 5 minutes! :D

Jon May 12, 2011 at 9:14 am

Thank you! I agree with Kay. The Linode library was thin on subdomain details. It’s a simple process, but not necessarily intuitive.

Xavi Colomer July 26, 2011 at 8:08 am

Exactly what I was looking for!

I’ll try it today!

thanks

Hans September 13, 2011 at 12:41 pm

I use you code, no error but subdomain is not working , ulr displaying main domain index file.

Ali Raza December 5, 2011 at 1:58 am

Hi

How long does it take for the sub domains to propagate? I have been waiting for a couple of days now. I can access my main domain:

http://www.skadooshlabs.com

but my subdomain:
http://test.skadooshlabs.com

is still not accessible. I have disabled the default site. My domain is managed by linode’s domain manager. I dont know if I am doing something wrong. Can you please help. I am very new to this.

Ali Raza December 5, 2011 at 2:11 am

I think I have figured out my mistake, in A/AAA records i had added the hostname as test.skadooshlabs.com when i read you post again i figured out that i have to enter just “test” i have made the changes now, waiting to see if it opens up in the browser.

David January 2, 2012 at 12:15 pm

In the /etc/apache2/sites-available file for the subdomain, did you change the DocumentRoot item to reflect a different directory? If did, have you enabled the site and refreshed Apache? (a2ensite subdomain.example.com followed by /etc/init.d/apache2 reload)

Evan R. Murphy February 22, 2012 at 8:53 pm

Handy post, thanks!

Techiestuffs April 19, 2012 at 10:24 am

Thanks a lot…your guide saved my day….Hoping DNS to propagate…

g33kgrrl January 15, 2013 at 6:44 pm

Thank you for this guide! It was immensely helpful for getting our wiki subdomain up and running!

Leave a Comment

{ 2 trackbacks }