Tomcat实战案例:yum和二进制安装

Posted njsummer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat实战案例:yum和二进制安装相关的知识,希望对你有一定的参考价值。

1. Tomcat 介绍

Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,Tomcat 具有处理html页面的功能,它还是一个Servlet和JSP容器。

Tomcat 起始于SUN 公司的一个Servlet的参考实现项目 Java Web Server,开发者是 James Duncan Davidson,在1999年,将项目贡献给了apache软件基金会(ASF),和ASF现有的项目 JServ 合并,并开源成为顶级项目。

Tomcat 仅仅实现了Java EE规范中与Servlet、JSP相关的类库,是JavaEE不完整实现。1999年发布初始版本是Tomcat 3.0,实现Servlet 2.2 和 JSP 1.1规范。Tomcat 4.x发布时,内建了Catalina(Servlet容器)和 Jasper(JSP engine)等。

当前 Tomcat 的正式版本已经更新到 10.x.x 版本,但当前企业生产环境中主流版本为 8.x 和 7.x。

官网: http://tomcat.apache.org/

官网文档: https://tomcat.apache.org/tomcat-8.5-doc/index.html

帮助文档:

https://cwiki.apache.org/confluence/display/tomcat/

https://cwiki.apache.org/confluence/display/tomcat/FAQ

2. yum安装

CentOS 7 的yum源的tomcat版本(7.0.76)较老,而CentOS8 yum源里无tomcat。目前比较主流的Tomcat是8.5.X版本,企业生产环境中推荐从Apache官网下载二进制tomcat包进行安装。

[root@mariadb-ip47 <sub>]# hostnamectl set-hostname TomCAT-IP47
[root@mariadb-ip47 </sub>]# exit
[root@tomcat-ip47 <sub>]# yum list tomcat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: repo.huaweicloud.com
* centosplus: repo.huaweicloud.com
* epel: repo.huaweicloud.com
* extras: repo.huaweicloud.com
* updates: repo.huaweicloud.com
Available Packages
tomcat.noarch 7.0.76-16.el7_9 updates
[root@tomcat-ip47 </sub>]# yum list tomcat*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: repo.huaweicloud.com
* centosplus: repo.huaweicloud.com
* epel: repo.huaweicloud.com
* extras: repo.huaweicloud.com
* updates: repo.huaweicloud.com
Available Packages
tomcat.noarch 7.0.76-16.el7_9 updates
tomcat-admin-webapps.noarch 7.0.76-16.el7_9 updates
tomcat-docs-webapp.noarch 7.0.76-16.el7_9 updates
tomcat-el-2.2-api.noarch 7.0.76-16.el7_9 updates
tomcat-javadoc.noarch 7.0.76-16.el7_9 updates
tomcat-jsp-2.2-api.noarch 7.0.76-16.el7_9 updates
tomcat-jsvc.noarch 7.0.76-16.el7_9 updates
tomcat-lib.noarch 7.0.76-16.el7_9 updates
tomcat-native.x86_64 1.2.23-1.el7 epel
tomcat-servlet-3.0-api.noarch 7.0.76-16.el7_9 updates
tomcat-webapps.noarch 7.0.76-16.el7_9 updates
tomcatjss.noarch 7.2.5-1.el7 base
[root@tomcat-ip47 <sub>]# yum -y install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp

[root@tomcat-ip47 </sub>]# ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=20295,fd=14))
LISTEN 0 128 *:111 *:* users:(("rpcbind",pid=870,fd=8))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1179,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1326,fd=13))
LISTEN 0 1 [::ffff:127.0.0.1]:8005 [::]:* users:(("java",pid=30381,fd=56))
LISTEN 0 100 [::]:8009 [::]:* users:(("java",pid=30381,fd=52))
LISTEN 0 128 [::]:111 [::]:* users:(("rpcbind",pid=870,fd=11))
LISTEN 0 100 [::]:8080 [::]:* users:(("java",pid=30381,fd=51))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1179,fd=4))
LISTEN 0 100 [::1]:25 [::]:* users:(("master",pid=1326,fd=14))
[root@tomcat-ip47 <sub>]# getent passwd tomcat
tomcat:x:53:53:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
[root@tomcat-ip47 </sub>]# ps aux|grep tomcat
tomcat 30381 31.6 2.0 8853548 336612 ? Ssl 10:43 0:19 /usr/lib/jvm/jre/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
root 30454 0.0 0.0 112812 976 pts/0 S+ 10:44 0:00 grep --color=auto tomcat
[root@tomcat-ip47 ~]#

打开浏览器,输入 http://192.168.250.47:8080 验证服务

Tomcat实战案例:yum和二进制安装_tomcat

3. 二进制安装

简要说明:CentOS8 下yum 源已经没有Tomcat 了,现在生产中都是下载二进制包安装比较多,在安装Tomat之前必须先部署JDK,JDK目前主要有Oracle JDK 和 OpenJDK,下面我们以Tomcat 9.0.62 和 jdk-8u321-linux-x64.rpm 包实现整个安装过程。

