Setting up virtual hosting with MAMP

When I started working on a Magento-based webshop I wanted to do the primary development locally. Apparently Magento doesn’t play nice with a vanilla MAMP setup so I decided to write an article on how you can configure virtual hosting for MAMP.

My apologies for not posting anything lately but I’ve been really busy developing several Magento shops. Normally I have the luxury to start development early on a test domain but for a more recent assignment this wasn’t the case. When I decided to start working on this webshop locally I encountered big problems with Magento running in a vanilla MAMP setup so I decided to share with you how you can easily turn MAMP into a more solid testing environment.

First of all get the latest version of MAMP. After installing this just follow the steps below.

Step 1: Configuring Apache

Since the default MAMP installation doesn’t offer pre-configured virtual hosting you have to add it yourself. Of course we always start by backing up our current configuration files. This can be easily done by opening Terminal and running the following commands.

cd /Applications/MAMP/conf/apache/
cp httpd.conf httpd.conf.bak

Now open httpd.conf in your favorite text-editor ( I suggest vi ) and add the following line at the end of the file.

Include /Applications/MAMP/conf/apache/sites

This extra line points Apache to the configuration files sites which we are about to create. You can also add the configuration to httpd.conf itself but that would result in a big and messy file if you decided to add more domain configurations. So for the sake of keeping things readable and maintainable we create a new file. Just run the following line in Terminal.

vi /Applications/MAMP/conf/apache/sites

And enter the following configuration into the file.

NameVirtualHost *:80

<VirtualHost *:80>
ServerName example.com
DocumentRoot /Users/yourusername/Documents/websites/example.com
</VirtualHost>

So in your Documents folder in OS X you can now create a folder called websites and in that folder you create a folder called example.com. This folder will function as the document root for your new locally hosted website.

Step 2: Tweaking your hosts file

I won’t dive into the functions of the hosts file itself because there already is a very clear explanation on it on Wikipedia. Just open up your hosts file in Terminal with the following line. You might be asked for your admin password for this.

sudo vi /etc/hosts

Then look for the line for 127.0.0.1 then edit it to look like this.

127.0.0.1       localhost example.com

You can always add more domains to it like this.

127.0.0.1       localhost example.com asecondexample.com

Save and quit your hosts file and run the following command in Terminal to flush your DNS cache.

dscacheutil -flushcache

And that’s that! You can now open up your browser and go to http://example.com to start the development of your local website.

Side note

You’ve noticed that I decided to use the domain example.com and this is because of when you use the actual domain name of the production version of the website you won’t be able to ‘reach’ it. Also be sure to clean up the test domains once in a while because you’re going to mix up the test version and the production version of your websites at least once.

Author: Luc De Brouwer

Mild-mannered software engineer, Nine Inch Nails lover, gin drinker, cat person, and somewhat of a geek. Ron Swanson is my spirit animal.

1 thought on “Setting up virtual hosting with MAMP”

Comments are closed.