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
Open TextWrangler (a free text editor available from www.barebones.com). 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.
This opens a system file that contains the following line:
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.
On the next line, type
127.0.0.1followed 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:
If you want to create more than one virtual host, add each one on a new line beginning with
127.0.0.1. This is the loopback IP address that refers to your local computer.
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.
Use TextWrangler to open the main Apache configuration file,
httpd.conf, which is located at
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
Remove the hash (pound) sign from the beginning of the line that begins with
# Virtual Hosts Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Save the file, and then open
Applications/MAMP/conf/apache/extra/httpd-vhosts.conf. This is where you define the virtual hosts.
At the bottom of the page are two examples of how to define virtual hosts in Apache. They look like this:
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot "/Applications/MAMP/Library/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin email@example.com DocumentRoot "/Applications/MAMP/Library/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "logs/dummy-host2.example.com-error_log" CustomLog "logs/dummy-host2.example.com-access_log" common </VirtualHost>
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
ServerNamedirectives 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
Sitesfolder. 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
If you want to create more than one virtual host, copy one of the definitions, and edit it accordingly.
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: