本文共 4911 字,大约阅读时间需要 16 分钟。
1、安装Tomcat
在/usr/local下部署两个Tomcat,tomcat的文件夹重命名为:tomcat1和tomcat2:
# chown xjweb:xjwebapache-tomcat-7.0.55.tar.gz
--部署tomcat1
# tar -zxvf apache-tomcat-7.0.55.tar.gz
--重命名
# mv -f apache-tomcat-7.0.55 tomcat1
# chown -R xjweb:xjweb tomcat1
--文件夹剪切到安装目录
# mv tomcat1 /usr/local/tomcat1
--部署tomcat2
# tar -zxvf apache-tomcat-7.0.55.tar.gz
--重命名
# mv -f apache-tomcat-7.0.55 tomcat2
# chown -R xjweb:xjweb tomcat2
--文件夹剪切到安装目录
# mv tomcat2 /usr/local/tomcat2
2、设置环境变量
# Set Tomcat1 Environment
export TOMCAT_HOME1=/usr/local/tomcat1
export CATALINA_BASE1=/usr/local/tomcat1
export CATALINA_HOME1=/usr/local/tomcat1
exportCATALINA_TMPDIR1=/usr/local/tomcat1/temp
# Set Tomcat2 Environment
export TOMCAT_HOME2=/usr/local/tomcat2
export CATALINA_BASE2=/usr/local/tomcat2
export CATALINA_HOME2=/usr/local/tomcat2
exportCATALINA_TMPDIR2=/usr/local/tomcat2/temp
3、修改tomcat1的端口号
--修改tomcat1端口号
$ cd /usr/local/tomcat1/conf
$ cp server.xml server.xml.bak
$ cp web.xml web.xml.bak
$ vi server.xml
<Connector port="80"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"/>
--修改tomcat2端口号
$ cd /usr/local/tomcat2/conf
$ cp server.xml server.xml.bak
$ cp web.xml web.xml.bak
$ vi server.xml
--修改tomcat2/conf下的service.xml文件,修改(三个地方)如下:
(1)将8005修改为8006(或其他不用端口)
<Server port="8006"shutdown="SHUTDOWN">
(2)可以将8080改为8081(此处不用修改,tomcat1改为80端口)
<Connector port="8080"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"/>
(3)可以将8009改为8010(或其他不用端口)
<Connector port="8010"protocol="AJP/1.3" redirectPort="8443" />
4、 启动Tomcat
--启动tomcat1
# cd /usr/local/tomcat1/bin
#./startup.sh --启动
#./shutdown.sh --关闭
Using CATALINA_BASE: /usr/local/tomcat1
Using CATALINA_HOME: /usr/local/tomcat1
Using CATALINA_TMPDIR:/usr/local/tomcat1/temp
Using JRE_HOME: /usr/java/jdk1.7.0_25/jre/
UsingCLASSPATH: /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.
--启动tomcat2
# cd /usr/local/tomcat2/bin
#./startup.sh --启动
#./shutdown.sh --关闭
Using CATALINA_BASE: /usr/local/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2
Using CATALINA_TMPDIR:/usr/local/tomcat2/temp
Using JRE_HOME: /usr/java/jdk1.7.0_25/jre/
UsingCLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.
5、添加Tomcat开机自动启动
将/usr/local/tomcat1/bin/catalina.sh文件复制到/etc/init.d目录下并改名为tomcat1
# cp/usr/local/tomcat1/bin/catalina.sh /etc/init.d/tomcat1
修改/etc/init.d/tomcat1文件,在文件中添加为红色部分:
# vi/etc/init.d/tomcat1
在第一行下面输入如下内容(否则出现错误:tomcat服务不支持 chkconfig):
#!/bin/sh
# chkconfig: 2345 1090
# description:Tomcatservice
# Licensed to the Apache SoftwareFoundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
.....................................................................
# $Id: catalina.sh 1498485 2013-07-0114:37:43Z markt $
#-----------------------------------------------------------------------------
CATALINA_HOME=/usr/local/tomcat1
JAVA_HOME=/usr/java/jdk1.7.0_25
# OS specific support. $var _must_ be set to either true or false.
同理可以添加tomcat2
--添加tomcat服务:
# chkconfig--add tomcat1
# chkconfig--add tomcat2
--设置tomcat为开机自启动:
# chkconfigtomcat1 on
# chkconfigtomcat2 on
--关闭和启动tomcat 服务,由此生成的tomcat没有重启命令:
# service tomcat1stop
# service tomcat1start
--查看tomcat 服务运行级别:
# chkconfig--list |grep tomcat1
# chkconfig--list |grep tomcat2
--检查是否开机启动:
# netstat -an |grep 80
# netstat -an |grep 8080
6、测试Tomcat
# cd /usr/local/tomcat/webapps
在webapps目录下新建一个目录,起名叫test
# mkdir test
在test下新建一个目录WEB-INF,注意,目录名称区分大小写
# mkdir ./test/WEB-INF
在WEB-INF下新建一个文件web.xml,内容如下:
# vi ./test/WEB-INF/web.xml
<?xml version="1.0"encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//SunMicrosystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>My Web Application</display-name>
<description>
测试Tomcat!
</description>
</web-app>
在test下新建名为index.jsp测试页面,内容如下:
# vi ./test/index.jsp
<%@ page language="java"import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>MyJSP 'MyJsp.jsp' starting page</title>
</head>
<body>
<center>Now Time: <%=new java.util.Date()%></center>
</body>
</html>
打开浏览器,输入http://59.75.129.108/test/index.jsp,页面应显示当前时间
7 、设置Tomcat起始页
# vi /usr/local/tomcat1/conf/server.xml
找到123行处,在<Host></Host>中间加入如下内容:
<Context path=""docBase="/usr/local/tomcat1/webapps/GIS/" debug="0"reloadable="true" />
备注:默认起始页文档在/usr/local/tomcat1/conf/web.xml中进行设置,如下:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
--防止Request出现乱码
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />