First of all the path to your hosts file on OS X is:

/private/etc/hosts

The "hosts" file is in the "etc" folder. Quick command:

CODE:
  1. sudo nano /private/etc/hosts

If you need more help and don't understand the format of the hosts file or need additional help then please continue, if you don't need the help than you, probably, have already left and are not reading this any longer.

For everyone else, adding Hostname to IP address mapping to your hosts file in OS X is essential for doing any development for yourself or for clients.

Doing this allows you to shorten your paths when accessing your website in development on the development server. This is assuming that you are setting up Virtual Host's on your development server. I use Apache as my development server and will cover setting up Apache Virtual Hosts at a later time. Until then there are many good examples.

An example of how this will work, you could access:

http://192.168.1.2/clients/client_name/html

Or, you could access:

http://client_name

While accessing the same content in both of the examples. The above example assumes that your development server is located on your LAN with an IP address of 192.168.1.2. Additionally, my example assumes that you have a Virtual Host called "client_name" setup up in Apache.

Let's Get Started

You can accomplish this a few ways, but I will be covering the method of adding it via a command line utility with Apple's Terminal or iTerm. If you are not comfortable with using the command line, even though the steps are very easy, you could use the GUI.

Here are the steps to take:

  1. Open your command line utility.
  2. Backup your original "hosts" file. Type "sudo cp /private/etc/hosts /private/etc/hosts.bak"
  3. Type "sudo nano /private/etc/hosts"
  4. Type your password to confirm that you want to edit the "hosts" file.
  5. Edit your "hosts" file with your new entries
  6. Press "ctrl-x" to save the changes
  7. Press "y" to confirm you would like to save the changes
  8. Press "return" to save the file as the same file name
  9. Test your new "hosts" file.
  10. Remove your backed up "hosts.bak" file.

Backup Original "hosts" File

CODE:
  1. sudo cp /private/etc/hosts /private/etc/hosts.bak

Open the File for Editing

Type the command and the terminal utility will prompt you for your password to continue editing the file.

CODE:
  1. sudo nano /private/etc/hosts

Editing the "hosts" file

Your "hosts" file will look something like:

CODE:
  1. ###
  2. # Host Database
  3. #
  4. # localhost is used to configure the loopback interface
  5. # when the system is booting.  Do not change this entry.
  6. 127.0.0.1     localhost

As you can see by the sample "hosts" file the IP address goes first then you press "tab" and type the Hostname. In our example situation we want to go to:

http://client_name

And have your browser internally go to:

http://192.168.1.2/clients/client_name/html

To do this we will edit our "hosts" file to look like:

CODE:
  1. ###
  2. # Host Database
  3. #
  4. # localhost is used to configure the loopback interface
  5. # when the system is booting.  Do not change this entry.
  6. 192.168.1.2     localhost
  7. 192.168.1.2     client_name

Our new "hosts" file will do two things for us.

  1. Redirect your browser to your development server's web root if you point your browser to http://localhost.
  2. Redirect your browser to your development server in the correct folder (http://192.168.1.2/clients/client_name/html) if you point your browser to http://client_name

Save the "hosts" File

Now all we need to do is save our "hosts" file with the following steps.

  1. Press "ctrl-x" to save the changes
  2. Press "y" to confirm you would like to save the changes
  3. Press "return" to save the file as the same file name

Testing and Cleanup

Now all we need to do is to point our browser to http://client_name and your browser will pull up your clients website. If it does not then the "hosts" file is in the wrong format.

If everything worked correctly all we need to do is delete the "hosts.bak" file and we are all done. You can do this by typing the following into your command line utility:

CODE:
  1. sudo rm /private/etc/hosts.bak

Type in your password and the file is gone.

Apache Development Linux Mac OS X