1、如果做实验之前Tomcat存在或者运行中,需要先关闭服务,删除所有创建的文件
[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh //关闭服务 Using CATALINA_BASE: /usr/local/tomcat8 Using CATALINA_HOME: /usr/local/tomcat8 Using CATALINA_TMPDIR: /usr/local/tomcat8/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar [root@localhost ~]# rm -rf /usr/local/tomcat8/ /web/ //删除创建的文件夹
2、解压并部署tomcat程序
[root@localhost ~]# tar -xf apache-tomcat-8.5.16.tar.gz [root@localhost ~]# mv apache-tomcat-8.5.16 /usr/local/ [root@localhost ~]# ls /usr/local/ apache-tomcat-8.5.16 etc include lib libexec share bin games java lib64 sbin src
3、创建两个实例的工作目录
[root@localhost ~]# mkdir -p /usr/local/tomcat_instance/tomcat1
[root@localhost ~]# mkdir -p /usr/local/tomcat_instance/tomcat2
4、拷贝tomcat程序目录下的conf目录分别放入2个实例目录中(配置文件就有了)
-R:递归复制
[root@localhost ~]# cp -R /usr/local/apache-tomcat-8.5.16/conf/ /usr/local/tomcat_instance/tomcat1 [root@localhost ~]# cp -R /usr/local/apache-tomcat-8.5.16/conf/ /usr/local/tomcat_instance/tomcat2
5、分别在两个tomcat实例中创建bin目录,在bin中创建实例启动脚本和停止脚本
bin目录
[root@localhost ~]# mkdir /usr/local/tomcat_instance/tomcat1/bin
[root@localhost ~]# mkdir /usr/local/tomcat_instance/tomcat2/bin
6、创建启动脚本,给脚本执行权限
[root@localhost ~]# touch /usr/local/tomcat_instance/tomcat1/bin/startup.sh [root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat1/bin/startup.sh [root@localhost ~]# touch /usr/local/tomcat_instance/tomcat2/bin/startup.sh [root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat2/bin/startup.sh
查看所创建的所有实例目录
[root@localhost ~]# ls /usr/local/tomcat_instance/
tomcat1 tomcat2
[root@localhost ~]# ls /usr/local/tomcat_instance/tomcat1/conf
catalina.policy jaspic-providers.xsd tomcat-users.xsd
catalina.properties logging.properties web.xml
context.xml server.xml
jaspic-providers.xml tomcat-users.xml
[root@localhost ~]# ls /usr/local/tomcat_instance/tomcat1/bin
startup.sh
给脚本添加内容tomcat1
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/bin/startup.sh #! /bin/bash export CATALINA_HOME="/usr/local/apache-tomcat-8.5.16" //版本号 export CATALINA_BASE="/usr/local/tomcat_instance/tomcat1" //实例目录 export CATALINA_TMPDIR="$CATALINA_BASE/temp" //临时文件存放 export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid" //PID进程号文件 export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat1" //创建logs目录 if [ ! -d "$CATALINA_BASE/logs" ] //判断是否有实例目录下的logs文件,没有的话创建一下 then mkdir $CATALINA_BASE/logs fi //创建temp目录 if [ ! -d "$CATALINA_BASE/temp" ] then mkdir $CATALINA_BASE/temp fi //调用tomcat启动脚本,'$@'表示预定参数的所有内容,启动文件 bash $CATALINA_HOME/bin/startup.sh "$@"
给脚本添加内容tomcat2
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/bin/startup.sh
#! /bin/bash
export CATALINA_HOME="/usr/local/apache-tomcat-8.5.16"
export CATALINA_BASE="/usr/local/tomcat_instance/tomcat2"
export CATALINA_TMPDIR="$CATALINA_BASE/temp"
export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
export JAVA_OPTS="-server -Xms1024m -Djava.awt.headless=true -Dtomcat.name=tomcat2"
if [ ! -d "$CATALINA_BASE/logs" ]
then
mkdir $CATALINA_BASE/logs
fi
if [ ! -d "$CATALINA_BASE/temp" ]
then
mkdir $CATALINA_BASE/temp
fi
bash $CATALINA_HOME/bin/startup.sh "$@"
7、创建停止脚本,给脚本添加执行权限
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/bin/shutdown.sh
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/bin/shutdown.sh
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat1/bin/shutdown.sh
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat2/bin/shutdown.sh
给脚本添加内容tomcat1/bin/shutdown.sh
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/bin/shutdown.sh #! /bin/bash export CATALINA_HOME="/usr/local/apache-tomcat-8.5.16" export CATALINA_BASE="/usr/local/tomcat_instance/tomcat1" export CATALINA_TMPDIR="$CATALINA_BASE/temp" export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid" bash $CATALINA_HOME/bin/shutdown.sh "$@"
给脚本添加内容tomcat2/bin/shutdown.sh
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/bin/shutdown.sh #! /bin/bash export CATALINA_HOME="/usr/local/apache-tomcat-8.5.16" export CATALINA_BASE="/usr/local/tomcat_instance/tomcat2" export CATALINA_TMPDIR="$CATALINA_BASE/temp" export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid" bash $CATALINA_HOME/bin/shutdown.sh "$@"
8、修改配置文件,修改三个端口
修改第一个实例的配置文件
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/conf/server.xml
<Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
修改第二个实例的配置文件
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/conf/server.xml
<Server port="8006" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
9、创建测试页
[root@localhost ~]# mkdir -p /usr/local/tomcat_instance/tomcat1/webapps/ROOT
[root@localhost ~]# mkdir -p /usr/local/tomcat_instance/tomcat2/webapps/ROOT
给第一个实例创建测试页
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/webapps/ROOT/index.jsp <html> <title>Tomcat-1</title> <body> Hello! This is Tomcat-1. </body> </html>
给第二个实例创建测试页
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/webapps/ROOT/index.jsp <html> <title>Tomcat-2</title> <body> Hello! This is Tomcat-2. </body> </html>
10、启动服务
[root@localhost ~]# /usr/local/tomcat_instance/tomcat1/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat_instance/tomcat1 Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.16 Using CATALINA_TMPDIR: /usr/local/tomcat_instance/tomcat1/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /usr/local/apache-tomcat-8.5.16/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.16/bin/tomcat-juli.jar Using CATALINA_PID: /usr/local/tomcat_instance/tomcat1/bin/tomcat.pid Tomcat started. [root@localhost ~]# /usr/local/tomcat_instance/tomcat2/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat_instance/tomcat2 Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.16 Using CATALINA_TMPDIR: /usr/local/tomcat_instance/tomcat2/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /usr/local/apache-tomcat-8.5.16/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.16/bin/tomcat-juli.jar Using CATALINA_PID: /usr/local/tomcat_instance/tomcat2/bin/tomcat.pid Tomcat started.
查看接口
[root@localhost ~]# netstat -anpt | grep java tcp6 0 0 :::8009 :::* LISTEN 2532/java tcp6 0 0 :::8010 :::* LISTEN 2558/java tcp6 0 0 :::8080 :::* LISTEN 2532/java tcp6 0 0 :::8090 :::* LISTEN 2558/java
11、测试
12、在以上实例的基础上创建一个新的实例
复制实例文件
cp -r :复制目录
[root@localhost ~]# cd /usr/local/tomcat_instance/ [root@localhost tomcat_instance]# cp -r tomcat2/ tomcat3 [root@localhost tomcat_instance]# ls tomcat1 tomcat2 tomcat3
修改tomcat3中的文件
[root@localhost tomcat_instance]# cd tomcat3 [root@localhost tomcat3]# ls bin conf logs temp webapps work [root@localhost tomcat3]# ls bin/ shutdown.sh startup.sh tomcat.pid
[root@localhost tomcat3]# vim bin/shutdown.sh #! /bin/bash export CATALINA_HOME="/usr/local/apache-tomcat-8.5.16" export CATALINA_BASE="/usr/local/tomcat_instance/tomcat3" export CATALINA_TMPDIR="$CATALINA_BASE/temp" export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid" bash $CATALINA_HOME/bin/shutdown.sh "$@" [root@localhost tomcat3]# vim bin/startup.sh #! /bin/bash export CATALINA_HOME="/usr/local/apache-tomcat-8.5.16" export CATALINA_BASE="/usr/local/tomcat_instance/tomcat3" export CATALINA_TMPDIR="$CATALINA_BASE/temp" export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid" export JAVA_OPTS="-server -Xms1024m -Djava.awt.headless=true -Dtomcat.name=tomcat3" if [ ! -d "$CATALINA_BASE/logs" ] then mkdir $CATALINA_BASE/logs fi if [ ! -d "$CATALINA_BASE/temp" ] then mkdir $CATALINA_BASE/temp fi bash $CATALINA_HOME/bin/startup.sh "$@"
[root@localhost tomcat3]# ls bin/
shutdown.sh startup.sh tomcat.pid
[root@localhost tomcat3]# rm -rf bin/tomcat.pid
[root@localhost tomcat3]# vim webapps/ROOT/index.jsp <html> <title>Tomcat-3</title> <body> Hello! This is Tomcat-3. </body> </html>
[root@localhost tomcat3]# vim conf/server.xml
<Server port="8004" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Connector port="8070" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8008" protocol="AJP/1.3" redirectPort="8443" />
启服务
[root@localhost tomcat3]# /usr/local/tomcat_instance/tomcat3/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat_instance/tomcat3 Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.16 Using CATALINA_TMPDIR: /usr/local/tomcat_instance/tomcat3/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /usr/local/apache-tomcat-8.5.16/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.16/bin/tomcat-juli.jar Using CATALINA_PID: /usr/local/tomcat_instance/tomcat3/bin/tomcat.pid Tomcat started.
查看端口
[root@localhost tomcat3]# netstat -anpt | grep java tcp6 0 0 127.0.0.1:8004 :::* LISTEN 2659/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 2532/java tcp6 0 0 :::8070 :::* LISTEN 2659/java tcp6 0 0 127.0.0.1:8006 :::* LISTEN 2558/java tcp6 0 0 :::8008 :::* LISTEN 2659/java tcp6 0 0 :::8009 :::* LISTEN 2532/java tcp6 0 0 :::8010 :::* LISTEN 2558/java tcp6 0 0 :::8080 :::* LISTEN 2532/java tcp6 0 0 :::8090 :::* LISTEN 2558/java
测试