1

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

1 Answers1

0

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
c4f4t0r
  • 5,301
  • 3
  • 31
  • 42
  • You don't actually need a default virtualhost, unless you really want to... normally if Apache gets a request for an unknown servername it will simply serve the first loaded virtualhost. – stoned May 14 '14 at 07:56
  • @stoned how do you solve the problem? – c4f4t0r May 14 '14 at 17:45
  • It's not really a problem, sorry if it looked like that I meant so. I just pointed out that the _default_ virtualhost isn't mandatory/needed to have the resut that was asked for. – stoned May 14 '14 at 22:15
  • @stoned :) ok, but i would like to know, how can i have the result of that was asked for,without using the default virtual host – c4f4t0r May 15 '14 at 08:31
  • I don't really understand your question @c4f4t0r.. user1695700 only asked how to make virtualhosts when called by their own name, he didn't mention any need for choosing a special default virtualhost to be used. Essentially, the _default_ VirtualHost is optional. For the second part of the question, "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..." too little information is given.. would need to check paths, configuration files and urls but they were not provided. – stoned May 15 '14 at 12:43
  • @Stoned i know the default virtual host is optional, by question for you is, how can you resolve the problem without using the default virtualhost? – c4f4t0r May 15 '14 at 12:56
  • 1
    NameVirtualHost *:8888 (together with a Listen 8888 of course) should be enough to make everything work as asked in the first part of the question. Problem is, the guy seems not to be taking much care of this question and as such we can't assess if that was really the problem, or if it should be looked for elsewhere. – stoned May 15 '14 at 13:04
  • @stoned that was what i wanna know – c4f4t0r May 15 '14 at 13:15
  • complain with @user1695700 lol... after 2 days, I doubt he's following this page at all. – stoned May 16 '14 at 12:47