3.1 官网及国内镜像站点

我们在官网或者镜像站点上找到下载源,并准备好安装包。

官方和镜像站点下载:

https://tomcat.apache.org/download-80.cgi

https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/

Tomcat实战案例:yum和二进制安装_Tomcat二进制安装_02

Tomcat实战案例:yum和二进制安装_java_03


3.2 安装Oracle JDK

准备Tomcat安装及运行的java环境

[root@CentOS84-IP68 ]#hostnamectl set-hostname TomCAT-IP68
[root@CentOS84-IP68 ]#exit

# 准备好安装包
[root@TomCAT-IP68 ]#ll
-rw-r--r-- 1 root root 115308516 Apr 8 11:30 jdk-8u321-linux-x64.rpm
[root@TomCAT-IP68 ]#
[root@TomCAT-IP68 ]#ls -lh jdk-8u321-linux-x64.rpm
-rw-r--r-- 1 root root 110M Apr 8 11:30 jdk-8u321-linux-x64.rpm

# dnf 安装jdk,无需依赖包
[root@TomCAT-IP68 ]#dnf -y install jdk-8u321-linux-x64.rpm

[root@TomCAT-IP68 ]#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-IP68 ]#

# 初始化环境变量
[root@TomCAT-IP68 ]#vim /etc/profile.d/jdk.sh
#!/bin/bash
export JAVA_HOME=/usr/java/default
export PATH=$JAVA_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
[root@TomCAT-IP68 ]#. /etc/profile.d/jdk.sh

# 查看jdk信息
[root@TomCAT-IP68 ]#which java
/usr/java/default/bin/java
[root@TomCAT-IP68 ]#

[root@TomCAT-IP68 ]#ll /usr/java/
total 0
lrwxrwxrwx 1 root root 16 Apr 8 20:00 default -> /usr/java/latest
drwxr-xr-x 9 root root 286 Apr 8 20:00 jdk1.8.0_321-amd64
lrwxrwxrwx 1 root root 28 Apr 8 20:00 latest -> /usr/java/jdk1.8.0_321-amd64
[root@TomCAT-IP68 ]#

3.3 安装Tomcat

# CentOS8下yum源没有 Tomcat安装包
[root@TomCAT-IP68 ]#yum list tomcat
Last metadata expiration check: 0:32:27 ago on Fri 08 Apr 2022 08:00:21 PM CST.
Error: No matching Packages to list
[root@TomCAT-IP68 ]#

# 下载 apache-tomcat-9.0.62.tar.gz 安装包
[root@TomCAT-IP68 ]#wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.62/bin/apache-tomcat-9.0.62.tar.gz
# 解压并规划并创建好软件目录
[root@TomCAT-IP68 ]#tar xf apache-tomcat-9.0.62.tar.gz -C /usr/local/
[root@TomCAT-IP68 ]#cd /usr/local/
[root@TomCAT-IP68 ]#ln -s apache-tomcat-9.0.62/ tomcat
# 指定PATH变量
[root@TomCAT-IP68 ]#echo PATH=/usr/local/tomcat/bin:$PATH > /etc/profile.d/tomcat.sh
[root@TomCAT-IP68 ]#. /etc/profile.d/tomcat.sh
[root@TomCAT-IP68 ]#echo $PATH
/usr/local/tomcat/bin:/usr/java/default/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@TomCAT-IP68 ]#

# 查看当前变量设置和命令用法
[root@TomCAT-IP68 ]#catalina.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/default/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Usage: catalina.sh ( commands ... )
commands:
debug Start Catalina in a debugger
debug -security Debug Catalina with a security manager
jpda start Start Catalina under JPDA debugger
run Start Catalina in the current window
run -security Start in the current window with security manager
start Start Catalina in a separate window
start -security Start in a separate window with security manager
stop Stop Catalina, waiting up to 5 seconds for the process to end
stop n Stop Catalina, waiting up to n seconds for the process to end
stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running
configtest Run a basic syntax check on server.xml - check exit code for result
version What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined

# 查看默认的环境变量和版本信息
[root@TomCAT-IP68 ]#catalina.sh version
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/default/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Server version: Apache Tomcat/9.0.62
Server built: Mar 31 2022 14:34:15 UTC
Server number: 9.0.62.0
OS Name: Linux
OS Version: 《精通Tomcat:Java Web应用开发框架分析与案例实战》.pdf

Tomcat实战案例:JPress 的安装部署

实战案例 : Tomcat8 利用memcached1.5实现会话Cluster (同一主机sticky 模式)

构建Docker镜像实战之构建Tomcat9.0镜像(RPM一键安装Java环境)

实战案例 : Tomcat9 利用memcached1.6实现会话Cluster (同一主机no sticky 模式)

Linux服务安装-手动安装二进制JDK和tomcat服务