Mac – Creating multiple Virtual Hosts in Apache the right way (macosx)

apache-2.2hostmacmac-osxvirtualhost

I got a strange problem trying to define multiple virtual hosts in Apache.
This is my code for httpd.conf file:

<VirtualHost *:8888>
    ServerName site1.local
    DocumentRoot /Applications/MAMP/htdocs/site1/public

    <Directory /Applications/MAMP/htdocs/site1/public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:8888>
    ServerName site2.local
    DocumentRoot /Applications/MAMP/htdocs/site2/public

    <Directory /Applications/MAMP/htdocs/site2/public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

When I try to access "site2.local" in browser, I'm redirected to "site1.local". This will happen to all VirtualHost definitions that I insert below in the httpd.conf file. When I want to access "site2.local" or any other, I need to put its VirtualHost definition on the top (as the first one). So, I assume that Apache is only executing correctly the first VirtualHost.

Also, another problem: if I try to access any file of project in my htdocs folder which is not defined in VirtualHost, I will get a message "Forbidden, You don't have permission to access /project/ on this server." If I want to access any of these projects, I need to remove all VirtualHost definitions in httpd.conf file.

What I am doing wrong?

In my case, these (defined in VirtualHost) are PHP projects w/ Zend Framework. I'm using MacOSX.

Thank you

Best Answer

you are missing

NameVirtualHost *:8888

and

<VirtualHost _default_:8888>
DocumentRoot /www/default
</VirtualHost> 

For more information about the apache default virtual host, you can read this online doc

http://httpd.apache.org/docs/2.2/vhosts/examples.html
Related Topic