Apache中配置多主机多站点,可以通过两种方式实现:

  • 将同一个域名的不同端口映射到不同的虚拟主机,不同端口映射到不同的站点;
  • 将同一个端口映射成不同的域名,不同的域名映射到不同的站点。

我们只需要修改相应的配置文件即可。

一、准备工作

1、修改系统 hosts文件

我是在我自己的电脑上操作的,我的系统是win7,所以先修改win7系统的hosts文件,增加两个域名映射,以便后面备用。hosts文件的路径是:C:/Windows/System32/drivers/etc/hosts,在这个文件中我们加入如下两行代码:

127.0.0.1 www.liuyazhuang.com
127.0.0.1 www.lyz.com

这两行代码的意思是将www.liuyazhuang.com和www.lyz.com映射到本机的ip地址上。

2、准备两个站点

在d:/Apache目录下分别准备两个站点myweb1和myweb2,这两个站点下分别有一个index.html文件,myweb1下index.html内容是"我是第一个站点",myweb2站点下的index.html内容是"我是第二个站点"。

具体如下图所示:

好了,到此,站点准备完毕

二、具体实现

我们分别讲解两种不同的实现方案

1、修改Apache的httpd.conf文件

1)修改监听端口

在Apache的httpd.conf中,将监听端口修改为8080和8090,具体如下图所示:

2)注释默认的访问路径

在httpd.conf文件中找到如下代码:

DocumentRoot "D:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs"

将这行代码注释掉,如下图:

3)打开引入httpd-vhosts.conf文件的注释

在httpd.conf文件中找到如下代码:

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

将#Include conf/extra/httpd-vhosts.conf这行代码的注释打开,如下图:

2、修改Apache的httpd-vhosts.conf文件

我们说到的方案一和方案二,只是在配置httpd-vhosts.conf文件的时候有所不同。

方案一、

将同一个域名的不同端口映射到不同的虚拟主机,不同端口映射到不同的站点

在httpd-vhosts.conf文件中添加如下代码:

#配置我们自己的虚拟主机
<VirtualHost www.lyz.com:8080>
#配置访问跟目录
DocumentRoot "d:/Apache/myweb1"
#这里配置欢迎首页面
DirectoryIndex index.html index.htm index.php
<Directory />
Options FollowSymLinks
#不允许别人修改我们的页面
AllowOverride All
#设置访问权限
Order allow,deny
Allow from All
</Directory>
</VirtualHost> #配置我们自己的虚拟主机
<VirtualHost www.lyz.com:8090>
#配置访问跟目录
DocumentRoot "d:/Apache/myweb2"
#这里配置欢迎首页面
DirectoryIndex index.html index.htm index.php
<Directory />
Options FollowSymLinks
#不允许别人修改我们的页面
AllowOverride All
#设置访问权限
Order allow,deny
Allow from All
</Directory>
</VirtualHost>

具体如下图所示:

启动Apache在浏览器中输入:http://www.lyz.com:8080显示"我是第一个站点",输入:http://www.lyz.com:8090显示"我是第二个站点"。

方案二、

将同一个端口映射成不同的域名,不同的域名映射到不同的站点。

在httpd-vhosts.conf文件中添加如下代码:

#配置我们自己的虚拟主机
<VirtualHost *:8080>
#配置访问跟目录
DocumentRoot "d:/Apache/myweb1"
ServerName www.lyz.com
#这里配置欢迎首页面
DirectoryIndex news.html index.html index.htm index.php
<Directory />
Options FollowSymLinks
#不允许别人修改我们的页面
AllowOverride All
#设置访问权限
Order allow,deny
Allow from all
</Directory>
</VirtualHost> #配置我们自己的虚拟主机
<VirtualHost *:8080>
#配置访问跟目录
DocumentRoot "d:/Apache/myweb2"
ServerName www.liuyazhuang.com
#这里配置欢迎首页面
DirectoryIndex news.html index.html index.htm index.php
<Directory />
Options FollowSymLinks
#不允许别人修改我们的页面
AllowOverride All
#设置访问权限
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

具体如下图所示:

启动Apache在浏览器中输入:http://www.lyz.com:8080显示"我是第一个站点",输入:http://www.liuyazhuang.com:8080显示"我是第二个站点"。

最新文章

  1. elastichq 离线安装
  2. Python~删除空格,插入换行符号
  3. tomcat常见错误及解决方案
  4. Python开发专业工具推荐
  5. Oracle自增列
  6. readlink
  7. Jekyll搭建过程详解
  8. H5 拖放
  9. 运行期以索引获取tuple元素-C++11之1
  10. mina statemachine解读(二)
  11. Python VisibleDeprecationWarning: converting an array with ndim &gt; 0 to an index will result in an error in the future
  12. Breakout 打砖块
  13. 如何用Python编写一个聊天室
  14. [费用流][BZOJ1070]修车
  15. php 微信支付V3 APP支付
  16. JavaScript for...in 循环
  17. 初识python函数
  18. linux批量关闭进程
  19. iOS开发- UILabel 自己主动换行 及 高度自适应
  20. colemak,你用了吗?

热门文章

  1. selenium3+python3自动化测试学习之网页元素定位
  2. Docker安装nacos1.0.0固定ip单机模式
  3. python多线程爬取图片实例
  4. Qt之股票组件-自选股--列表可以拖拽、右键常用菜单
  5. 并发编程-concurrent指南-原子操作类-AtomicInteger
  6. ZooKeeper入门(四) Zookeeper监视(Watches)
  7. async与await详解
  8. 分布式事务(3)---RocketMQ实现分布式事务原理
  9. junit中test用法
  10. python 写入excel数据而不改变excel原有样式