Setting up "subdomains"/virtual hosts on a Mac OS X Server


This page deals with how to set up your Mac OS X Server so that it can serve more than one domain name, or subdomains on top of its one domain name, without having to use more than one IP address. In other words, your server is "www.aaronwells.com". You want it to be able to serve other websites at "webmail.aaronwells.com", "gregory.aaronwells.com", or even "www.dirtycouches.com".

I recently had to do this for work, and I found the manuals and online help communities to be inadequate. It was only after much digging and a little experimenting that I managed to unearth the truth. I'm a total newb to server administration and to DNS and networking. I've been hosting my own websites off of commercial web hosts for a long time, but haven't set up my own server until quite recently. So the information I give you here may be completely inaccurate. I offer no warranty, implied or otherwise, that the advice below is correct, accurate, or safe. Always believe the manuals, the help files, and more experienced users rather than me, and proceed only at your own risk.

When you're using a commercial web host, they refer to the prepending of words to the front of your domain as "subdomains". For instance, shitblog.iowaline.net and aaronslist.iowaline.net are both referred to as "subdomains" in the administration panel for this site.

Real server admins, however, scoff when they hear "subdomains" used like that. In actuality, a true "subdomain" is a networking abstraction that has to do with the grouping together of subgroups of servers. When it's just one server, serving the pages in a different directory when it sees "subdomain.domain.com" instead of "www.domain.com", that's actually a virtual host.

So, rather than give out useful advice when someone asks how to set up subdomains on their Mac OS X Server, experienced admins get confused, or tell them to set up a "virtual host". However, the words "virtual host" never appear in the Web Services manual, making that advice not evry useful.

It turns out to be pretty easy to set up virtual hosts on a Mac OS X Server, though. Here's how I did it. You may need to change some settings, depending on your network setup, but this should give you a start.

  1. On your name server(s), set up a DNS record for the fully qualified domain name you want to create (i.e. "subdomain.domain.com" or "www.huffing.org"). The type of record you need to create is an alias, or CNAME, pointing from the fully qualified domain name to the IP address of your Mac server.

  2. Create a new web site in Server Admin. Under the "General" tab, put the fully qualified domain name. Leave IP adress as "any". Set the rest of the settings however you need them for your website.

That's it

Now save, and restart the Web Service. When someone (who has access to the DNS information in your name servers) tries to go to the new domain name, the request gets sent to your server. If their web browser was programmed later than 1995, it'll be using HTTP 1.1, which contains the domain name of the requested address in the GET request. Apache sees the requested domain name, looks for a web site record that matches it, and serves up those pages.

One more thing to consider: If Apache receives a page request that only has an IP address, or for some other reason is ambiguous enough to match a couple of your web sites, it will go to the web site which is highest up on the list on the Sites pane in Server Admin. So you'll probably want to set your most generic site to the top of the list.

And on a final note: This setup will not route e-mail to virtual hosts. You can easily enough set up your SMTP server to receive e-mail from several domain names (and on a side-note, your SMTP server actually sends and receives e-mail; IMAP just distributes it to users), but it'll just act like they're all the same domain name. So, if you set it to receive e-mail to domain.com and niamod.com, then it will see the e-mail address user@domain.com and user@niamod.com as identical.

It is possible to set up e-mail for virtual hosts. Again, that's what commercial web hosts do; if you have to type your full e-mail address with your domain name as your e-mail login, it's because you're on a virtual host. However, there is no GUI to do this on Mac OS X Server. You'll have to do it through the command line. A google search for "postfix virtual domain" might yield some useful results.

This page last updated 10/22/1994