Whether for different domain names or different subdomains of the same domain, the procedure is the same.

Question

I am often asked by people how to use their Slice to serve multiple domains.

The question often surprises me as they may have setup their Slice, installed a web server (Apache, Nginx, etc) and even created a virtual host to serve their main domain.

Don't get me wrong, the question itself is good (I like questions as it makes me feel useful!), but the answer is always so simple:

Create another vhost.

Outline

There may not be a great deal I can add to the answer but let me outline the process of setting up a Slice and creating a virtual host (I won't go into any details of the installation and creation process - please see the relevant articles for detailed help).

When your Slice is first created it is a minimal Linux install (it doesn't matter what OS you choose).

Then the detailed stuff begins. It doesn't matter if your site is PHP based or Rails based or something else entirely. You install the language and framework basics (say Ruby and rubygems or mod_php and so on).

Once that is all done, you come to the part that allows you to server your site: creating virtual hosts.

Procedure

Greatly simplified the procedure for serving a website is as follows:

A browser send a request to your Slice IP asking for the contents of 'domain.com' (your domain name).

Your web server jumps into action and says 'yes! I have something for you'. The web server does its 'thing' and serves up an http representation of your site which is sent to the browser.

The browser then translates the http and parses it to a human form of the web site (something like this one).

All jolly good but how does your web server know what to send?

Virtual Hosts

This is where name based virtual hosts come in.

One of the first lines in any virtual host contains the domain name that is related to the vhost.

So yes, stuff.domain.com would probably point to /home/me/apps/stuff/public and so on.

The port 80 setting in the vhost is telling the web server (Apache or Nginx) to listen to port 80 and when it receives a request for stuff.domain.com on port 80 (the standard http port) to serve the relevant contents.

I hope that helps.

PickledOnion

Chris commented Fri Jun 27 12:08:22 UTC 2008:

How about sharing a complete VirtualHost block that details how the entire entry looks. Like, for example, how to point the blog.domain1.com to some content...

Great stuff!
After setting up my second slice (and I'm trying to eat it all too...) I'm grappling with this problem and have setup a similar thing in my nginx config. blah blah... Now, I am trying to test this before I re-wire the DNS config...
It may be another of those "silly" questions, but is there any way to test out the operation of the multi hosts such that I can send the browser request in and get the server to serve the appropriate virtual host content?
Sam.

The article is simply an overview of how to server multiple domains - please see the vhost articles for the webserver you are using for full details of the actual file names and what details you need in them.

Hi Pickled Onion! Even though Slicehost is for developers, some of us using it aren't very advanced. For us, your tutorials are a godsend! You write theee most amazing tech articles and tutorials that I've ever read. Thanks!

You would need to actually register a domain through another company (a domain registrar), since we only provide the hosting side of things. Once you have a domain registered you can manage its DNS either at the registrar or through Slicehost. Then when the DNS is set up, you can use the advice in this article to help you set up services for the domain.

Typically something like "subdomain A <ip>" or "subdomain CNAME <domain>" would work. Just don't put a period at the end of the subdomain name, so DNS knows to add your domain to the end of it when making a full address. For more info on DNS you can check the ebook we have up on the subject.

Ashok commented Thu Oct 21 19:14:06 UTC 2010:

I want to know if i have domain like abc.com and xyz.com then how can we handle same. If user types in browser http://abc.com/ user shuld get data of abc.com only but if user types xyz.com then of xyz.com

You would use this approach, basically, to serve both abc.com and xyz.com from the same machine. If you want more detail on doing this in apache you might check out this article series for apache - it covers installation, but the last couple articles in the series talks about setting up just what you describe, two domains served from the same machine with separate content.

Good morning! Sorry, I do not have an Servers at Slicehost, because they are overseas :-( However, the whole Articles from you are that good, it helped me so much to dive deeper in the Ubuntu/LAMP Setup. Great stuff - you where the first, that did such good customers (and non-customers) service!
Thanks
Bernd Wein

ali commented Wed Dec 29 20:55:54 UTC 2010:

hi pickedonions,

i am lost! you have done good tuts for litespeed but litespeed themseleves seem to have washed their hands off the need for any tuts or easy to follow guides or indeed 'reasonab cost' support. so you put have yourself as the only 'official litespeed superemo' amongst many other areas.

ok. my prob is i am unable to serve any of my two virtual domains from localhost on litespeed - my experiment has stopped! i don't want to use apache because it's not good at dealing with Dos attacks... what wrong? DNS, Port numbers, listners?

Hi Ali, I'm afraid Pickled Onion doesn't work on the articles anymore (he's actually been promoted up the chain in the company), and I don't know Litespeed myself. You might try posting to our forums to see if anyone there can help. I'll plan on looking into Litespeed some more, though, and maybe (if I can) get some basic, updated tutorials done.

There isn't a method of adding a virtual host without restarting that I'm aware of. I'm not as familiar with nginx as I could be, but I know apache has a "graceful" or "reload" option you can use with the service to try and restart without interrupting availability. It basically starts a new instance to handle new requests, leaving the old one in place until all of its connections are completed and inactive.

Tom commented Fri Jul 15 22:19:21 UTC 2011:

Im using nginx,passenger,rails. I have setup one domain and am using the website without problems. I tried to add the second domain with its own rails application. I modified nginx.conf and added the second server. but I always get the response from the server who is first in the .conf file. What should I be doing different?

Good post that helped me migrate off my shared hosting onto a cloud server. How many domains would you recommend being hosted on a small cloud server?

jones commented Thu Jun 14 14:26:33 UTC 2012:

Wait, so when you configure a Virtual Host, where do you set it up? Is there one file where you put all of them, or multiple files, one for each virtual host? What directory is the file/files located in?

You can see more details on setting up a virtual host in the "installing apache" series for your distribution, or in the nginx installation articles. Typically you use a separate file for each virtual host, then use an Include directive in the main config file to have the web server read all the virtual host config files.

As to where they are, it depends on the distribution. Debian-based distributions and Red Hat-based distributions have their own config layouts for apache these days, which is why I recommend the apache install articles (since they cover those locations).