与Apache 2.4在同一端口上的几个虚拟主机

时间:2016-01-25 20:41:25

标签: php apache wamp virtualhost httpd.conf

我试图在Apache 2.4中使用虚拟主机做一些简单的事情(使用Wampserver 2.5)

我希望能够拥有多个虚拟主机并通过以下方式访问它们:

  • www.project1.dev
  • www.project2.dev

所以我通过阅读official guide

httpd.conf中进行了以下配置
NameVirtualHost \*:80

#
# Project 1
#

<VirtualHost *:80>
    DocumentRoot "C:/wamp/www/myProject1/"
    ServerName www.project1.dev
    ErrorLog "logs/project1-error_log "
    CustomLog "logs/project1-access_log" common
    <Directory "C:/wamp/www/myProject1/">
      AllowOverride all
      Require all granted
    </Directory>
</VirtualHost>

#
# Project 2
#

<VirtualHost *:80>
    DocumentRoot "C:/wamp/www/myProject2/"
    ServerName www.project2.dev
    ErrorLog "logs/project2-error_log "
    CustomLog "logs/project2-access_log" common
    <Directory "C:/wamp/www/myProject2/">
      AllowOverride all
      Require all granted
    </Directory>
</VirtualHost>

我还将它们添加到我的hosts文件

127.0.0.1 http://project1.dev
127.0.0.1 http://project2.dev

但是,在我重启wamp服务后进行测试时,http://project1.devhttp://project2.dev都指向C:/wamp/www/myProject1/

C:/wamp/www/myProject2/相关的第二条路径project2.dev似乎被忽略了。

我错过了什么吗?

感谢。

2 个答案:

答案 0 :(得分:1)

您尚未在任何地方定义http://project2.dev - 仅http://www.project2.dev。所以Apache默认返回第一个配置,因为它无法找到匹配。

将以下配置添加到第一个vhost:

ServerAlias project1.dev

和第二种类似:

ServerAlias project2.dev

然后重启Apache。

答案 1 :(得分:1)

简单的答案是,从Apache 2.4开始,不再需要或允许NameVirtualHost *:80语法!它会抛出错误并导致Apache无法启动。如果你找到它,错误将出现在apache error log

哦,NameVirtualHost \*:80永远不对!

所以只需完全删除行NameVirtualHost \*:80

一旦定义了虚拟主机,httpd.conf中的localhost就会被忽略,所以你应该在httpd-vhost.conf中定义它,否则其他2个VH def看起来很好,除了{{{ 1}}并且缺少ServerName和缺少的ServerAlias

Options

引用:但是当我在重启wamp服务后进行测试时,http://project1.devhttp://project2.dev都指向C:/ wamp / www / myProject1 /

如果您的VH定义中存在错误,Apache将默认为httpd-vhosts.conf中第一个正确定义的VH,这就是可能发生的事情

对于你的<VirtualHost *:80> DocumentRoot "C:/wamp/www/" ServerName localhost <Directory "C:/wamp/www/"> Options Indexes FollowSymLinks MultiViews AllowOverride all Require local </Directory> </VirtualHost> # # Project 1 # <VirtualHost *:80> DocumentRoot "C:/wamp/www/myProject1/" ServerName project1.dev ServerAlias www.project1.dev ErrorLog "logs/project1-error_log " CustomLog "logs/project1-access_log" common <Directory "C:/wamp/www/myProject1/"> AllowOverride all Options Indexes FollowSymLinks MultiViews Require all granted </Directory> </VirtualHost> # # Project 2 # <VirtualHost *:80> DocumentRoot "C:/wamp/www/myProject2/" ServerName project2.dev ErrorLog "logs/project2-error_log " CustomLog "logs/project2-access_log" common <Directory "C:/wamp/www/myProject2/"> AllowOverride all Options Indexes FollowSymLinks MultiViews Require all granted </Directory> </VirtualHost> 文件,它应该包含对IPV4和IPV6堆栈的引用,特别是因为windows似乎越来越多地使用IPV6堆栈,最后我们要移动的地方,最终,它不应该包括HOSTS所以它应该是这样的: -

httpd://

更改HOSTS文件后,您需要重启,或者在“以管理员身份运行”的命令窗口启动中执行以下操作

#IPV4 stack ip addresses
127.0.0.1 localhost
127.0.0.1 project1.dev
127.0.0.1 project2.dev

#IPV6 stack ip addresses
::1 localhost
::1 project1.dev
::1 project2.dev