php - In Apache Server,does making the virtualhost enabled mean the 'Mainhost' is disabled?
I'm configging my apache server. In httpd.conf, I set the DocumentRoot as below:
DocumentRoot "D:/phpwwwroot"
And I set the directory node as below:
<Directory "D:/phpwwwroot">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
For this moment, I haven't enabled the virtual host. And I visit the url:http://localhost:8080/
, everything is ok.
Next, I tried to set the virtual host.I uncommented out the Virtual hosts. As below:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
And my httpd-vhosts.conf is as below:
NameVirtualHost *:8080
<VirtualHost *:8080>
ServerName www.testphp1.com
DocumentRoot "D:\VirtualRoot"
DirectoryIndex index.shtml index.html index.htm index.php
<Directory "D:\VirtualRoot">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerName www.testphp2.com
DocumentRoot "D:\VirtualRoot1"
DirectoryIndex index.shtml index.html index.htm index.php
<Directory "D:\VirtualRoot1">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And I also edit the hosts file to correct the mapping, you should know what I mean:
127.0.0.1 www.testphp1.com
127.0.0.1 www.testphp2.com
Next restart apache server, visit below urls:
http://www.testphp1.com:8080/
http://www.testphp2.com:8080/
Everything is ok. They are mapped to the right folder.
A new question raised, When I try to visithttp://localhost:8080/
, it come to the first virtual host--www.testphp1.com. I means the page shows whatwww.testphp1.com
shows.
It seems that the "main host" doesn't work.
How does it happen? How to fix it
Answer
Solution:
It's same situation when you're using IP to access server. It'll show first active website that uses this IP (
localhost
is treated as IP here). You can fix it by defining main website first.Answer
Solution:
You need to add main host also such as: