现在的位置: 首页服务器技术>正文
在Tomcat上配置虚拟主机的方法
2007年12月11日 服务器技术 暂无评论 ⁄ 被围观 阅读(1,103)+

一 准备
我们将配置两台虚拟主机,假设域名分别为
bbs.adminfx.cn
blog.adminfx.cn

为了测试方便,请在客户机的:
Win:\\WINNT\\system32\\drivers\\etc\\hosts
Linux:/etc/hosts

文件中增加下面内容,然后检查一下这两个域名是否解析正确。
192.168.0.1 bbs.adminfx.cn
192.168.0.1 blog.adminfx.cn

当然,在生产环境中这样做是不行的,需要的在 DNS 上做相应的域名解析。

二 Tomcat安装
Tomcat 的安装不在本文的讨论范围,请参考这里。请确保Tomcat安装正确,不然请不要继续进行下面的配置步骤。

将 tomcat 目录下的 webapps 目录在同一目录复制一份,目录名分为 webapps2 ,然后将webapps 目录改名 为 webapps1 。最后 tomcat 的目录结构大致如下:
tomcat
|--bin
|--common
|--conf
|--logs
|--server
|--shared
......
|--webpapps1
|--webpapps2
|--work

最后,写一个简单 html 文件用于测试,文件名为 test.html ,文件内容如下:

<HTML>
<HEAD>
<TITLE>测试</TITLE>
</HEAD>

<BODY>
<P align="center">你现在访问的是 <FONT COLOR="#FF0000">bbs.adminfx.cn</FONT></P>
</BODY>
</HTML>
将 test.html 文件分别在
tomcat/webapps1/ROOT、tomcat/webapps2/ROOT 目录放置一份,然后将
tomcat/webapps2/ROOT/test.html
文件内容中"bbs.adminfx.cn"改为"blog.adminfx.cn"。

至此,前期的准备工作做完了,全是一些体力活。

三 配置虚拟主机

配置 bbs.adminfx.cn 虚拟主机

打开 tomcat/conf/server.xml 文件,将 Host 元素之间的内容全部删掉,然后把下面内容加如到 Host 元素 原来的位置。
<Host name="bbs.adminfx.cn" debug="0" appBase="webapps1"
unpackWARs="true" autoDeploy="true">

<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="bbs_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>

<Logger className="org.apache.catalina.logger.FileLogger"
directory="logs" prefix="bbs_log." suffix=".txt"
timestamp="true"/>

</Host>
配置 blog.adminfx.cn 虚拟主机

将下面内容追加到 Host 元素后面,注意 Host 元素中 name 属性和 appBase 属性的值的变化。

<Host name="blog.adminfx.cn" debug="0" appBase="webapps2"
unpackWARs="true" autoDeploy="true">

<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="blog_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>

<Logger className="org.apache.catalina.logger.FileLogger"
directory="logs" prefix="blog_log." suffix=".txt"
timestamp="true"/>

</Host>
现在可以启动 Tomcat 了,分别访问
http://bbs.adminfx.cn:8080/test.html
http://blog.adminfx.cn:8080/test.html
如果访问得到的页面内容分别是下面的内容,那表明虚拟主机已经配置成功了。否则,请检查你的配置过程并
重新按照文档配置。
你现在访问的是 bbs.adminfx.cn
你现在访问的是 blog.adminfx.cn

本文来自: 站长分享站(www.AdminFX.cn) 详细出处参考:http://www.adminfx.cn/article/jishu/Server/20071211/936.html

给我留言

留言无头像?


[face=9] [face=8] [face=7] [face=6] [face=5] [face=4] [face=3] [face=30] [face=2] [face=29] [face=28] [face=27] [face=26] [face=25] [face=24] [face=23] [face=22] [face=21] [face=20] [face=1] [face=19] [face=18] [face=17] [face=16] [face=15] [face=14] [face=13] [face=12] [face=11] [face=10] [em=9] [em=8] [em=7] [em=6] [em=5] [em=4] [em=3] [em=30] [em=2] [em=29] [em=28] [em=27] [em=26] [em=25] [em=24] [em=23] [em=22] [em=21] [em=20] [em=1] [em=19] [em=18] [em=17] [em=16] [em=15] [em=14] [em=13] [em=12] [em=11] [em=10] ;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)