Tomcat实战案例(升级CentOS7.9下yum安装的Tomcat运行的java环境(JDK))

知识的价值不在于占有,而在于使用。这篇文章主要讲述Tomcat实战案例:升级CentOS7.9下yum安装的Tomcat运行的java环境(JDK)相关的知识,希望能为你提供帮助。
【Tomcat实战案例(升级CentOS7.9下yum安装的Tomcat运行的java环境(JDK))】在centos7.9 上yum安装tomcat,查询出运行的java环境为java-1.8.0-openjdk-headless-1.8.0.322,原因应该是Oracle 拥有java的知识产权,OpenJDK尚属于开源免费。下面我们先升级OpenJDK 版本到 Oracke JDK "1.8.0_321",并确保tomcat能正常工作; 再将Oracke JDK "1.8.0_321" 版本升级到 jdk-11.0.14_linux-x64_bin ,也让Tomcat 正常工作起来。

# CentOS7.9下yum安装的Tomcat
[root@tomcat-ip47 < sub> ]# yum -y install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp

# 查看 jdk 版本
[root@tomcat-ip47 < /sub> ]# rpm -qa|grep jdk
java-1.8.0-openjdk-headless-1.8.0.322.b06-1.el7_9.x86_64
copy-jdk-configs-3.3-10.el7_5.noarch
java-1.8.0-openjdk-1.8.0.322.b06-1.el7_9.x86_64

# 查看 tomcat 的版本
[root@tomcat-ip47 < sub> ]# rpm -qa|grep tomcat
tomcat-lib-7.0.76-16.el7_9.noarch
tomcat-docs-webapp-7.0.76-16.el7_9.noarch
tomcat-7.0.76-16.el7_9.noarch
tomcat-webapps-7.0.76-16.el7_9.noarch
tomcat-servlet-3.0-api-7.0.76-16.el7_9.noarch
tomcat-el-2.2-api-7.0.76-16.el7_9.noarch
tomcat-admin-webapps-7.0.76-16.el7_9.noarch
tomcat-jsp-2.2-api-7.0.76-16.el7_9.noarch
[root@tomcat-ip47 < /sub> ]#

# 查看并记录下 tomcat 的服务运行文件
[root@tomcat-ip47 < sub> ]# find / -name tomcat.service
/sys/fs/cgroup/systemd/system.slice/tomcat.service
/etc/systemd/system/multi-user.target.wants/tomcat.service
/usr/lib/systemd/system/tomcat.service
[root@tomcat-ip47 < /sub> ]# cat /usr/lib/systemd/system/tomcat.service
# Systemd unit file for default tomcat
#
# To create clones of this service:
# DO NOTHING, use tomcat@.service instead.

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=simple
EnvironmentFile=/etc/tomcat/tomcat.conf
Environment="NAME="
EnvironmentFile=-/etc/sysconfig/tomcat
ExecStart=/usr/libexec/tomcat/server start
SuccessExitStatus=143
User=tomcat

[Install]
WantedBy=multi-user.target

# 启动服务文件中 EnvironmentFile=/etc/tomcat/tomcat.conf这条参数需要我们记录并理解
[root@tomcat-ip47 < sub> ]# cat /etc/tomcat/tomcat.conf
# System-wide configuration file for tomcat services
# This will be loaded by systemd as an environment file,
# so please keep the syntax. For shell expansion support
# place your custom files as /etc/tomcat/conf.d/*.conf
#
# There are 2 "classes" of startup behavior in this package.
# The old one, the default service named tomcat.service.
# The new named instances are called tomcat@instance.service.
#
# Use this file to change default values for all services.
# Change the service specific ones to affect only one service.
# For tomcat.service its /etc/sysconfig/tomcat, for
# tomcat@instance its /etc/sysconfig/tomcat@instance.

# This variable is used to figure out if config is loaded or not.
TOMCAT_CFG_LOADED="1"

