环境
windows7 , wampserver集成环境 , thinkphp5.0文件
wamp目录:D:/wamp/www
thinkphp目录:F:/tp5
thinkphp入口文件:F:/tp5/public/index.php

第一步:修改http.conf文件,将http-vhosts.conf配置文件包含进来
点击wamp-apache-http.conf,用记事本打开,定位到此处。

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

注意:不要去掉第一个#号,否则wamp无法正常运行

第二步:配置http-vhosts.conf
点击wamp-apache-http-vhosts.conf,用记事本打开,全选粘贴以下内容。

    # VirtualHost example:  
    # Almost any Apache directive may go into a VirtualHost container.  
    # The first VirtualHost section is used for all requests that do not  
    # match a ServerName or ServerAlias in any <VirtualHost> block.  
    #  
    <VirtualHost *:80>  
        ServerAdmin webmaster@dummy-host.example.com  
        DocumentRoot "D:\wamp\www"  
        ServerName localhost  
        ServerAlias www.dummy-host.example.com  
        ErrorLog "logs/localhost-error.log"  
        CustomLog "logs/localhost-access.log" common  
    </VirtualHost>  
      
    <VirtualHost *:80>  
        ServerAdmin webmaster@dummy-host2.example.com  
        DocumentRoot "F:\tp5\public"  
        ServerName tp5.com
        ErrorLog "logs/tp5.com-error.log"  
        CustomLog "logs/tp5.com-access.log" common  
        <Directory />  
            Require all granted  
        </Directory>  
    </VirtualHost>  

在第一个例子中只改动了servername和documentRoot,这个配置保证了我们在开启vhost后仍然可以访问www目录。
在第二个例子中,除了修改serverName和documentRoot以外,还添加了Directory那句话,如果不添加这句话,当我们访问tp5.com的时候会报403权限的错误。

注意:

        DocumentRoot "F:\tp5\public"  #填写入口文件所在的文件夹
        ServerName tp5.com            #用tp5.com来访问这个网站入口文件

第三步:修改C:\Windows\System32\drivers\etc\hosts文件
在最后一行加入

127.0.0.1 tp5.com

最后重启wamp,在浏览器输入tp5.com
如果看到这样的页面就成功了:)
图片描述

一键操作
点击wamp主页的Tool栏目下的Add a Virtual Host,可以帮助你一键添加vhost
但是访问wamp主页时页面出现如下警告(暂不清楚原因):

The number of <Directory or </Directory> does not match the number of ServerName in d:/wamp/bin/apache/apache2.4.23/conf/extra/httpd-vhosts.conf file
The number of <VirtualHost does not match the number of ServerName in d:/wamp/bin/apache/apache2.4.23/conf/extra/httpd-vhosts.conf file

参考:wamp环境下配置vhost


1nFrastr
5 声望0 粉丝