How to serve multiple domains

Most people serve more than one domain on their Slice(s).

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).

You SSH into the Slice and update and secure it.

Then you install your preferred web server (Apache, Nginx, Litespeed, etc).

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.

Something like this for Apache:

<VirtualHost *:80>

  ServerName  domain1.com
  ServerAlias www.domain1.com

and something like this for Nginx:

server {

  server_name  www.domain1.com;
  rewrite ^/(.*) http://domain1.com/$1 permanent;

Each one starts slightly differently but the same principle applies - that particular virtual host will respond to queries for 'domain1.com' and 'www.domain1.com'.

Multiple domains

So, to serve different content for different domains is as simple as adding another virtual host.

Let's say you have a subdomain called 'blog.domain1.com' serving a blog (I know, shocking originality!).

The basic creation process would be to create a folder in your public_html folder with the relevant files (let's say a Wordpress install).

A virtual host would be created with the server_name or ServerName as 'blog.domain1.com' which would be configured to point to the blog files and folders in your public_html folder.

Done.

Language and frameworks

It doesn't matter what language or framework your domain uses.

To serve multiple Rails applications for example requires the same setup for each application.

Of course, there would be some differences such as port numbers for the mongrel or thin instances. Virtual hosts can't share ports.

For example, you may create a mongrel cluster to run from port 8000 - 8002 for one domain and another running from 8010 - 8012 for your blog and so on.

Summary

Once the Slice has been setup and the web server has been installed, serving multiple domains is very easy:

Simply create more vhosts...

PickledOnion

Article Comments:

chris commented Wed May 28 14:02:12 UTC 2008:

No mongrels, please :) Same for Passenger (mod_rails)? e.g. ... <virtualhost> ServerName blog.easy.org DocumentRoot /home/me/apps/blog/public

<virtualhost> ServerName stuff.easy.org DocumentRoot /home/me/apps/stuff/public ... or does this port need to be something other than 80?

thanks btw, these tutorials are great!

PickledOnion commented Wed May 28 14:08:53 UTC 2008:

Hi Chris,

It is the same for any domain and any framework you want to use.

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...

samotage commented Sun Aug 03 23:41:31 UTC 2008:

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.

vinceyoumans commented Fri Aug 29 11:24:34 UTC 2008:

you should mention the files that your editing.

PickledOnion commented Sat Aug 30 09:46:04 UTC 2008:

Hi,

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.

PickledOnion

Nabeel commented Sun Feb 15 04:22:30 UTC 2009:

Also, don't forget to add the DNS records!

Innohead commented Sat Feb 28 11:35:40 UTC 2009:

I would recommend this reference for Djangos:

http://www.djangobook.com/en/beta/chapter21/

Sergio commented Wed Apr 08 17:59:25 UTC 2009:

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!

Edison rao commented Wed May 13 16:17:29 UTC 2009:

Can i make another file to configure the new site ? like apache on ubuntu, how can I do it ?

Klaus commented Thu Apr 08 18:05:34 UTC 2010:

Thanks for this great article! Very good explanation!

tahsin hasan commented Fri Apr 30 10:30:51 UTC 2010:

hi,

how can i create new domain name in slicehost?

Jered commented Fri Apr 30 14:49:48 UTC 2010:

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.

Josh Brown commented Mon Aug 23 16:35:16 UTC 2010:

What would the DNS setting look like for the subdomain?

Jered commented Mon Aug 23 21:03:12 UTC 2010:

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

Jered commented Mon Oct 25 16:21:40 UTC 2010:

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.

Bernd Wein commented Fri Nov 26 08:05:36 UTC 2010:

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?

please give us some pointers...

thanks.

Jered commented Fri Jan 07 21:17:24 UTC 2011:

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.

Hans commented Wed Apr 13 09:46:48 UTC 2011:

Thanx for this very intrested Article. good work!

Venky commented Wed May 25 02:22:45 UTC 2011:

is it possible to add new virtual host either to apache/ngix with our restarting the service?

Jered commented Wed May 25 15:59:37 UTC 2011:

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?

bangla music commented Tue Aug 16 04:18:06 UTC 2011:

Oh nice help. Thanks for sharing.

Jon commented Mon Feb 06 19:37:11 UTC 2012:

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?

Jered commented Thu Jun 28 13:39:07 UTC 2012:

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).

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)