# In new-style instances, if CATALINA_BASE isnt specified, it will
# be constructed by joining TOMCATS_BASE and NAME.
TOMCATS_BASE="/var/lib/tomcats/"

# Where your java installation lives
JAVA_HOME="/usr/lib/jvm/jre"

# Where your tomcat installation lives
CATALINA_HOME="/usr/share/tomcat"

# System-wide tmp
CATALINA_TMPDIR="/var/cache/tomcat/temp"

# You can pass some parameters to java here if you wish to
#JAVA_OPTS="-Xminf0.1 -Xmaxf0.3"

# Use JAVA_OPTS to set java.library.path for libtcnative.so
#JAVA_OPTS="-Djava.library.path=/usr/lib"

# Set default javax.sql.DataSource factory to apache commons one. See rhbz#1629162
JAVA_OPTS="-Djavax.sql.DataSource.Factory=org.apache.commons.dbcp.BasicDataSourceFactory"

# You can change your tomcat locale here
#LANG="en_US"

# Run tomcat under the Java Security Manager
SECURITY_MANAGER="false"

# SHUTDOWN_WAIT has been deprecated. To change the shutdown wait time, set
# TimeoutStopSec in tomcat.service.

# If you wish to further customize your tomcat environment,
# put your own definitions here
# (i.e. LD_LIBRARY_PATH for some jdbc drivers)
[root@tomcat-ip47 < /sub> ]#

# 停掉 tomcat 服务
[root@tomcat-ip47 < sub> ]# systemctl stop tomcat

# 确定jdk版本,并卸载掉老版的安装包等
[root@tomcat-ip47 < /sub> ]# rpm -qa|grep jdk
java-1.8.0-openjdk-headless-1.8.0.322.b06-1.el7_9.x86_64
copy-jdk-configs-3.3-10.el7_5.noarch
java-1.8.0-openjdk-1.8.0.322.b06-1.el7_9.x86_64
[root@tomcat-ip47 < sub> ]# rpm -e --nodeps java-1.8.0-openjdk-1.8.0.322.b06-1.el7_9.x86_64
[root@tomcat-ip47 < /sub> ]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.322.b06-1.el7_9.x86_64
[root@tomcat-ip47 < sub> ]# rpm -e --nodeps copy-jdk-configs-3.3-10.el7_5.noarch
[root@tomcat-ip47 < /sub> ]# rpm -qa|grep jdk

# 下载并上传新版 jdk安装包 jdk-8u321-linux-x64.rpm
[root@tomcat-ip47 < sub> ]# ll
-rw-r--r--1 root root11915655 Apr8 20:53 apache-tomcat-10.0.20.tar.gz
-rw-r--r--1 root root 151500100 Apr8 11:34 jdk-11.0.14_linux-x64_bin.rpm
-rw-r--r--1 root root 115308516 Apr8 11:30 jdk-8u321-linux-x64.rpm
[root@tomcat-ip47 < /sub> ]#

# 安装新版本 jdk-8u321-linux-x64.rpm 安装包
[root@tomcat-ip47 < sub> ]# rpm -ivh jdk-8u321-linux-x64.rpm
warning: jdk-8u321-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing...################################# [100%]
Updating / installing...
1:jdk1.8-2000:1.8.0_321-fcs################################# [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...

# 查看java版本
[root@tomcat-ip47 < /sub> ]# java -version
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)
[root@tomcat-ip47 < sub> ]# which java
/usr/bin/java
[root@tomcat-ip47 < /sub> ]# ll /usr/java/
total 0
lrwxrwxrwx 1 root root16 Apr8 19:49 default -> /usr/java/latest
drwxr-xr-x 9 root root 286 Apr8 19:49 jdk1.8.0_321-amd64
lrwxrwxrwx 1 root root28 Apr8 19:49 latest -> /usr/java/jdk1.8.0_321-amd64
[root@tomcat-ip47 < sub> ]#
[root@tomcat-ip47 < /sub> ]#

