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

发布于 2024-09-12 00:06:28 字数 6966 浏览 2 评论 0原文

我不确定这里发生了什么,但我在 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/ 进行任何更改后,我已经重新启动了 apache 等主机或 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

I'm not sure what's happening here, but I'm running MAMP 1.9 on MAC OS X. In my httpd.conf file, the DocumentRoot is set to the default htdocs path. Within the htdocs folder, I've created a number of "subsites" in an effort to set them up as Virtual Hosts.... I can get to the paths specified, but they all seem to default to the first one I set up.

Essentially, the first VirtualHost I configured was for site1.localhost.com. I've since configured site2.localhost.com and site3.localhost.com. However, going to either of the last two addresses, always seems to redirect me to site1.localhost.com. Additionally, going to just localhost.com:8888 also just pulls up site1.localhost.com. I'm not sure where I made a mis-step, but hopefully someone here will be able to help me figure out the problem... Oh, and I have restarted apache etc., after any changes made to either the /etc/hosts or httpd.conf file.

In my httpd.conf file (the relevant parts, anyway...):

#                                                                                                                                                                                                                                                                             
# 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>

In my /etc/hosts file:

##                                                                                                                                                                                                                                                                            
# 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

厌倦 2024-09-19 00:06:28

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

 # NameVirtualHost * 

In your httpd.conf file find the following line and remove the comment (#):

 # NameVirtualHost * 
心病无药医 2024-09-19 00:06:28

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

ServerAlias www.website.dev

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

In addition to MattLeff's answer, you should also add a server alias, just to be safe:

ServerAlias www.website.dev

If you don't and your browser automagically adds "http://www" (and hides it, rawr!) then your environment will automatically default to the first virtual host.

神爱温柔 2024-09-19 00:06:28

我可以看到你已经得到答案了。对我来说这还不够。当我添加 Chords 的建议时,我能够让 localhost + 许多其他虚拟主机 (demo.client.com) 在我的 MAMP 上工作。仅当我将本地主机添加为虚拟主机(位于列表顶部)时,它才起作用。

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

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

I can see you got your answer. It wasn't sufficient in my case. I was able to get the localhost + many other vhosts (demo.client.com) working on my MAMP when I added Chords's suggestion. It only worked when I added the localhost as a virtual host, on the top of the list.

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

<VirtualHost *:80>
  ServerAdmin [email protected]
  DocumentRoot "/Applications/MAMP/htdocs/clientA/"
  ServerName clientA.local
  ErrorLog "logs/clientA-local-error_log"
  CustomLog "logs/clientA-local-access_log" common
</VirtualHost>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文