I'm trying to create subdomains on my localhost and am way out of my territory... I'm running MAMP on my Mac OS X and I thought/think I had/have to do the following:

(Assuming I want to create me.localhost.com and you.localhost.com)

(1) Edit /private/etc/hosts

Right now, it looks like this:       localhost broadcasthost
::1             localhost
fe80::1%lo0     localhost

So, do I just make it:       localhost       me.localhost.com       you.localhost.com broadcasthost
::1             localhost
fe80::1%lo0     localhost

(2) I'm assuming I don't need to mess with DNS at all because it's local? So, the hosts file should suffice?

(3) And then, I need to edit my httpd.conf file to include virtual hosts? I tried this, but it's not picking it up...

NameVirtualHost *

<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs"
ServerName localhost

<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/me.localhost.com"
ServerName me.localhost.com

<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/you.localhost.com"
ServerName you.localhost.com

Not sure if I'm way off-base here... Help is greatly appreciated!


For local name resolution:

Do you need the localhost.com? In my environment:

$ vi /etc/hosts       localhost       box1 box2 box3

$ ping box1

returns "" just fine.

In the browser, I can then also resolve http://box1 , http://box2, and so on.

On 10.5, 10.6 at least I can confirm that OSX should not overwrite your /etc/hosts, which was a question by the previous answer.

For Apache, for each new virtual host, add:

<VirtualHost *:80>

ServerName box1

DocumentRoot /full/path/to/documentroot/



Since your browser will properly resolve 'box1' as, your request will be routed to the local box, where apache will read the header and see that it was intended for 'box1' and will use the corresponding virtual host.

Alternatively, you can also use one of the (many?) tools to automate this for you, like ( I am not affiliated with this, just quick googled while trying to find a different example I was thinking of: http://clickontyler.com/virtualhostx/ )


Note that all aliases for should be specified on a single line or you'll get strange results (in Linux at least). localhost me.localhost.com you.localhost.com

VirtualHost gets the name of the virtual host you want to define. <VirtualHost *> defines the catch-all default. So, that should be

<VirtualHost me.localhost.com>

(And I hope you realize that me.localhost.com is, strictly speaking, a violation of TwoCows namespace and completely different from me.localhost.)

I'm not too sure if/how often OS X will overwrite /etc/hosts, though. It may become necessary to work with Open Directory – but as I just realized, the man page for that (man DirectoryService) in Snow Leopard points to a non-existing utility (/Applications/Utilities/Directory Utility.app). I.e., you'd need to use dscl and, probably, go to /Local/Default/Hosts and add your entries there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy