Creating a virtual host in MAMP

The easiest way to set up virtual hosts in MAMP is to use the commercial version of MAMP, which automates the process through the MAMP Pro console. However, if you don't want to buy MAMP Pro, it's quite simple to do it manually by editing two files.

  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. For example, to create a virtual host called mysite add this:

    127.0.0.1 mysite

  5. Save the file. Because it's owned by "root", you will be prompted to enter your Mac password. Next. you need to tell Apache about the virtual host.

  6. Use BBEdit or TextWrangler to open the main Apache configuration file, httpd.conf, which is located at Applications:MAMP:conf:apache:httpd.conf.

  7. Scroll to the bottom of the file and locate the following line:

    # NameVirtualHost *

  8. Remove the hash (pound) sign from the beginning of the line.

  9. Virtual hosts replace the existing setup, so you need to create a virtual host for localhost and one for each virtual host you want to add. Add the following code at the bottom of httpd.conf:

    <VirtualHost *:80>    DocumentRoot /Applications/MAMP/htdocs    ServerName localhost </VirtualHost> <VirtualHost *:80>    DocumentRoot "/Users/username/Sites/mysite"    ServerName mysite </VirtualHost>

    This assumes that you want to locate the files for mysite in your Sites folder. Replace "username" in the second definition with your own Mac username. If you want to store the files in a different location, adjust the value of DocumentRoot accordingly.

  10. Save all the files you have edited, and restart the servers in the MAMP control panel. You should now be able to access the virtual host with the following URL: http://mysite/.

Books & Videos by David Powers

Beginning CSS3Dreamweaver CS6: Learn by VideoDreamweaver CS5.5: Learn by VideoDreamweaver CS5.5 for MobilePHP SolutionsDreamweaver CS5 with PHP