/ 中存储网

Apache本地部署配置多个虚拟主机网站站点

2013-12-06 17:16:01 来源:ITJS.CN

最近由于工作需要,在本地架设一个开发项目和自己测试项目的虚拟站点。具体操作分两种方案如下:

方案一

端口与htaccess文件的结合

优点:能在一个局域网或内网中多人访问

缺点:网站地址栏多了一个端口号

注意:必须开启httaccess功能。(httaccess功能怎么开启?)

步凑:

(1)找到你的apache安装目录,找到httpd.conf文件,搜索#listen这一句,在下面增加.

listen88

listen89

每个端口可以监听一个网站。

(2)httpd.conf文件后面加上你要解析文件地址,如:

#启用htaccess文件. 注意:linux为:AccessFileName .htaccess在htaccess文件前面加了一个点.

AccessFileName htaccess

NameVirtualHost *:80

#默认端口

<VirtualHost *:80>

DocumentRoot "D:www"

ServerName127.0.0.1:80

DirectoryIndex index.html index.htm index.php index.shtml

<Directory "D:www">

Options Indexes FollowSymLinks

AllowOverride ALL

Order allow,deny

Allow from all

</Directory>

</VirtualHost>

#88端口网站

<VirtualHost 192.168.1.221:88>

DocumentRoot "D:www88"

ServerName192.168.1.221:88

DirectoryIndex index.html index.htm index.php index.shtml

<Directory "D:www88">

Options Indexes FollowSymLinks

AllowOverride ALL

Order allow,deny

Allow from all

</Directory>

</VirtualHost>

#89端口网站

<VirtualHost 192.168.1.221:89>

DocumentRoot "D:www89"

ServerName192.168.1.221:89

DirectoryIndex index.html index.htm index.php index.shtml

<Directory "D:www88">

Options Indexes FollowSymLinks

AllowOverride ALL

Order allow,deny

Allow from all

</Directory>

</VirtualHost>

(3)如何访问:http://192.168.1.221/访问是www里面的内容http://192.168.1.221:88/访问的是www88文件夹里面的内容.....

方案二

简单的说,打开httpd.conf 在最后加入如下内容:

<VirtualHost 127.0.0.2:80>

DocumentRoot d:/AppServ/wwwroot

ServerName 127.0.0.2:80

< irtualHost>

<Directory "d:/AppServ/wwwroot">

Options Indexes FollowSymLinks Multiviews

AllowOverride All

Order Allow,Deny

Allow from all

</Directory>

"d:/AppServ/wwwroot" 为你的站点存放目录:重启apache2以后,你的虚拟主机就配置好了,以后就可以通过127.0.0.2,和127.0.0.3进入不同的站点了。

下面为详细说明分析:

在我们安装APACHE的时候一般默认的apache的配置是只有一个网站,这样切换起来很不方便。其实这个问题很好解决,就是把本机的apache配置成为虚拟服务器。但是,网上大多数教程的是教用 apache如何配置基于域名的虚拟主机的,而在本机调试网站的时候,一般都是用本地ip(127.0.0.1 或 localhost)直接访问,没有用到域名。所以得把apache配置成为基于ip地址的虚拟主机。

首先,我们都知道,所有以127打头的ip地址都应该指向本机,并不只有127.0.0.1,这点大家可以试试。

这样一来,也就是说本机有足够多的ip地址供你来开设虚拟主机了。

废话少说,进入正式的配置工作,下面是apache的httpd.conf里相关配置部分( httpd.conf 位于 Apache2.2conf ):

1、Listen部分,必须直接指定端口,不指定ip地址,配置应写为:

Listen 80

2、不用像基于域名的虚拟主机那样写“NameVirtualHost”。

3、虚拟主机配置段:在httpd.conf 最后加上

<VirtualHost 127.0.0.2:80>

DocumentRoot d:/AppServ/wwwroot

ServerName 127.0.0.2:80

< irtualHost>

<VirtualHost 127.0.0.3:80>

DocumentRoot d:/AppServ/www3

ServerName 127.0.0.3:80

< irtualHost>...

4、然后相应的配置好各个目录属性,下面是一个目录属性的典型配置:

<Directory "d:/AppServ/www2">

Options Indexes FollowSymLinks Multiviews

AllowOverride All

Order Allow,Deny

Allow from all

</Directory>

<Directory "d:/AppServ/www3">

Options Indexes FollowSymLinks Multiviews

AllowOverride All

Order Allow,Deny

Allow from all

</Directory>

重启apache2以后,你的虚拟主机就配置好了,以后就可以通过127.0.0.1和127.0.0.2,127.0.0.3进入不同的站点了。

备注:有问题留言。

关于如何使用htaccess和深入使用htaccess参考如下扩展阅读:

htaccess启用

htaccess配置详解

Rewrite实现URL的跳转和域名跳转

网站首页重定向