CentOS下安装Tomcat

时间:2024-01-19 10:38:44

CentOS版本:CentOS-7-x86_64-Minimal-1810

1、安装JDK

详情查看:CentOS下安装JDK-rpm文件CentOS安装JDK-tar.gz文件

2、下载tomcat。

测试tomcat版本为:apache-tomcat-9.0.24.tar.gz。下载地址:https://tomcat.apache.org/download-90.cgi

3、通过ftp等工具将文件上传到主机上。

4、将tomcat解压到/usr/local/目录

cd /usr/local/
tar -zxvf /var/ftp/pub/apache-tomcat-9.0.24.tar.gz

CentOS下安装Tomcat

5、在/etc/init.d/目录,创建文件tomcat文件

cd /etc/init.d/
vi tomcat

文件内容如下:

#!/bin/bash
# chkconfig: 345 63 37 #export JAVA_HOME=/usr/local/jdk1.8.0_191 CATALINA_HOME=/usr/local/tomcat/apache-tomcat-9.0.24 case "$1" in
start)
echo "Starting Tomcat..."
$CATALINA_HOME/bin/startup.sh
;;
stop)
echo "Stopping Tomcat..."
$CATALINA_HOME/bin/shutdown.sh
;;
restart)
echo "Stopping Tomcat..."
$CATALINA_HOME/bin/shutdown.sh
sleep 1
echo "Starting Tomcat..."
$CATALINA_HOME/bin/startup.sh
;;
*)
echo "Usage: $0 {start|stop|restart}"
;;
esac
exit 0

CATALINA_HOME值与上面tomcat解压目录相同。

JDK有2种安装方式,如果安装的是.tar.gz文件,请将上面的标红行注释打开,明确JAVA_HOME位置,否则以服务方式启动tomcat会找不到JDK

6、对tomcat文件授予权限。

chmod 777 tomcat

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAd8AAAApCAIAAAD73rS/AAAEzUlEQVR4nO2c2XXkIBBFlZPS6WCUi2JRKB3HzEdrYXkFRWsxM773zDljY1QFNHrGQNUwCMZpea/Mr7VgmcbPD1/ze/96/X6rFhtIynJes6yTeQ8dRaVHzb1I2dwfDCrrHqmapnfVza1F83yMktFNZXPv0OfZrZGrhbDNRt/fmfeGxssGRnayfkYmo/bZjdeDbFnMCo3PKBm6oTxpzQ7FQ+d8Eaxpo5qvHQF0Rfwu92vzJB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d9VVi1PmkI486H1d6bduoM0B3WNEoZBDV4dClrYj98SRW8B/IIIo6A3SHkaPOijl2RnKXIpZL6RfckdyDekbaDCJhgpo6kvv3ZhBFnQG6w1BnU0basyCleTbIICpr+h2VbOYWRlcGUdQZoDucOerydayd3yd5PshksExjUVyqjuJG6RVhq02ZfCPT0e/V2RilU+ps2lR/uCxTPYMo6gzQHWQQJYPogDoDdAgZRMkgOqDOAB1CBlF9Z+OXZRBFnQG6gwyi/w/ECgLAF5BBtGdQZwCAHkGdAQAAAAAAAAAAAAAAAAAAAAAAAADgZ/kLmYnw01ZPsSMAAAAASUVORK5CYII=" alt="" />

7、启动、关闭、重启tomcat

service tomcat start    // 启动tomcat
service tomcat stop // 关闭tomcat
service tomcat restart // 重启tomcat

8、设置开机启动和关闭开机启动

chkconfig tomcat on     // 设置开机启动
chkconfig tomcat off // 关闭开机启动

9、通过http://IP地址:8080访问tomcat

CentOS下安装Tomcat

访问成功,tomcat安装完成。

补充:

tomcat可以指定JDK路径,创建在$TOMCAT_HOME/bin目录中创建setenv.sh文件

#!/bin/sh

JAVA_HOME=/usr/local/jdk1.8.0_221

CentOS下安装Tomcat

添加执行权限:

chmod  setenv.sh