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 ?

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)