一般在使用Tomcat时,服务器会从conf及webapps目录中读取配置文件,并将文件写入logs、temp和work目录,当然一些jar文件和class文件需要从服务器的公共目录树中予以加载。因此,为了让多个实例能同时运行,每一个Tomcat实例都必须有自己的目录集。
首先下载安装tomcat,这里jdk的安装就不提了
tar xf apache-tomcat-8.5.16.tar.gz -C /usr/local
然后,创建一个文件夹tomcat-instance(该文件夹用于存放tomcat所有实例),并在这个文件夹下分别创建tomcat1和tomcat2两个实例文件夹:
[root@wadeson local]# ll
drwxr-xr-x. 9 root root 4096 8月 9 15:24 apache-tomcat-8.5.16
drwxr-xr-x. 4 root root 4096 8月 9 15:38 tomcat-instance
cd tomcat-instance
mkdir tomcat1 tomcat2
cd tomcat1然后拷贝需要的一些目录
cp -a /usr/local/apache-tomcat-8.5.16/conf ./
创建一些必须的工作和应用目录:mkdir logs webapps work temp
[root@wadeson tomcat1]# ll
总用量 20
drwx------. 3 root root 4096 8月 9 15:41 conf
drwxr-xr-x. 2 root root 4096 8月 9 15:31 logs
drwxr-xr-x. 2 root root 4096 8月 9 15:26 temp
drwxr-xr-x. 8 root root 4096 8月 9 15:45 webapps
drwxr-xr-x. 3 root root 4096 8月 9 15:40 work
修改tomcat1实例下面的server.xml,因为各实例必须使用不同的端口
<Server port="8012" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
<Host name="localhost" appBase="/usr/local/tomcat-instance/tomcat1/webapps"
<Context path="" docBase="ROOT" reloadable="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/usr/local/tomcat-instance/tomcat1/logs"
然后去创建相应的目录:mkdir classes lib META-INF WEB-INF
[root@wadeson webapps]# pwd
/usr/local/tomcat-instance/tomcat1/webapps
[root@wadeson webapps]# ll
总用量 24
drwxr-xr-x. 2 root root 4096 8月 9 15:45 classes
drwxr-xr-x. 2 root root 4096 8月 9 15:45 lib
drwxr-xr-x. 2 root root 4096 8月 9 15:45 META-INF
drwxr-xr-x. 2 root root 4096 8月 9 15:44 myapp
drwxr-xr-x. 2 root root 4096 8月 9 15:40 ROOT
drwxr-xr-x. 2 root root 4096 8月 9 15:45 WEB-INF
在myapp和ROOT目录下面分别有index.jsp文件
最后为实例tomcat1创建start和stop脚本
[root@wadeson tomcat-instance]# cat start-tomcat1.sh
#!/bin/bash
export CATALINA_HOME=/usr/local/apache-tomcat-8.5.16
export CATALINA_BASE=/usr/local/tomcat-instance/tomcat1 指的是实例名,如果只有一个就和home变量一样
cd ${CATALINA_HOME}
./bin/catalina.sh start
[root@wadeson tomcat-instance]# cat stop-tomcat1.sh
#!/bin/bash
export CATALINA_HOME=/usr/local/apache-tomcat-8.5.16
export CATALINA_BASE=/usr/local/tomcat-instance/tomcat1
cd ${CATALINA_HOME}
./bin/catalina.sh stop
chmod +x start-tomcat1.sh stop-tomcat1.sh
然后启动实例tomcat1:
[root@wadeson tomcat-instance]# sh start-tomcat1.sh
Using CATALINA_BASE: /usr/local/tomcat-instance/tomcat1 这里则是tomcat实例名
Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.16
Using CATALINA_TMPDIR: /usr/local/tomcat-instance/tomcat1/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/apache-tomcat-8.5.16/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.16/bin/tomcat-juli.jar
Tomcat started.
[root@wadeson tomcat-instance]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 14669/sshd
tcp 0 0 ::ffff:127.0.0.1:8012 :::* LISTEN 6752/java
tcp 0 0 :::8081 :::* LISTEN 6752/java
tcp 0 0 :::22 :::* LISTEN 14669/sshd
tcp 0 0 :::8010 :::* LISTEN 6752/java
访问网站:
创建tomcat2实例:
cd tomcat2
[root@wadeson tomcat2]# cp -a /usr/local/apache-tomcat-8.5.16/conf ./
[root@wadeson tomcat2]# ll
总用量 4
drwx------. 2 root root 4096 6月 22 01:02 conf
[root@wadeson tomcat2]# mkdir logs webapps work temp
[root@wadeson tomcat2]# ll
总用量 20
drwx------. 2 root root 4096 6月 22 01:02 conf
drwxr-xr-x. 2 root root 4096 8月 9 16:12 logs
drwxr-xr-x. 2 root root 4096 8月 9 16:12 temp
drwxr-xr-x. 2 root root 4096 8月 9 16:12 webapps
drwxr-xr-x. 2 root root 4096 8月 9 16:12 work
[root@wadeson webapps]# mkdir ROOT WEB-INF META-INF classes lib tomcat2_app
[root@wadeson webapps]# ll
总用量 24
drwxr-xr-x. 2 root root 4096 8月 9 16:15 classes
drwxr-xr-x. 2 root root 4096 8月 9 16:15 lib
drwxr-xr-x. 2 root root 4096 8月 9 16:15 META-INF
drwxr-xr-x. 2 root root 4096 8月 9 16:15 ROOT
drwxr-xr-x. 2 root root 4096 8月 9 16:15 tomcat2_app
drwxr-xr-x. 2 root root 4096 8月 9 16:15 WEB-INF
修改server.xml:
<Server port="8013" shutdown="SHUTDOWN">
<Connector port="8082" protocol="HTTP/1.1"
<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
<Host name="localhost" appBase="/usr/local/tomcat-instance/tomcat2/webapps"
<Context path="" docBase="ROOT" reloadable="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/usr/local/tomcat-instance/tomcat2/logs"
创建实例tomcat2启动和停止脚本:
[root@wadeson tomcat-instance]# cat start-tomcat2.sh
#!/bin/bash
export CATALINA_HOME=/usr/local/apache-tomcat-8.5.16
export CATALINA_BASE=/usr/local/tomcat-instance/tomcat2
cd ${CATALINA_HOME}
./bin/catalina.sh start
[root@wadeson tomcat-instance]# cat stop-tomcat2.sh
#!/bin/bash
export CATALINA_HOME=/usr/local/apache-tomcat-8.5.16
export CATALINA_BASE=/usr/local/tomcat-instance/tomcat2
cd ${CATALINA_HOME}
./bin/catalina.sh stop
启动tomcat2实例:
[root@wadeson tomcat-instance]# sh start-tomcat2.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/java/latest
Using CLASSPATH: /usr/local/apache-tomcat-8.5.16/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.16/bin/tomcat-juli.jar
Tomcat started.
[root@wadeson tomcat-instance]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 14669/sshd
tcp 0 0 :::8011 :::* LISTEN 6866/java
tcp 0 0 ::ffff:127.0.0.1:8012 :::* LISTEN 6752/java
tcp 0 0 ::ffff:127.0.0.1:8013 :::* LISTEN 6866/java
tcp 0 0 :::8081 :::* LISTEN 6752/java
tcp 0 0 :::8082 :::* LISTEN 6866/java
tcp 0 0 :::22 :::* LISTEN 14669/sshd
tcp 0 0 :::8010 :::* LISTEN 6752/java
udp 0 0 0.0.0.0:68 0.0.0.0:* 1780/dhclient
访问网站:
至此多实例的tomcat就部署完成了
当把Web应用的文件和Tomcat发行版的文件分开管理后,升级Tomcat将会变得十分容易,因为我们可以用新目录直接替换整个Tomcat发行版的目录。(升级替换tomcat版本目录就行,替换webapps应用就行)