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

Zab*_*abs 5 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
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

Rig*_*lly 10

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

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

net stop "DNS Client"
Run Code Online (Sandbox Code Playgroud)

然后当完成时

net start "DNS Client"
Run Code Online (Sandbox Code Playgroud)

由于服务名称中有空格,因此需要引用!!

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

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

这也意味着如果您定义第一个vhost定义与Require local您的系统上的随机黑客一样也应该在那里发送,如果它的安全设置为Require localhack应该收到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>
Run Code Online (Sandbox Code Playgroud)

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

此外,如果您确实希望世界看到这些客户端站点,那么您也必须使用Port Forward路由器.

此外,如果您不打算向全世界提供对这些站点的访问权限,但只是遵循不好的建议,那么将使用所有这些站点的更安全的定义,Require local因此您只能从运行WAMP的PC访问它们.

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

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

1.将您的vhost定义放入\wamp\bin\apache\apache2.4.4\conf\extra\httpd-vhosts.conf and then in thehttpd.conf文件中,取消注释该行(它位于conf文件的底部附近).

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
Run Code Online (Sandbox Code Playgroud)

删除#前面的Include线

# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Run Code Online (Sandbox Code Playgroud)

2.将您的vhost定义放入文件夹中的"您喜欢的任何名称" \wamp\vhost文件中.

httpd.conf现在文件底部有一行说明IncludeOptional "d:/wamp/vhosts/*" 这将包含该文件夹中的任何文件,如果它是vhost定义,它将应用于该配置.这是Apache 2.4的一个新命令我相信所以只能用于Apache 2.4.x安装.


Zab*_*abs 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>
Run Code Online (Sandbox Code Playgroud)

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