【问题标题】:MAC OS X: MAMP Environment & Virtual HostsMAC OS X:MAMP 环境和虚拟主机
【发布时间】:2011-03-22 19:00:25
【问题描述】:

我不确定这里发生了什么,但我在 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。我不确定我在哪里做错了,但希望这里的人能够帮助我找出问题所在......哦,我已经重新启动了 apache 等,在对 /etc/ 进行任何更改之后主机或 httpd.conf 文件。

在我的 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

【问题讨论】:

    标签: macos mamp virtualhost


    【解决方案1】:

    我可以看到你得到了答案。就我而言,这还不够。当我添加 Chords 的建议时,我能够让 localhost + 许多其他虚拟主机(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>
    

    【讨论】:

      【解决方案2】:

      除了 MattLeff 的回答,您还应该添加一个服务器别名,为了安全起见:

      ServerAlias www.website.dev
      

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

      【讨论】:

        【解决方案3】:

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

         # NameVirtualHost * 
        

        【讨论】: