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.
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.
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:
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.
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
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.
Use BBEdit or 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 line:
# NameVirtualHost *
Remove the hash (pound) sign from the beginning of the line.
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
<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
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: