Mutiple local sites on your OS X Leopard

Here's how to set up multiple websites on your OS X Leopard machine, each with their own unique domain name (also useful if you need to access your site with a domain name like http://mylocalsite instead of http://localhost/mylocalsite)

We'll set up two local sites, called foo and bar, so that you can access them individually with http://foo and http://bar.

You'll need a web server already set up on your maching, and a text editor that can edit hidden files (TextEdit doesn't). I use MAMP, which runs Apache (http://www.mamp.info) and TextWrangler (http://www.barebones.com/products/textwrangler/) for text editing.

First, you need to edit the hosts file. This file is where you create new local domain names for your site...

  1. Launch the editor
  2. Open the file /private/etc/hosts
  3. You may be prompted to enter your admin password at this point - enter it
  4. Make a backup of the file (in TextWrangler, do a File > Save a Copy).
  5. Add the following two lines to the end of the file:
    127.0.0.1 foo127.0.0.1 bar
  6. Save the file
Then edit the Apache config file...
  1. Open /MAMP/Applications/conf/apache/httpd.conf (or /MAMP/Applications/conf/httpd.conf for MAMP prior to v1.0 final).
  2. Make a backup of the file.
  3. Locate the line that says Listen 8888 and change it to Listen 80
  4. Locate the Virtual Hosts section. This is usually right at the bottom of the file.
  5. Remove the '#' from the line that says # NameVirtualHost *
  6. Now add the following block of code after the "NameVirtualHost *" line, leaving a space or two after it for readability. This assumes that your root path for your sites is "/Applications/MAMP/htdocs". Each set of four lines beginning with <virtualhost> and ending with </virtualhost> defines one local site. The first one defines the default 'localhost' site, which in this case just points to the root of your local site path.
  7. <virtualhost>DocumentRoot "/Applications/MAMP/htdocs"ServerName localhost</virtualhost><virtualhost>DocumentRoot "/Applications/MAMP/htdocs/foo"ServerName foo</virtualhost><virtualhost>DocumentRoot "/Applications/MAMP/htdocs/bar"ServerName bar</virtualhost>
  8. Save the httpd.conf file
  9. Restart MAMP (or whichever way you start Apache)
Now you can launch your browser and browse your local sites with http://foo and http://bar

==============
NB 1: If you don't want to use TextWrangler or install any additional editors, you can use Terminal and your system's built-in editor:

  1. Open Terminal (Applications > Utilities > Terminal)
  2. Use an editor, e.g. vi or nano, to edit the hosts file (I use nano):
    sudo nano /private/etc/hosts
  3. Use the editor to edit httpd.conf file:
    sudo nano /Applications/MAMP/conf/apache/httpd.conf
==============
NB 2: If you are using Leopard's built-in Apache server, you will need to edit two Apache config files:
  1. Open /etc/apache2/httpd.conf and remove the '#' from the line "#Include /private/etc/apache2/extra/httpd-vhosts.conf" and save the file.
  2. Open /etc/apache2/extra/httpd-vhosts.conf and follow steps above for inserting virtual host definitions (<virtualhost> ... </virtualhost>).
  3. Save the file, and then restart Apache.

Hope that helps!

Filed under  //  apache   localhost   mamp   osx   virtual hosts  
Posted by Farez Rahman