一、下载安装tomcat
# 安装wget,如果已经下载安装包,不使用wget在线下载,就不需要安装wget
[root@localhost ~] yum install wget
# 官网下载Tomcat9.0.89
[root@localhost ~] wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.89/bin/apache-tomcat-9.0.89.tar.gz
# Tomcat依赖Jdk,安装jdk1.8
[root@localhost home] tar -zxvf jdk-8u261-linux-x64.tar.gz
# 将Jdk1.8移到opt目录
[root@localhost home] mv jdk1.8.0_261 /opt/
# 配置Jdk环境变量,文件末尾加入
[root@localhost opt] vi /etc/profile
# Jre Environment 文件末尾加入
export JAVA_HOME=/opt/jdk1.8.0_261
export PATH=$JAVA_HOME/bin:$PATH
# 刷新环境变量
[root@localhost opt] source /etc/profile
# 查询Jdk版本
[root@localhost opt] java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
# 解压安装包
[root@localhost ~] tar -zxvf apache-tomcat-9.0.89.tar.gz
# 将安装包移到opt目录
[root@localhost ~] mv apache-tomcat-9.0.89 /opt/
# 进入tomcat目录
[root@localhost ~] cd /opt/apache-tomcat-9.0.89/bin/
# 启动Tomcat服务
[root@localhost bin] ./startup.sh
Using CATALINA_BASE: /opt/apache-tomcat-9.0.89
Using CATALINA_HOME: /opt/apache-tomcat-9.0.89
Using CATALINA_TMPDIR: /opt/apache-tomcat-9.0.89/temp
Using JRE_HOME: /opt/jdk1.8.0_261
Using CLASSPATH: /opt/apache-tomcat-9.0.89/bin/bootstrap.jar:/opt/apache-tomcat-9.0.89/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
# 检查tomcat启动状态
[root@localhost bin] ps -ef|grep tomcat
# 关闭Tomcat。
[root@localhost bin] ./shutdown.sh
二、设置Tomcat服务管理
# 设置Tomcat服务环境变量,在文件最前面增加如下内容
[root@localhost bin] vi /opt/apache-tomcat-9.0.89/bin/setclasspath.sh
# 配置Tomcat服务管理
[root@localhost bin] vi /etc/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
ExecStart=/opt/apache-tomcat-9.0.89/bin/startup.sh
ExecStop=/opt/apache-tomcat-9.0.89/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
# 重新加载配置文件
[root@localhost bin] systemctl daemon-reload
# 启动Tomcat服务
[root@localhost bin] systemctl start tomcat
# 查询Tomcat服务状态
[root@localhost bin] systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2024-06-09 13:10:01 CST; 5s ago
Process: 2132 ExecStart=/opt/apache-tomcat-9.0.89/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 2140 (java)
CGroup: /system.slice/tomcat.service
└─2140 /opt/jdk1.8.0_261/jre/bin/java -Djava.util.logging.config.file=/opt/apache-tomcat-9.0.89/conf/logging.properties -Dj...
Jun 09 13:10:01 localhost.localdomain systemd[1]: Starting Apache Tomcat Web Application Container...
Jun 09 13:10:01 localhost.localdomain startup.sh[2132]: Tomcat started.
Jun 09 13:10:01 localhost.localdomain systemd[1]: Started Apache Tomcat Web Application Container.
# 设置开机启动Tomcat
[root@localhost bin] systemctl enable tomcat
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /etc/systemd/system/tomcat.service.
三、开放Tomcat访问端口
# 添加8080端口
[root@localhost bin] firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
# 刷新防火墙配置
[root@localhost bin] firewall-cmd --reload
success
# 查看防火墙开放端口
[root@localhost bin] firewall-cmd --list-ports
8080/tcp
四、常用服务管理
# 启动Tomcat服务
[root@localhost bin] systemctl start tomcat
# 停止Tomcat服务
[root@localhost bin] systemctl stop tomcat
# 重启Tomcat服务
[root@localhost bin] systemctl restart tomcat
# 开机启动Tomcat服务
[root@localhost bin] systemctl enable tomcat
# 禁止开机启动Tomcat服务
[root@localhost bin] systemctl disable tomcat