Tomcat定义虚拟主机案例

时间:2022-10-21 15:31:44

                Tomcat定义虚拟主机案例

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.准备环境

1>.创建web程序的根目录

[root@yinzhengjie ~]# mkdir -pv /home/yinzhengjie/data/www/webapps/ROOT
mkdir: created directory ‘/home/yinzhengjie/data/www’
mkdir: created directory ‘/home/yinzhengjie/data/www/webapps’
mkdir: created directory ‘/home/yinzhengjie/data/www/webapps/ROOT’
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# mkdir -pv /home/yinzhengjie/data/www/webapps/test
mkdir: created directory ‘/home/yinzhengjie/data/www/webapps/test’
[root@yinzhengjie ~]#

2>.编辑根目录的首页信息

[root@yinzhengjie ~]# more /home/yinzhengjie/data/www/webapps/ROOT/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP test page.</title>
</head>
<body>
<% out.println("Yinzhengjie ! Hello,world!"); %>
</body>
</html>
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# more /home/yinzhengjie/data/www/webapps/test/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP test page.</title>
</head>
<body>
<% out.println("www.yinzhengjie.org.cn"); %>
</body>
</html>
[root@yinzhengjie ~]#

3>.windows的hosts配置信息如下:

Tomcat定义虚拟主机案例

4>.tomcat服务器的配置信息如下

[root@yinzhengjie ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.30.1.101 s101
172.30.1.101 www.yinzhengjie.org.cn
172.30.1.102 s102
172.30.1.103 s103
172.30.1.104 s104
172.30.1.105 s105
[root@yinzhengjie ~]#

二.编辑Server.xml配置文件

 1>.备份配置文件

[root@yinzhengjie ~]# cp /usr/local/tomcat/conf/server.xml /usr/local/tomcat/conf/server.xml.bak
[root@yinzhengjie ~]#

2>.编辑Server.xml配置文件,添加虚拟主机信息

      <Host name="www.yinzhengjie.org.cn"  appBase="/home/yinzhengjie/data/www/webapps" unpackWARs="true" autoDeploy="true">
<context path="" docBase="ROOT" reloadable="true" />
<context path="/test" docBase="test" reloadable="true" /> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="yinzhengjie.org.cn_access_log" suffix=".log"
pattern="%h %l %u %t &quot;%r&quot; %s %b" /> </Host>

Tomcat定义虚拟主机案例

3>.重启tomcat服务

[root@yinzhengjie ~]# catalina.sh stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /soft/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@yinzhengjie ~]#
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /soft/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@yinzhengjie ~]#
[root@yinzhengjie ~]#

4>.查看webUI信息如下

Tomcat定义虚拟主机案例

5>.查看test页面

Tomcat定义虚拟主机案例