How to set up hosts file for local environment?

5,060

Solution 1

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

Solution 2

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:
5,060
Zoredache
Author by

Zoredache

Real Name: Chris Francy I work as a Senior Network Analyst at Northwest Educational Service District #189 in the Technology Services department. The Technology Service department, in addition to supporting the staff at NWESD, provides network support services to 35 K-12 school districts in Northwest Washington region. In my free time, when I am not at work or answering questions, I play a lot of video games on the PC (Steam Profile).

Updated on September 17, 2022

Comments

  • Zoredache
    Zoredache almost 2 years

    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!