I don’t personally use the MAMP application nor do I condone its use. However, a reader posted a comment asking how to setup virtual hosts within MAMP. So here you go: A simple guide on how to setup named-based virtual hosts in the MAMP environment.

If you want what’s in my opinion a more stable and faster Mac, Apache, MySQL, and PHP (MAMP) environment read my tutorial on How to setup Apache, PHP, & MySQL on Mac OS X 10.5 (Leopard). If you did not install the MAMP application and still need to setup virtual hosts on your Mac read How to setup virtual hosts on Mac OS X 10.5 (Leopard) and not the tutorial below.

Preparation
This tutorial assumes that MAMP is installed in the /Applications/MAMP directory and you configured it to use its default Apache port of 8888. If you did not simply replace all instances of 8888 in this tutorial with whatever port you have MAMP set to use for Apache (e.g. 80). Also, if you use Apache’s standard port of 80 you may have to prefix sudo to some of the commands below. To find out what port you have Apache set to use open /Applications/MAMP/MAMP.app, click the Preferences button, and then click on the Ports tab.

I will be using the GNU nano text editor in my examples since it already comes with the Mac, but if you’d like to use TextMate or BBEdit just replace nano with mate or bbedit respectively and remove any nano specific options (e.g. +n or -w).

Whenever I ask you to type something into Terminal you have to hit the return key for the command to go through. Alternatively, for you lazy ones out there, you can just copy and paste these commands.

You will first need to select a name for your virtual host; for the sake of this tutorial I will be using http://application.loc/

Before you start the instructions below open up the Terminal application in /Applications/Utilities

Editing the hosts file
Go to the Terminal window and type:
sudo nano -w /private/etc/hosts

When you put sudo in front of a command you will need to enter your root/administrator password if prompted to do so.

Hold control v until you reach the end of the file and place this text at the end, replacing application.loc with whatever name you’d like to use for your virtual host:
127.0.0.1 application.loc

You can add more hosts on new lines by using the same format.

Now you will close and save the file: hit control x to exit, then type y, and hit return to save your changes.

Setup virtual hosts
Now, type the following into Terminal:
nano -w vhosts-temp.conf

Copy and paste the text below into Terminal:

#
# Virtual Hosts
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
# <VirtualHost *:8888>
#     ServerAdmin webmaster@dummy-host.example.com
#     DocumentRoot "/www/docs/dummy-host.example.com"
#     ServerName dummy-host.example.com
#     ServerAlias www.dummy-host.example.com
#     ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
#     CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log common"
# </VirtualHost>
#
# <VirtualHost *:8888>
#     ServerAdmin webmaster@dummy-host2.example.com
#     DocumentRoot "/www/docs/dummy-host2.example.com"
#     ServerName dummy-host2.example.com
#     ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
#     CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log common"
# </VirtualHost>
# 

<VirtualHost *:8888>
    ServerName application.loc
    DocumentRoot /Applications/MAMP/htdocs/application/
    <Directory /Applications/MAMP/htdocs/application/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

Remember to replace application with your application’s folder and application.loc with the name you chose for your virtual host.

Close and save the file as I explained earlier.

Go back to Terminal and type:
mv vhosts-temp.conf /Applications/MAMP/conf/apache/vhosts.conf

If you find later you need to add more virtual hosts go to Terminal and type:
nano -w /Applications/MAMP/conf/apache/vhosts.conf

Go to the end of the file and simply paste new entries at the end of the configuration file like so:

<VirtualHost *:8888>
    ServerName application.loc
    DocumentRoot /Applications/MAMP/htdocs/application/
    <Directory /Applications/MAMP/htdocs/application/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:8888>
    ServerName newvhost.loc
    DocumentRoot /Applications/MAMP/htdocs/newapplication/
    <Directory /Applications/MAMP/htdocs/newapplication/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

Remember to replace newapplication with your application’s folder and newvhost.loc with the name you chose for your new virtual host.

Make sure to add these new virtual hosts to the end of the /private/etc/hosts file. To edit this file use the command below:
sudo nano -w /private/etc/hosts

Put new entries on a new line like so:
127.0.0.1 application.loc
127.0.0.1 newvhost.loc

Enable virtual hosts in Apache
Go to the Terminal window and type:
nano -w /Applications/MAMP/conf/apache/httpd.conf

Hold control v until you reach the bottom of the httpd.conf file and place this text at the end:
NameVirtualHost *:8888
Include /Applications/MAMP/conf/apache/vhosts.conf

Finally, to apply all our changes type this into Terminal:
/Applications/MAMP/bin/apache2/bin/apachectl restart

Test your virtual host(s)
Go to http://application.loc/ (or whatever you replaced application.loc with) in your favorite web browser to see if this works. If not let me know by leaving a comment below.

Last modified: January 10, 2010