How to Set Up Virtual Hosts With MAMP on Mac OS X?

The simplest way to set up virtual hosts in MAMP is to use the commercial version of MAMP pro, which automates the procedure to create virtual hosts through the MAMP Pro console.

However, if you don’t want to buy MAMP Pro, it’s quite easy to do it manually by modifying two files. You need to be logged in as a Mac administrator to modify the files.

Follow following steps to create virtual hosts :

Step 1: Adding Your New Domains to the Hosts File
Throughout this post I will create virtual host for the example domain name example.dev and test.dev.
Now edit the hosts file in Nano by typing the following command on terminal:

Near the start of the hosts file, you’ll see the line: 127.0.0.1 localhost. On the next line, type 127.0.0.1 followed by a space and the name of the virtual host you want to create as in below example:

If you are thinking about creating more than one virtual host, add each one on a new line starting with 127.0.0.1.
I also added each of my domains to the IPv6 version, so it looks like this:

This is a good idea because there are reports that OS X Lion’s DNS cache is a bit rubbish.

Now save and close the file.

Step 2: Preparing the Virtual Hosts in Your httpd.conf File
Open the main Apache configuration file httpd.conf, which is located at Applications/MAMP/conf/apache/httpd.conf. Scroll to the bottom of the file and locate the following lines (around 524–525).

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

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, add following code to define virtual host for your domain:

Here in the above example DocumentRoot represent to the directory of the virtual host domain.
And ServerName is to represent the virtual host domain name.

Now save and close the file, Then restart the servers in the MAMP control panel.

All steps are done. Now can test it by visiting virtual host domains (example.dev and test.dev) on your browser.

Leave a Reply

Your email address will not be published. Required fields are marked *