When you first install MAMP, it's configured to serve content from a single directory (typically /Applications/MAMP/htdocs
). For developers working on multiple projects simultaneously, this single-site approach becomes limiting quickly. The solution lies in configuring virtual hosts.
Here's how to set up multiple development sites:
# 1. Edit hosts file
sudo nano /etc/hosts
# Add entries like:
127.0.0.1 project1.local
127.0.0.1 project2.local
Navigate to /Applications/MAMP/conf/apache/httpd.conf
and uncomment or add:
# Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Then edit /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
:
<VirtualHost *:80>
DocumentRoot "/path/to/project1"
ServerName project1.local
<Directory "/path/to/project1">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/path/to/project2"
ServerName project2.local
<Directory "/path/to/project2">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
For developers who prefer port-based access:
Listen 8081
Listen 8082
<VirtualHost *:8081>
DocumentRoot "/path/to/project1"
ServerName localhost
</VirtualHost>
<VirtualHost *:8082>
DocumentRoot "/path/to/project2"
ServerName localhost
</VirtualHost>
For frequent setup changes, consider creating a bash script:
#!/bin/bash
echo "127.0.0.1 $1.local" | sudo tee -a /etc/hosts
mkdir -p "/Users/youruser/Sites/$1"
cat << EOF >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/Users/youruser/Sites/$1"
ServerName $1.local
<Directory "/Users/youruser/Sites/$1">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
After making changes:
- Restart MAMP servers
- Test each site by visiting the configured domains
- Check MAMP's Apache error logs if sites don't load
When developing multiple websites locally using MAMP (Mac/Apache/MySQL/PHP), virtual hosts allow you to run separate sites under different domain names on your development machine. This mimics a production environment where each site has its own domain.
First, edit your Apache configuration file. In MAMP PRO, this can be done through the UI. For regular MAMP:
# Open httpd.conf
sudo nano /Applications/MAMP/conf/apache/httpd.conf
# Ensure this line is uncommented
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Edit the virtual hosts file:
sudo nano /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
# Add configurations like this:
<VirtualHost *:80>
ServerName project1.test
DocumentRoot "/Users/yourname/Sites/project1"
<Directory "/Users/yourname/Sites/project1">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName project2.test
DocumentRoot "/Users/yourname/Sites/project2"
<Directory "/Users/yourname/Sites/project2">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Map the domains to localhost:
sudo nano /etc/hosts
# Add these lines
127.0.0.1 project1.test
127.0.0.1 project2.test
After making these changes:
# Restart MAMP servers through the UI or command line
/Applications/MAMP/bin/apache2/bin/apachectl restart
Create test files in each project directory:
# For project1
echo "<?php phpinfo(); ?>" > /Users/yourname/Sites/project1/index.php
# For project2
echo "<h1>Project 2 Works!</h1>" > /Users/yourname/Sites/project2/index.html
Now visit http://project1.test and http://project2.test in your browser to verify.
For SSL development, add HTTPS virtual hosts:
<VirtualHost *:443>
ServerName secure.test
DocumentRoot "/Users/yourname/Sites/secure"
SSLEngine on
SSLCertificateFile "/Applications/MAMP/conf/apache/server.crt"
SSLCertificateKeyFile "/Applications/MAMP/conf/apache/server.key"
</VirtualHost>
- Check Apache error logs at /Applications/MAMP/logs/apache_error.log
- Ensure DocumentRoot paths are correct and accessible
- Verify ServerName matches exactly what's in your hosts file
- Clear browser cache when testing changes
For developers managing many projects, consider creating setup scripts:
#!/bin/bash
# new_vhost.sh
echo "Creating virtual host for $1..."
echo "<VirtualHost *:80>
ServerName $1.test
DocumentRoot \"/Users/yourname/Sites/$1\"
<Directory \"/Users/yourname/Sites/$1\">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>" >> /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
echo "127.0.0.1 $1.test" >> /etc/hosts
mkdir -p "/Users/yourname/Sites/$1"