解决方案:虚拟主机是设置在httpd-vhosts.conf还是vhosts.conf还是httpd.conf?

解决方案:虚拟主机是设置在httpd-vhosts.conf还是vhosts.conf还是httpd.conf?

答案是:都可以。但是推荐在vhosts.conf中设置。


——官方文档的举例是在httpd.conf中设置的。

——百度一下会发现99%都是在httpd-vhosts.conf中设置的。但是这种设置会存在一些问题,比如设置后localhost打不开等等问题,虽然解决方案简单,但是毕竟感觉不太保险。

——在vhosts.conf中设置的话,比较简单,而且没有什么幺蛾子问题。

<VirtualHost *:80>
	ServerName    localhost
	DocumentRoot  "E:\WWW"
	DirectoryIndex index.html index.php
	<Directory "E:\WWW">
		Options Indexes
		Order Allow,Deny
		Allow From All
	</Directory>
</VirtualHost>

<VirtualHost *:80>
	ServerName    www.hellocations.com
	DocumentRoot  "E:\hellocations"
	DirectoryIndex index.html index.php
	<Directory "E:\hellocations">
		Options Indexes
		Order Allow,Deny
		Allow From All
	</Directory>
</VirtualHost>

<VirtualHost *:80>
	ServerName    www.sina.com
	DocumentRoot  "E:\sina"
	DirectoryIndex index.html index.php
	<Directory "E:\sina">
		Options Indexes
		Order Allow,Deny
		Allow From All
	</Directory>
</VirtualHost>

当然,你需要在windows/system32/drivers/etc/hosts中设置几个域名和IP的映射:

127.0.0.1    www.hellocations.com
127.0.0.1    www.sina.com

(1)问题1:是不是需要在httpd.conf中打开包含虚拟主机设置文件的那条语句?不需要。用phpstudy配置集成环境的话,我们看httpd.conf的配置文件,如下,发现vhosts.conf文件已经默认被包含了,而htttpd-vhosts.conf默认没有包含,所以如果你执意要在httpd-vhosts.conf中设置虚拟主机的话,那么你需要把如下第二行的#去掉即可。

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

……
Include conf/vhosts.conf

# Secure (SSL/TLS) connections
#Include conf/extra/httpd-ssl.conf

(2)虚拟主机设置的格式以及注意事项?

——方法一:这个百度一下即可。

——方法二:查看官方文档,或者下载一个离线的apache手册。

——方法三:看配置文件中的示例。在httpd-vhosts.conf中有示例,如下。这也是为什么99%的人都在这个配置文件中配置的原因吧。不过apache的配置文件都是相通的。也就是说,有一个主配置文件httpd.conf,在其他地方还有很多分散的配置文件,当然这些分散的配置文件要想生效就需要在主配置文件中包含一下。思想和import或者include一样一样的。

# Add any other Virtual Hosts below

#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot "/Apache24/docs/dummy-host.example.com"
#    ServerName dummy-host.example.com
#    ServerAlias www.dummy-host.example.com
#    ErrorLog "logs/dummy-host.example.com-error.log"
#    CustomLog "logs/dummy-host.example.com-access.log" common
#</VirtualHost>

——设置格式的注意事项,需要说明的是一般除了设置serverName和DocumentRoot外,还需要对目录进行设置,即<Directory></Directory>的东西。否则会出现无权限访问之类的问题,其他参数诸如错误日志、用户访问信息、别名设置等等可以设置也可以省略。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值