DocumentRoot in vhosts.conf override global DocumentRoot in httpd.conf

5

I'm running Apache 2.4 on Yosemite

This is my /private/etc/apache2/httpd.conf

ServerName 127.0.0.1:80
DocumentRoot "/Library/WebServer/Documents/home_www/"
<Directory "/Library/WebServer/Documents/home_www">   
    Options Multiviews FollowSymLinks
    MultiviewsMatch Any
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

With this setting I can use http://127.0.0.1 and http://localhost in the web browser then it directs me to /Library/WebServer/Documents/home_www/index.html as usual

Then I added Include /private/etc/apache2/extra/httpd-vhosts.conf because I would like to use vhost on my machine

This is my /private/etc/apache2/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin jeud@hotmail.com
    ServerName tutor4dev.local 
    DocumentRoot "/Library/WebServer/Documents/home_www/xxx"
</VirtualHost>

I tried to use sudo apachectl -S to display vhost configuration, there are the result

VirtualHost configuration:
*:80                   xxx.local (/private/etc/apache2/extra/httpd-vhosts.conf:28)
ServerRoot: "/usr"
Main DocumentRoot: "/Library/WebServer/Documents/home_www/"
Main ErrorLog: "/private/var/log/apache2/error_log"
Mutex proxy: using_defaults
Mutex default: dir="/private/var/run/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
PidFile: "/private/var/run/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="_www" id=70
Group: name="_www" id=70

I can use http://xxx.local to access /Library/WebServer/Documents/home_www/xxx/index.html (vhost), but after adding vhost, http://127.0.0.1 and http://localhost also direct me to /Library/WebServer/Documents/home_www/xxx/index.html instead of /Library/WebServer/Documents/home_www/index.html

Please guide how to fix it Thanks

Artisan

Posted 2014-11-02T11:25:52.033

Reputation: 235

Answers

5

You have only one VirtualHost, and with * in it (<VirtualHost *:80>). Your main server will never answer any request. All requests will be handeld by the first virtual host configured, which is the default server in case of name based virtual hosts. You should create a new virtual host that must appear before all others in your config file(s) like:

# Main server, catches all requests that do not correspond to a ServerName/Alias
<VirtualHost *:80>
    ServerName 127.0.0.1
    DocumentRoot "/Library/WebServer/Documents/home_www/"
    ...
</VirtualHost>

More info in the apache doc

Zimmi

Posted 2014-11-02T11:25:52.033

Reputation: 341