Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost

So, do I just make it:

127.0.0.1       localhost
127.0.0.1       me.localhost.com
127.0.0.1       you.localhost.com
255.255.255.255 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>

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

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

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

share|improve this question

migrated from stackoverflow.com Jan 31 '10 at 17:55

This question came from our site for professional and enthusiast programmers.

3 Answers 3

For local name resolution:

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

$ vi /etc/hosts

127.0.0.1       localhost
127.0.0.1       box1 box2 box3

$ ping box1

returns "127.0.0.1" 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/

...[etc]

</VirtualHost>

Since your browser will properly resolve 'box1' as 127.0.0.1, 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/ )

share|improve this answer

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

127.0.0.1 localhost me.localhost.com you.localhost.com
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.