虚拟主机 - 全部重定向到WAMP localhost'主页'

时间:2014-01-27 00:49:29

标签: php apache wamp virtualhost virtual-hosts

我在Windows 8 PC上设置了最新版本的WAMP - 我似乎无法使多个虚拟主机工作,我加载的每个本地URL都会显​​示WAMP主页。

任何人都可以解释我做错了吗?

// My hosts file
127.0.0.1   localhost   
127.0.0.1   client1.localhost
127.0.0.1   client2.localhost

我的WAMP目录中有两个文件夹,'client1'和'client2'显然每个文件夹都会关联client1&上面主机文件中的client2。

// My virtual hosts file
<VirtualHost *:80>
  ServerName localhost
  DocumentRoot "C:\wamp\www"
</VirtualHost>

<VirtualHost *:80>
  ServerName client1.localhost
  DocumentRoot "C:\wamp\www\client1"
<Directory "C:\wamp\www\client1">
  allow from all
  order allow,deny
  AllowOverride All
</Directory>
 DirectoryIndex index.html index.php
</VirtualHost>

<VirtualHost *:80>
  ServerName client2.localhost
  DocumentRoot "C:\wamp\www\client2"
<Directory "C:\wamp\www\client2">
  allow from all
  order allow,deny
  AllowOverride All
</Directory>
  DirectoryIndex index.html index.php
 </VirtualHost>

4 个答案:

答案 0 :(得分:10)

您的hosts文件看起来不错,但您的虚拟主机定义不太好。

如果您更改了hosts文件,则可以通过使用Runs as Administrator启动的命令窗口或简单重启来重新加载Windows缓存: -

net stop "DNS Client"

然后当完成时

net start "DNS Client"

需要引号,因为服务名称中有空格!!

DNS客户端服务缓存访问过的域名,并在启动时预先加载HOSTS文件中存在的域名,或者如上所述重新启动服务。

调试新的vhost定义时,请记住,如果您尝试访问的定义出现问题,Apache将始终默认使用vhost定义文件中定义的第一个vhost。所以如果你最终的那个,即WAMP主页,你可以假设你在定义那个vhost时犯了错误。

这也意味着如果你用Require local之类的东西定义第一个vhost定义,那么你的系统上也应该发送一个随机hack,如果它的安全性设置为Require local,那么hack应该会收到404错误可能会阻止进一步的黑客攻击。

// My virtual hosts file
<VirtualHost *:80>
  ServerName localhost
  DocumentRoot "C:\wamp\www"
  <Directory "C:\wamp\www">
     AllowOverride All
     # never want to allow access to your wamp home page area to anyone other than This PC
     # plus us the Apache 2.4.x syntax and not the 2.2 syntax
     Require local
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName client1.localhost
  DocumentRoot "C:\wamp\www\client1"
  <Directory "C:\wamp\www\client1">
     AllowOverride all
     # use Apache 2.4 syntax to all access to your internal network only
     Require ip 192.168.0
     # Or if you really want to give access to the whole internet uncomment this and comment the previous line
     #Require all granted
 </Directory>
 DirectoryIndex index.html index.php
</VirtualHost>

<VirtualHost *:80>
  ServerName client2.localhost
  DocumentRoot "C:\wamp\www\client2"
  <Directory "C:\wamp\www\client2">
     AllowOverride all
     # use Apache 2.4 syntax to all access to your internal network only
     Require ip 192.168.0
     # Or if you really want to give access to the whole internet uncomment this and comment the previous line
     #Require all granted
  </Directory>
  DirectoryIndex index.html index.php
 </VirtualHost>

如果您实际上不希望世界被允许访问这些客户端站点,但您确实希望能够从内部网络上的其他PC访问该站点,那么更好的访问机制将是使用{{ 1}}。请注意仅使用子网的前3个四分位数(您的子网可能不是192.168.0,但很多路由器默认使用。请先检查您的子网)

此外,如果您确实希望全世界都能看到这些客户端网站,那么您也需要Require ip 192.168.0路由器。

此外,如果您不打算向全世界提供对这些网站的访问权限,但只是遵循了不好的建议,那么对所有这些网站的更安全的定义就是使用Port Forward,这样您就可以从运行WAMP的PC上访问它们。

WAMPServer 2.4,我认为你的意思是当你说你正在运行最新版本的WAMPServer时,实际上改变了你可以包含vhost定义的方式。实际上,它包含了一种新的方式,并保持旧的方式。

因此,要包含vhost定义,您可以执行以下两项操作之一: -

1。 将您的vhost定义放入文件Require local httpd.conf文件中取消注释该行(它位于conf文件的底部附近。

\wamp\bin\apache\apache2.4.4\conf\extra\httpd-vhosts.conf and then in the

移除# Virtual hosts #Include conf/extra/httpd-vhosts.conf 行前面的#

Include

2。 将您的vhost定义放入# Virtual hosts Include conf/extra/httpd-vhosts.conf 文件夹中的“您喜欢的任何名称”文件中。

现在\wamp\vhost文件底部有一行说httpd.conf 这将包括该文件夹中的任何文件,如果它是vhost定义,它将应用于该配置。这是Apache 2.4的一个新命令,我相信只能在Apache 2.4.x安装上运行。

答案 1 :(得分:2)

尝试将您的客户端文件夹放在C:\ wamp \ www \下 C:\ wamp \ www \ client1和C:\ wamp \ www \ client2

修改

如果仍然无效,请尝试此操作 使用client1.dev而不是client1.localhost更改您的hosts文件,然后在您的Virtualhost设置中添加Servername

<VirtualHost *:80>
DocumentRoot "F:\www\client1"
ServerName client1.dev
ServerAlias client1.dev www.client1.dev
Options Indexes FollowSymLinks
<Directory "F:\www\client1">
    AllowOverride All
    Order Deny,Allow
</Directory>
</VirtualHost>

您必须重新启动才能在主机文件中进行更改

答案 2 :(得分:1)

首先,出于安全目的,您的目录结构永远不应位于www文件夹之上。

- c:\wamp\www (home)
    -c:\wamp\www\client1 (client1)
    -c:\wamp\www\client2 (client2)

其次在vhost中更改需要reload,但在您的情况下,您必须执行restart,因为wamp不提供reload

答案 3 :(得分:0)

我按照本指南操作,效果很好

我不得不改变路径......

<VirtualHost *:80>
 ServerAdmin emailaddress@domain.com
 DocumentRoot "c:\wamp\www\client1"
 ServerName client1.localhost
 ErrorLog "logs/client1.log"
 CustomLog "logs/client1-access.log" common
</VirtualHost>

除此之外..我发现对于使用WAMP的虚拟主机,这是一个很好的快速指南 http://www.techrepublic.com/blog/smb-technologist/create-virtual-hosts-in-a-wamp-server/#