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...- Launch the editor
- Open the file /private/etc/hosts
- You may be prompted to enter your admin password at this point - enter it
- Make a backup of the file (in TextWrangler, do a File > Save a Copy).
- Add the following two lines to the end of the file:
127.0.0.1 foo127.0.0.1 bar
- Save the file
- Open /MAMP/Applications/conf/apache/httpd.conf (or /MAMP/Applications/conf/httpd.conf for MAMP prior to v1.0 final).
- Make a backup of the file.
- Locate the line that says Listen 8888 and change it to Listen 80
- Locate the Virtual Hosts section. This is usually right at the bottom of the file.
- Remove the '#' from the line that says # NameVirtualHost *
- 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.
- Save the httpd.conf file
- Restart MAMP (or whichever way you start Apache)
<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>
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:
- Open Terminal (Applications > Utilities > Terminal)
- Use an editor, e.g. vi or nano, to edit the hosts file (I use nano):
sudo nano /private/etc/hosts - 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:
- Open /etc/apache2/httpd.conf and remove the '#' from the line "#Include /private/etc/apache2/extra/httpd-vhosts.conf" and save the file.
- Open /etc/apache2/extra/httpd-vhosts.conf and follow steps above for inserting virtual host definitions (<virtualhost> ... </virtualhost>).
- Save the file, and then restart Apache.
Hope that helps!