# 尝试启动 tomcat 服务
[root@tomcat-ip47 < sub> ]# systemctl start tomcat
[root@tomcat-ip47 < /sub> ]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-04-08 19:54:10 CST; 8s ago
Main PID: 32688 (java)
CGroup: /system.slice/tomcat.service
└─32688 /usr/bin/java -Djavax.sql.DataSource.Factory=org.apache.commons.dbcp.BasicDataSourceFactory -classpath /...

[root@tomcat-ip47 < sub> ]#

# 老版本的tomcat 并没有 catalina.sh 和 startup.sh 这样的脚本;回头可以去查看服务启动文件也确实没有调用这类脚本去启动,因此上面升级好JDK后可以直接启动服务,并能正常工作
[root@tomcat-ip47 < /sub> ]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@tomcat-ip47 < sub> ]# find / -name startup.sh
[root@tomcat-ip47 < /sub> ]# find / -name catalina.sh

[root@tomcat-ip47 < sub> ]# ps aux|grep tomcat
tomcat11811.62.1 8849548 349092 ?Ssl19:550:18 /usr/bin/java -Djavax.sql.DataSource.Factory=org.apache.commons.dbcp.BasicDataSourceFactory -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -Dcatalina.home=/usr/share/tomcat -Djava.endorsed.dirs= -Djava.io.tmpdir=/var/cache/tomcat/temp -Djava.util.logging.config.file=/usr/share/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager org.apache.catalina.startup.Bootstrap start
root29990.00.0 112812976 pts/0S+20:130:00 grep --color=auto tomcat
[root@tomcat-ip47 < /sub> ]#


#############################################################################################
#### 下面我们进一步升级到最新的JDK,过程和步骤相同

[root@tomcat-ip47 < sub> ]# rpm -e --nodeps jdk1.8-1.8.0_321-fcs.x86_64
[root@tomcat-ip47 < /sub> ]# rpm -ivh jdk-11.0.14_linux-x64_bin.rpm
warning: jdk-11.0.14_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing...################################# [100%]
Updating / installing...
1:jdk-11-2000:11.0.14-ga################################# [100%]

[root@tomcat-ip47 < sub> ]# java -version
java version "11.0.14" 2022-01-18 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.14+8-LTS-263)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.14+8-LTS-263, mixed mode)

[root@tomcat-ip47 < /sub> ]# which java
/usr/bin/java
[root@tomcat-ip47 < sub> ]# ll /usr/java/
total 0
lrwxrwxrwx1 root root16 Apr8 20:32 default -> /usr/java/latest
drwxr-xr-x 10 root root 139 Apr8 20:32 jdk-11.0.14
lrwxrwxrwx1 root root21 Apr8 20:32 latest -> /usr/java/jdk-11.0.14
[root@tomcat-ip47 < /sub> ]#

[root@tomcat-ip47 < sub> ]# systemctl start tomcat
[root@tomcat-ip47 < /sub> ]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-04-08 20:35:21 CST; 6s ago
Main PID: 4533 (java)
CGroup: /system.slice/tomcat.service
└─4533 /usr/bin/java -Djavax.sql.DataSource.Factory=org.apache.commons.dbcp.BasicDataSourceFactory -classpath /u...

[root@tomcat-ip47 < sub> ]#


[root@tomcat-ip47 < /sub> ]# ll /usr/share/tomcat
total 0
drwxr-xr-x 2 root root76 Apr8 10:42 bin
lrwxrwxrwx 1 root tomcat 11 Apr8 10:42 conf -> /etc/tomcat
lrwxrwxrwx 1 root tomcat 22 Apr8 10:42 lib -> /usr/share/java/tomcat
lrwxrwxrwx 1 root tomcat 15 Apr8 10:42 logs -> /var/log/tomcat
lrwxrwxrwx 1 root tomcat 22 Apr8 10:42 temp -> /var/cache/tomcat/temp
lrwxrwxrwx 1 root tomcat 23 Apr8

    推荐阅读