Creating a virtual host in MAMP

Updated: 5 January, 2014

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. You need to be logged in as a Mac administrator to edit the files

  1. Open TextWrangler (a free text editor available from From the File menu, select Open. At the bottom of the Open dialog box, select Everything from the Enable drop-down menu, and make sure the Show hidden items checkbox is checked. Then navigate to Macintosh HD/private/etc/hosts, and click Open.

  2. This opens a system file that contains the following line: localhost

  3. Place your cursor at the end of that line, and press Return. An alert will ask if you're sure you want to unlock "hosts". Click Unlock.

  4. On the next line, type followed by a space and the name of the virtual host you want to create. For example, if you want to call the virtual host mysite, add the following line: mysite

  5. If you want to create more than one virtual host, add each one on a new line beginning with This is the loopback IP address that refers to your local computer.

  6. Save the file when you have finished adding the names of your virtual hosts. Because it's owned by "wheel", you will be prompted to enter your Mac password.

    Next. you need to tell Apache about the virtual host.

  7. Use TextWrangler to open the main Apache configuration file, httpd.conf, which is located at Applications/MAMP/conf/apache/httpd.conf.

  8. Scroll to the bottom of the file and locate the following lines (around 524–525):

    # Virtual Hosts # Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

  9. Remove the hash (pound) sign from the beginning of the line that begins with Include:

    # Virtual Hosts Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

  10. Save the file, and then open Applications/MAMP/conf/apache/extra/httpd-vhosts.conf. This is where you define the virtual hosts.

  11. At the bottom of the page are two examples of how to define virtual hosts in Apache. They look like this:

    <VirtualHost *:80> ServerAdmin DocumentRoot "/Applications/MAMP/Library/docs/" ServerName ServerAlias ErrorLog "logs/" CustomLog "logs/" common </VirtualHost> <VirtualHost *:80> ServerAdmin DocumentRoot "/Applications/MAMP/Library/docs/" ServerName ErrorLog "logs/" CustomLog "logs/" common </VirtualHost>

  12. Edit both examples. Virtual hosts override the existing localhost, so the first one needs to re-establish localhost. Edit the second one for the virtual host you want to add. Only the DocumentRoot and ServerName directives are required. To add a virtual host for mysite, the edited definitions should look like this:

    <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.

  13. If you want to create more than one virtual host, copy one of the definitions, and edit it accordingly.

  14. 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

PHP SolutionsBeginning CSS3Dreamweaver CS6: Learn by VideoDreamweaver CS5.5: Learn by VideoDreamweaver CS5.5 for MobileDreamweaver CS5 with PHP