WAMP环境下配置Virtual hosts
twocode

    使用本地开发时,一开始都会将项目放在一个公共目录下即DocumentRoot。但这样做随着项目的增多,就会产生小小问题,,管理起来显得不直观,麻烦。为了方便管理和测试,可以配置虚拟的本地网址;

    那该如何配置达到这个效果?
  

     一、本机DNS解析实现
    修改windows的hosts
    C:/WINDOWS/system32/drivers/etc/hosts
    127.0.0.1      localhost
    127.0.0.1     test

    二、注释掉配置文件一些语句

    #apache/conf/httpd.conf
    #ServerAdmin admin@localhost
    #ServerName localhost:80
    #DocumentRoot "E:/www/"  

    三、开启配置文件项:

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


    四 、修改配置文件

    #httpd-vhosts.conf
    #修改这个文件即可,避免把httpd.conf文件改得乱糟糟

    #配置a网址的示例
    #http://a

    <Directory "E:/www/web/a"> #对应路径是你的php解析环境目录
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    <VirtualHost *:80>
        DocumentRoot  E:/www/web/a
        ServerName a
    </VirtualHost>


    #配置b网址的示例
    #http://b

    <Directory "E:/www/web/b"> #对应路径是你的php解析环境目录
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    <VirtualHost *:80>
        DocumentRoot  E:/www/web/b
        ServerName b
    </VirtualHost>  


    #配置localhost路径
    #http://localhost
    <VirtualHost *:80>
        DocumentRoot D:/xampp170/htdocs
        ServerName localhost
    </VirtualHost>


    或者可以这样配置

    NameVirtualHost *:80

    #

    # 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-host2.cjb21sj.com

       DocumentRoot "E:/www/"

       ServerName *

       ErrorLog "logs/dummy-host2.cjb21sj.com-error.log"

       CustomLog "logs/dummy-host2.cjb21sj.com-access.log" common

    </VirtualHost>


    <VirtualHost *:80>  

        DocumentRoot E:/www/a/

        ServerName www.a.com

        <Directory "E:/www/a/">  

            Options Indexes FollowSymLinks Includes ExecCGI  

            AllowOverride All  

            Order allow,deny  

            Allow from all  

        </Directory>  

    </VirtualHost> 


    <VirtualHost *:80>  

        DocumentRoot E:/www/b

        ServerName www.b.com  

        <Directory "E:/www/b/">  

            Options Indexes FollowSymLinks Includes ExecCGI  

            AllowOverride All  

            Order allow,deny  

            Allow from all  

        </Directory>  

    </VirtualHost>


    五.配置完成后,别忘了重启apache

    启动、停止和重新启动Apache服务的方法通常是使用Apache Service Monitor工具,另外也可以使用控制台命令:
    NET START|STOP Apache2.2
    或者使用Windows控制面板下的服务
    C:\Apache2.2\bin> httpd  -k  start |stop|restart

网友评论已关闭