---
title: "服务器apache下绑定域名到指定目录的方法"
author: "daweijsp"
date: "2010-09-27T22:51:10Z"
tags: ["Access", "Apache", "PHP", "Windows", "项目管理"]
categories: ["操作系统"]
slug: "apache"
draft: false
lastmod: "2010-09-27T22:51:10Z"
id: 773668
url: "https://daweijsp.iteye.com/blog/773668"
---

首先搞定dns,这个是关键...apache的虚拟主机要有dns的解析才能使用

Win9x中这个文件位于windows目录下，文件名可能是hosts.sam;Win2K及以后的操作系统中这个文件位于WINNT\\system32\\drivers\\etc 目录下，文件名为hosts。

打开就能看到

127.0.0.1 localhost

熟悉吧...这下知道localhost为什么也叫主机了吧

添加一个 test.com和test.myz

这样好象不能用book.test.myz或者news.test.myz...这个就要用dns了...

完成如下:

127.0.0.1 localhost

127.0.0.1 test.com

127.0.0.1 test.myz

\-----------------------------------------------------------------------

方法一：

下面就开始做虚拟主机

我的项目目录D:/www

在里面建立两个文件夹做测试

一个叫myz,一个叫myz2

在httpd.conf最后加上,如下....

NameVirtualHost \*:80

&lt;VirtualHost \*:80&gt;  
ServerName test.com   #要绑定的域名

DocumentRoot D:/www/myz    #虚拟主机目录（网站根目录）

&lt;/VirtualHost&gt;

&lt;VirtualHost \*:80&gt;

ServerAdmin XXXXX@XXXX.com #设置网站管理员邮箱

ServerName test.myz    #要绑定的域名

DocumentRoot D:/www/myz2   #虚拟主机目录（网站根目录）

&lt;/VirtualHost&gt;

别忘了重启apache

然后可以试了

放个index.html在test文件夹下

访问test.com OK

如果出现403,请查看apache中的目录权限设置

出现404,请查看上面写的documentroot是否正确,再看看文件夹里是否有文件....对了,也可以在conf/extra/httpd-vhosts.conf文件里搜索vhost，然后把前面的#去掉

然后去extra文件夹里找那个文件.在里面写VirtualHost

注意:写了VirtualHost后如果遇到没有的,就默认执行第一个VirtualHost

 

 

方法二：

在conf/extra/httpd-vhosts.conf文件里搜索vhost，然后把前面的#去掉

然后在conf/httpd.conf文件的最下面加入：

&lt;Directory D:/www&gt;这里的“D:/www”填自己的网站目录  
   Order allow,deny  
   Allow from all  
&lt;/Directory&gt;

&lt;VirtualHost \*:80&gt;  
    ServerAdmin XXXXX@XXXX.com #设置网站管理员邮箱  
    DocumentRoot D:/www/tz    #虚拟主机目录（网站根目录）  
    ServerName [www.test.com.cn](http://www.test.com.cn/)   #要绑定的域名  
    #RewriteEngine On  
    #RewriteRule ^/$ /index.php  
    #RewriteRule ^/(.\*)/$ /index.php  
    #RewriteRule ^/(.\*).php$ /index.php  
    ErrorLog logs/dummy-host2.appservnetwork.com-error\_log  
    CustomLog logs/dummy-host2.appservnetwork.com-access\_log common  
&lt;/VirtualHost&gt;