php 虚拟主机 - 全部重定向到 WAMP 本地主机“主页”页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21371349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:05:13  来源:igfitidea点击:

Virtual Hosts - all redirect to the WAMP localhost 'home' page

phpapachewampvirtualhostvirtual-hosts

提问by Zabs

I've setup the latest version of WAMP on my Windows 8 PC - I cannot seem to get multiple virtual hosts to work, every local URL I load will show the WAMP homepage.

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

Can anyone explain what I am doing wrong?

谁能解释我做错了什么?

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

I have two folders in my WAMP directory, 'client1' and 'client2' obviously each folder will relate the the client1 & client2 in the host file above.

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

采纳答案by Zabs

I followed this guide and it worked fine

我遵循了本指南,效果很好

I had to change the path...

我不得不改变路径...

<VirtualHost *:80>
 ServerAdmin [email protected]
 DocumentRoot "c:\wamp\www\client1"
 ServerName client1.localhost
 ErrorLog "logs/client1.log"
 CustomLog "logs/client1-access.log" common
</VirtualHost>

In addition to this.. I find this is a good quick guide for virtual hosts with WAMP http://www.techrepublic.com/blog/smb-technologist/create-virtual-hosts-in-a-wamp-server/#

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

回答by RiggsFolly

Your hosts file looks good, but your virtual hosts definitions are not so good.

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

If you change your hostsfile you can reload the windows cache by doing this from a command window that has been launched using Runs as Administratoror a simple reboot :-

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

net stop "DNS Client"

then when that completes do

然后当完成时

net start "DNS Client"

Quotes required as there is a space in the service name!!

需要引用,因为服务名称中有一个空格!!

The DNS Client service caches domain names accessed and is pre loaded with domain names that exists in the HOSTSfile at boot time or if you restart the service as above.

DNS 客户端服务会缓存访问的域名,并预先加载HOSTS文件中存在的域名,这些域名在启动时或如果您按上述方式重新启动服务。

When debugging a new vhost definition, remember, that if something is wrong with a definition you are attempting to access, Apache will always default to the first vhost defined in the vhost definition file. So if thats where you end up i.e. the WAMP homepage, you can assume you made a mistake defining that vhost.

在调试新的虚拟主机定义时,请记住,如果您尝试访问的定义有问题,Apache 将始终默认为虚拟主机定义文件中定义的第一个虚拟主机。因此,如果那是您最终到达 WAMP 主页的位置,您可以假设您在定义该虚拟主机时犯了一个错误。

This also means that if you define that first vhost definition with something like Require locala random hack on your system should also be sent there, if that has its security set to Require localthe hack should receive a 404 error which might discourage further hack attempts.

这也意味着,如果您定义第一个 vhost 定义,并Require local在您的系统上进行诸如随机黑客之类的内容,则也应将其发送到那里,如果其安全设置为Require local黑客,则应收到 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>

If you don't actually want the world to be allowed access to these client sites, but you do want to be able to access the site from other PC's on your internal network then a better access mechanism would be to use Require ip 192.168.0. Note the use of just the first 3 quartiles of your subnet ( yours may not be 192.168.0, but a lot of routers default to that. Check your subnet first )

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

Also if you do want the world to see these client sites then you have to Port Forwardyour router as well.

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

Also if you didn't intend to give access to these site to the whole world, but were just following bad advice, a much more secure definition of all these sites would be to use Require localso you can only access them from the PC running WAMP.

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

WAMPServer 2.4, which I assume you mean when you say you are running the latest release of WAMPServer actually changed the the way that you can include vhost definitions. Well actually it included a new way and kept the old way as well.

WAMPServer 2.4,我假设您说您正在运行最新版本的 WAMPServer 时的意思实际上改变了您可以包含 vhost 定义的方式。好吧,实际上它包含了一种新方法,也保留了旧方法。

So to include the vhost definition you can do one of these 2 things :-

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

1. Put your vhost definitions into the file \wamp\bin\apache\apache2.4.4\conf\extra\httpd-vhosts.conf and then in thehttpd.conf file uncomment this line ( its near the bottom of the conf file.

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

Remove the #infront of the Includeline

删除线的#前面Include

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

2. Place your vhost definitions into a file 'called anything you like' into the \wamp\vhostfolder.

2. 将您的虚拟主机定义放入文件夹中“您喜欢的任何名称”的\wamp\vhost文件中。

There is a line at the bottom of the httpd.conffile now that say IncludeOptional "d:/wamp/vhosts/*"This will include any file in that folder and if it is a vhost definition it will apply that to the config. This is a new command to Apache 2.4 I believe so will only work on Apache 2.4.x installs.

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

回答by Ohgodwhy

Firstly, your directory structure should never go above the wwwfolder for security purposes.

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

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

Secondly changed in the vhost require a reload, but in your case, you must do a restartas wampdoesn't offer reload.

其次在 vhost require a 中更改reload,但在您的情况下,您必须执行 a restartas wampdoes not offer reload

回答by Andreas P.

Try to put your client folders under C:\wamp\www\ like this C:\wamp\www\ client1 and C:\wamp\www\client2

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

EDIT

编辑

If still doesn't work try this change your hosts file with client1.dev instead of client1.localhost and then put a Servername in you Virtualhost settings like this

如果仍然不起作用,请尝试使用 client1.dev 而不是 client1.localhost 更改您的主机文件,然后在您的 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>

You have to restart again to make the changes in hosts file work

您必须再次重新启动才能使主机文件中的更改生效