I’ve been using the regular MAMP for a long time without problems but recently I wanted to start experimenting with subdomains on my local development server using Virtual Hosts.
I knew that MAMP Pro supported Virtual Hosts and so I fired up the trial and indeed they do make it very easy for you to add new host names: simply add a server name and a directory for your files and you’re good to go. If ease and convenience is important to you, then just get the Pro version and be done with it.
However, I like to know how things work and so, after a bit of poking around in my /etc/hosts and the various httpd.conf files in both the /Applications/MAMP and /Applications/MAMP Pro directories, I found that it was actually pretty straightforward to add new virtual hosts manually.
Adding Your New Domains to the Hosts File
Throughout this post I’ll be using the example domain name
- Open up a terminal window and make a backup of your hosts file by typing
(as this is a system file, you’ll probably need to enter your password)
- Edit the hosts file in Nano by typing the following:
Near the start of the file, you’ll see the line:
127.0.0.1 localhost. You can simply add your new domain to this line so that it reads:
I also added each of my domains to the IPv6 version (which is declared a few lines later), 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. I found that it was taking up to 5 seconds to resolve my new domain names. Adding the domains to the one line seemed to help fix this problem).
Ctrl + xand then
ywill write out your changes
Preparing the Virtual Hosts in Your httpd.conf File
- Go to your MAMP application folder and find the configuration files for Apache. It should be in
/Applications/MAMP/conf/apache/For my version of MAMP, they use includes to separate out some aspects of the configuration files including virtual hosts. This seems like a nice way of keeping things organised and allows you to easily play around with virtual hosts without messing up your main httpd.conf file so that’s what we’re going to do here.
- Open up httpd.conf and scroll all the way down to the bottom. You’re looking for the line:
It might have a
# symbol at the start, meaning that it’s commented out. Delete that symbol. If it’s not there at all, simply add the line.
1. Save and exit out and go back into the configuration folder (
/Applications/MAMP/conf/apache/). If you have a subfolder called
extra, open that up. If not, create it.
1. Open up
httpd-vhosts.conf. If it doesn’t exist, create it. If you didn’t create it yourself, you might see some example dummy hosts in there. You can ignore these or delete them.
1. If the following line doesn’t exist in your file, create it:
- The first thing we have to do is to create the default configuration. This is what Apache will use if it can’t find a server that is pointing to your localhost IP address but hasn’t been declared as a unique virtual host. Copy and paste this:
1 2 3
/Path/To/Your/Default/Document/Root to the path where you store your web files (default in MAMP is
/Applications/MAMP/htdocs, but you may have changed this).
Adding New Web Document Roots
With virtual hosts, you can use different folders for different document roots. For example, I have a web design clients folder and I hold all of my web files for that client in an individual webroot folder within that design folder. This makes archiving and organisation easy (though if you do move the folder, you’ll need to remember to update the Virtual Hosts file).
- Create a new folder for your virtual host somewhere on your system. Make a note of its location. You may also want to add a quick
index.htmlfile to that folder which will tell you if it’s working or not.
- Copy out the following into the
httpd-vhosts.conffile, underneath the first VirtualHost declaration:
1 2 3 4 5 6 7 8 9 10
Important things to note:
ServerNameshould be what you set your
/etc/hostsdeclaration to (in this case, line-in.co.uk).
- Virtual hosts can contain most Apache directives. For directory-specific directives, create a new
declaration. You generally want to copy your set up from your main Apache directory configuration (see `httpd.conf`) so that things work consistently across all of your hosts. One important declaration is `AllowOverride All`. This allows you to use `.htaccess` files in your new directory.
Save this file out and restart Apache. If everything is working, you should be able to open your browser and go to your new host (e.g. http://line-in.co.uk) and it should open up the
index.html file you created.
Congratulations, you now have a working virtual hosts set up!
You can add as many of the
Everything went smoothly following the above for me but your mileage may vary, especially if you are using older versions of MAMP or have heavily configured your httpd.conf file.
If Apache doesn’t start at all, check your
httpd-vhosts.conf file for errors (make sure the closing quotes match, that you’ve closed all open directive tags and that you’ve spelled the directives correctly). If you just can’t get it to work, you can always open up your
httpd.conf file and comment out (with a #) or delete this line:
This will effectively ëturn off’ your Virtual Hosts configuration and get you back to a working development server.