Gavin
Gavin

Reputation: 7944

VirtualHosts in MAMP

(I've read many StackOverflow articles that address vhosts in MAMP, but none of them solved this.)

I'm trying to set up a virtual host on my MacBook. I'm using the exact same virtual host setup as I am on my Windows machine, which works fine.

First I edited the hosts file. Under the 127.0.0.1 localhost line I added:

127.0.0.1 dev.mysite.com

Next I edited /Applications/MAMP/conf/apache/httpd.conf and removed the # from the vhosts include:

# Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

Next I edited /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf:

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot "/Users/Gavin/Web"
   ServerName localhost
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot "/Users/Gavin/Web/mysite/public_html"
   ServerName dev.mysite.com
</VirtualHost>

Next I opened Terminal and ran dscacheutil -flushcache and restarted MAMP. localhost works fine, dev.mysite.com gives the standard Chrome This webpage is not available error.

Upvotes: 2

Views: 3871

Answers (1)

ek9
ek9

Reputation: 3442

You must also define <Directory> in vhosts for it to work properly. I believe localhost works only because you have it defined in main apache configuration.

You should disable main localhost server in main config, and have this in httpd-vhosts.conf:

## localhost                                                                 
<VirtualHost *:80>                                                                
    DocumentRoot /Users/Gavin/Web                                           
    ServerName localhost                                                       
    ServerAlias www.localhost                                                  

    <Directory "/Users/Gavin/Web">                                          
        Allow from All                                                                  
        AllowOverride all                                                               
        Options -Indexes +FollowSymlinks                                                
    </Directory>                                                                      

    UseCanonicalName on                                                               
</VirtualHost> 

## dev.mysite.com                                                                 
<VirtualHost *:80>                                                                
    DocumentRoot /Users/Gavin/Web/mysite/public_html                                           
    ServerName dev.mysite.com                                                       
    ServerAlias www.dev.mysite.com                                                  

    <Directory "/Users/Gavin/Web/mysite/public_html">                                          
        Allow from All                                                                  
        AllowOverride all                                                               
        Options -Indexes +FollowSymlinks                                                
    </Directory>                                                                      

    UseCanonicalName on                                                               
</VirtualHost>    

Also, to make sure that chrome is really picking up /etc/hosts file, type your URLs with http:// in the address-bar like this:

http://localhost http://dev.mysite.com

It is not a good idea to have .com for local, so do not use that. Use .localhost TLD instead to avoid further problems. .local won't work well on Mac.

Upvotes: 2

Related Questions