MAMP Virtual Domains

Set up virtual domains for MAMP with this great hack from springworks. Be sure to quit MAMP first.

From the tip on the Joynet board:
  1. With a text editor that can open hidden files (eg TextWrangler (free) or TextMate), open /private/etc/hosts
  2. Find the line which reads

Code:

127.0.0.1    localhost
3. add the name of your chosen virtualhosts at the end of this line, separating each of them with a space, like this:

Code:

127.0.0.1    localhost mytestdomain1.dev mytestdomain2.dev

4. Save the file. You will need to provide an Administrator password to do this.

5. Create a folder for each of your test domains in the Sites folder in your home folder, eg: /Users/myusername/Sites/mytestdomain1.dev /Users/myusername/Sites/mytestdomain2.dev

6. Now open the MAMP httpd.conf, which you can find in /Applications/MAMP/conf/apache/httpd.conf

7. At the end of this file, insert the following:

Code:

NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Users/myusername/Sites"
</VirtualHost>
<VirtualHost *:80>ServerName mytestdomain1.dev
DocumentRoot "/Users/myusername/Sites/mytestdomain1.dev"
</VirtualHost>
<VirtualHost *:80>
ServerName mytestdomain2.dev
DocumentRoot "/Users/myusername/Sites/mytestdomain2.dev"
</VirtualHost>

8. In the MAMP preferences, choose the Ports section and click on the "Use default Apache and MySQL ports". Now stop the MAMP servers and restart them again.

You should now be able to reach all of your test domains at the same time, using just the domain name as the address, none of this "localhost:8888/mytestdomain1/" stuff.

Comments

Anonymous said…
very good!
ottoa said…
I can see virtual host on my mac 10.5

But can't access from 2nd mac & pc on same network

What am I missing?
Any additional setting
(in network or dns or ...)?

_____________________________________________
## 1. Host Database
# 1. localhost is used to configure the loopback interface 2. when the system is booting. Do not change this entry.
##
127.0.0.1 localhost media1.dev media2.dev
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
______________________________________

in httpd.conf
________________________________________

NameVirtualHost *:80

VirtualHost *:80>
ServerName localhost
DocumentRoot "/users/myname/Sites"
/VirtualHost>

VirtualHost *:80>
ServerName media1.dev
DocumentRoot "/users/myname/Sites/1site"
/VirtualHost>

VirtualHost *:80>
ServerName media2.dev
DocumentRoot "/users/myname/Sites/2site"
/VirtualHost>
Neil said…
If you want to see your new Mac virtual hosts from other computers on your LAN (as otta is attempting, above) you need to inform those other computers about the virtual host name and corresponding IP address, since you can't access virtual domains with only an IP address.

There are two ways to do this: either update the hosts file on each computer, or set up a DNS (name) server for your LAN.

Note that you'll need to know the IP address of your MAMP server computer, which should be a fixed (not DHCP) address. If you are using DCHP for your MAMP server, there's a good chance your host/DNS mappings will break when your server is auto-assigned a new IP address.
berkelep said…
This was quite helpful. Thanks!

Popular Posts