MAC OS X: MAMP环境和虚拟主机

6
我不确定这里发生了什么事情,但我正在MAC OS X上运行MAMP 1.9。在我的httpd.conf文件中,DocumentRoot设置为默认的htdocs路径。在htdocs文件夹中,我创建了许多“子站点”,以试图将它们设置为虚拟主机……我可以访问指定的路径,但它们似乎都默认为我设置的第一个。实际上,我配置的第一个VirtualHost是site1.localhost.com。之后,我配置了site2.localhost.com和site3.localhost.com。然而,访问最后两个地址时,总是会重定向到site1.localhost.com。此外,只访问localhost.com:8888也只会拉起site1.localhost.com。我不确定我犯了哪里的错误,但希望这里的某个人能帮助我解决问题……哦,而且,在对/etc/hosts或httpd.conf文件进行任何更改后,我已经重新启动了apache等。在我的httpd.conf文件中(至少是相关部分):
#                                                                                                                                                                                                                                                                             
# DocumentRoot: The directory out of which you will serve your                                                                                                                                                                                                                
# documents. By default, all requests are taken from this directory, but                                                                                                                                                                                                      
# symbolic links and aliases may be used to point to other locations.                                                                                                                                                                                                         
#                                                                                                                                                                                                                                                                             
# MAMP DOCUMENT_ROOT !! Don't remove this line !!                                                                                                                                                                                                                             


 DocumentRoot "/Applications/MAMP/htdocs"

#                                                                                                                                                                                                                                                                             
# Note that from this point forward you must specifically allow                                                                                                                                                                                                               
# particular features to be enabled - so if something's not working as                                                                                                                                                                                                        
# you might expect, make sure that you have specifically enabled it                                                                                                                                                                                                           
# below.                                                                                                                                                                                                                                                                      
#                                                                                                                                                                                                                                                                             

#                                                                                                                                                                                                                                                                             
# This should be changed to whatever you set DocumentRoot to.                                                                                                                                                                                                                 
#                                                                                                                                                                                                                                                                             
<Directory "/Applications/MAMP/htdocs">

<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/site1/"
ServerName site1.localhost.com
</VirtualHost>

<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/sub/site2/"
ServerName site2.localhost.com
</VirtualHost>

<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/sub/site3/"
ServerName site3.localhost.com
</VirtualHost>

在我的/etc/hosts文件中:

##                                                                                                                                                                                                                                                                            
# Host Database                                                                                                                                                                                                                                                               
#                                                                                                                                                                                                                                                                             
# localhost is used to configure the loopback interface                                                                                                                                                                                                                       
# when the system is booting.  Do not change this entry.                                                                                                                                                                                                                      
##                                                                                                                                                                                                                                                                            
127.0.0.1       localhost
127.0.0.1       site1.localhost.com
127.0.0.1       site2.localhost.com
127.0.0.1       site3.localhost.com
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost
3个回答

4
在您的httpd.conf文件中找到以下行并删除注释符号(#):
 # NameVirtualHost * 

0
除了MattLeff的回答之外,为了安全起见,您还应该添加一个服务器别名:
ServerAlias www.website.dev

如果你不这样做,而你的浏览器自动添加了“http://www”(并且隐藏它,嗷呜!),那么你的环境将自动默认为第一个虚拟主机。

0

我看到你已经得到了答案。但在我的情况下,这并不足够。当我添加了Chords的建议时,我才能让本地主机和许多其他虚拟主机(如demo.client.com)在我的MAMP上正常工作。只有当我将localhost作为虚拟主机添加到列表顶部时,它才能正常工作。

NameVirtualHost *:80
<VirtualHost *:80>
  DocumentRoot "/Applications/MAMP/htdocs"
  ServerName localhost
</VirtualHost>

<VirtualHost *:80>
  ServerAdmin me@email.com
  DocumentRoot "/Applications/MAMP/htdocs/clientA/"
  ServerName clientA.local
  ErrorLog "logs/clientA-local-error_log"
  CustomLog "logs/clientA-local-access_log" common
</VirtualHost>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接