Creating a virtual host in Mac OS X (Leopard)

Leopard no longer has NetInfo Manager, so you need to edit a file called /private/etc/hosts directly in a script editor. If you're running OS X 10.5, and want to use a virtual host for local development, use these instructions in place of those in the book.

  1. Open BBEdit or TextWrangler (a free, cut-down version of BBEdit available from www.barebones.com). From the File menu, select Open Hidden. In the Open dialog box, select All Files from the Enable drop-down menu. Then navigate to Macintosh HD:private:etc:hosts, and click Open.
  2. This opens a system file, so you need to unlock it by clicking the icon of a pencil with a line through it at the top-left of the toolbar, as shown in the following screenshot:
    Mac hosts file
  3. You will be told that the document is owned by "root", and be asked to confirm that you want to unlock it. Click Unlock. This removes the line through the pencil, and readies the file for editing.
  4. Place your cursor on a new line at the end of the file, and type 127.0.0.1 followed by a space and the name of the virtual host you want to create. If you're following the examples in The Essential Guide to Dreamweaver CS3, it should look like this:Hosts file after editing
  5. Save the file. Because it's owned by "root", you will be prompted to enter your Mac password. That takes care of the changes that used to be made in NetInfo Manager. You now need to tell Apache about the virtual host.
  6. Use BBEdit or TextWrangler to open the main Apache configuration file, httpd.conf. It's a system file, so you need to open and unlock it in the same way as the hosts file. It's located at Macintosh HD:private:etc:apache2:httpd.conf.
  7. Scroll down to around line 460 and locate the following lines:
    # Virtual hosts
    #Include /private/etc/apache2/extra/httpd-vhosts.conf
  8. Remove the hash (pound) sign from the beginning of the second of these two lines like this:
    Include /private/etc/apache2/extra/httpd-vhosts.conf
    This enables the configuration file for virtual hosts, which now needs to be edited.
  9. Use BBEdit or TextWrangler to open httpd-vhosts.conf. Again, it's a system file, so needs to be handled the same way as the previous two files. The file is located at Macintosh HD:private:etc:apache2:extra:httpd-vhosts.conf.
  10. The section of the file that you're interested in is shown in the following screenshot:
    Virtual hosts configuration in Apache
    Lines 27–42 of the preceding screenshot are examples of virtual host definitions. You need to replace these with your own definitions. When you enable virtual hosting, Apache disables the main server root, so the first definition needs to reproduce it.
  11. You don't need all the options shown in the examples, so replace the code shown on lines 27–42 of the preceding screenshot with the following:
    <VirtualHost *:80>
       DocumentRoot "/Library/WebServer/Documents"
       ServerName localhost
    </VirtualHost>

    <VirtualHost *:80>
       DocumentRoot "/Users/username/Sites/mactale"
       ServerName mactale
    </VirtualHost>

    Replace "username" in the second definition with your own Mac username.
  12. Save all the files you have edited, and restart Apache by going to Sharing in System Preferences, deselecting Web Sharing, and selecting it again. You should now be able to access the virtual host with the following URL: http://mactale/.
-->