您最好将所有网站都设置为Virtual Hosts。它们始终可用,您无需更改 URL 以外的任何内容即可单独访问它们。
操作方法:在 WAMP 中创建虚拟主机
在执行任何此操作之前,请首先确保 Apache 和 MYSQL 正常工作!!!
在 wamp 目录结构之外创建一个新文件夹。例如
C:\websites\www
但这可以在运行 wamp 的 PC 可见的任何磁盘驱动器上
在 c:\websites 中为您要创建的每个站点创建一个子文件夹。例如:
C:\websites\www\site1
C:\websites\www\site2
编辑文件 C:\wamp\bin\apache\apachex.y.z\conf\extra\httpd-vhosts.conf 其中 x,y 和 z 是您实际安装的 apache 的版本号。
注意:如果您在 2 个或更多版本的 apache 之间切换,则必须依次对所有 apache 版本执行此操作。
建议:我喜欢使用 sitename.dev 格式来让我清楚地知道我正在处理我的本地主机开发网站副本,您可能更喜欢另一种表示法,没关系,在这种情况下,单词 dev 没有实际定义的含义,它只是我命名实时站点的开发版本的方式。
注意:删除或最好还是注释掉(使用第 1 列中的 # )此文件中已存在的行。它们只是示例。
示例内容:
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
## must be first so the the wamp menu page loads when you use just localhost as the domain name
## Also NEVER change the security to anything other than Allow from 127.0.0.1 localhost ::1
## Then a drive by Ip address hack should return a 403 denied access
<VirtualHost *:80>
DocumentRoot "C:/wamp/www"
ServerName localhost
ServerAlias localhost
**Using Apache 2.2 syntax**
<Directory "C:/wamp/www">
Order Deny,Allow
Deny from all
Allow from 127.0.0.1 localhost ::1
## For every ip in the subnet, just use the first 3 numbers of the subnet
## Check you actual subnet for the actual values to use here
Allow from 192.168.0
</Directory>
**Using Apache 2.4 syntax**
<Directory "C:/wamp/www">
Require local
## And possibly allow access from you local network
## Check you subnet for the actual values to use here
Require ip 192.168.0
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/websites/www/site1"
ServerName site1.dev
ServerAlias www.site1.dev
Options Indexes FollowSymLinks
**Using Apache 2.2 syntax**
<Directory "C:/websites/www/site1">
AllowOverride All
Order Deny,Allow
Deny from all
Allow from 127.0.0.1 localhost ::1
#If you want to allow access from your internal network
# For specific ip addresses add one line per ip address
#Allow from 192.168.0.nnn
# For every ip in the subnet, just use the first 3 numbers of the subnet
#Allow from 192.168.0
# If you want to allow access to everyone
#Allow from all
</Directory>
**Using Apache 2.4 syntax**
<Directory "C:/websites/www/site1">
AllowOverride All
Require local
#If you want to allow access from your internal network
# For specific ip addresses add one line per ip address
#Require ip 192.168.0.nnn
# For every ip in the subnet, just use the first 3 numbers of the subnet
#Require ip 192.168.0
# If you want to allow access to everyone
#Require all granted
</Directory>
</VirtualHost>
根据需要添加任意数量,以便您的每个站点都有一个,并根据需要更改 DocumentRoot、ServerName 和任何其他参数。
这还允许您对配置进行特定于站点的更改。
注意:这将使 wamp 管理器“上线”功能不再对这些新的虚拟主机站点产生任何影响,因为每个站点的安全性现在都是虚拟主机定义的一部分,所以离开 WAMP,离线。
如果您想将一个或多个站点上线,您必须在 httpd-vhosts.conf 文件中手动更改允许命令。
要检查您的子网,请执行以下操作:
启动命令窗口,然后运行
>ipconfig
在输出中查找“默认网关”行,并在允许命令中使用第三个数字。
编辑您的 httpd.conf 文件并搜索这些行,它们位于文件底部附近。
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
删除此行上的“#”注释字符以包含您新更改的虚拟主机,这将导致 apache 注册它们的存在。例如
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
在编辑你的 httpd.conf 文件时搜索
# onlineoffline tag - don't remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
不要更改这些行!
在 标记后添加以下内容以保护您的新 C:\websites 文件夹。
<Directory "C:/websites/">
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
这是为您的新目录结构设置安全性,以便仅允许从 127.0.0.1 (localhost) 访问这些新站点,除非从特定 VHOST 中进行修改。
附:您的内部网络可能是 192.168.x.y,请先检查您的系统!
现在,为了让您的浏览器知道如何访问这些新域名,即 site1.dev 和 site2.dev,我们需要告诉 Windows 它们所在的 IP 地址。有一个名为 HOSTS 的文件是域名服务器 (DNS) 发明之前的遗留问题。这是一种为 IP 地址提供易于记忆的好名字的方法,这当然是 DNS 服务器现在为我们所做的事情。
编辑您的 HOSTS 文件,该文件位于 C:\windows\system32\drivers\etc 中,该文件没有扩展名。
Windows 保护此文件,因此您必须是管理员才能保存对此文件的更改。
如果您使用的是 VISTA 或 Windows7/8,您可能认为您是管理员,但您不是!!!!
因此,要编辑此文件,您必须以特定方式启动编辑器或记事本以获得管理员权限。为此,请找到您的编辑器图标并使用以下按键启动它:
Shift + 右键单击其图标,这将显示一个菜单,单击“以管理员身份运行”项,然后在出现的挑战对话框中单击“允许”。
现在您已准备好编辑 hosts 文件,因此请将您的编辑器导航到 c:\windows\system32\drivers\etc\hosts
将以下行添加到该文件中
127.0.0.1 站点 1.dev
127.0.0.1 站点2.dev
注意:您需要在此文件中为每个新虚拟主机添加一行。
保存这些更改后,您需要让 Windows 刷新其“域名 - ipaddress 交叉引用”缓存。
为此,以管理员身份启动命令窗口(Shift + 左键单击命令窗口图标)并运行这 2 个命令。
>net stop "DNS Client"
>net start "DNS Client"
注意:引号是必需的,因为服务名称中有空格。
为了让 Apache 接受这些更改,您必须反弹(重新启动)apache。
通过以下方式执行此操作:Wamp 管理器 -> Apache -> 服务 -> 重新启动服务
您现在应该可以在浏览器中使用地址 site1.dev 访问您的新站点。
如果您已经有一个站点编码,则将您的站点代码复制到“C:/websites/xxxx”文件夹中,或者,
将一个快速简单的 index.php 文件放入“c:\websites\xxxx”文件夹以证明一切正常。
示例:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>SITE1</title>
</head>
<body>
<?php
echo '<div style="background-color:red;color;white;text-align:center;font-size:18px">HELLO FROM Site1</div>';
?>
</body>
</html>
疑难解答:
如果您使用了新域名 ( site1.dev ) 并且没有找到该站点。
一种。检查主机文件的更改。
湾。重新启动在 Windows 中运行的“DNS 服务”。这会缓存您在浏览器中使用的所有域名,这样浏览器就不必在每次重复使用域名时查询 DNS 服务器。这可能已经缓存了您失败的尝试,但重新启动很容易,应该可以解决问题,并且比重新启动 Windows 更快,这也应该可以工作。