CentOS7 install Tomcat9

教程适用于CentOS7,使用编译安装方式,在使用本教程时请注意你的安装路径是否一致!

教程测试于:

CentOS7.4 64位

jdk-8u152

Tomcat install

跳转到软件目录

cd /opt

下载tomcat 编译包

wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.2/bin/apache-tomcat-9.0.2.tar.gz

解压编译包

tar zxvf apache-tomcat-9.0.2.tar.gz

安装完毕

打开防火墙端口

firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload

访问http://服务器ip:8080 即可查看tomcat欢迎页

设置tomcat开机自启动

vi /etc/systemd/system/tomcat.service

在文件内追加如下内容并保存

[Unit]

Description=Apache Tomcat Web Application Container

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

Environment=JAVA_HOME=/opt/jdk1.8.0_152

Environment=CATALINA_HOME=/opt/apache-tomcat-9.0.2

Environment=CATALINA_BASE=/opt/apache-tomcat-9.0.2

ExecStart=/opt/apache-tomcat-9.0.2/bin/startup.sh start

ExecStop=/opt/apache-tomcat-9.0.2/bin/shutdown.sh stop

[Install]

WantedBy=multi-user.target

重载文件

systemctl daemon-reload

设置tomcat开机启动

systemctl enable tomcat

启动tomcat

systemctl start tomcat

常用命令汇总

查看运行状态

systemctl status tomcat

启动tomcat

systemctl start tomcat

停止tomcat

systemctl stop tomcat

重启tomcat

systemctl restart tomcat

去除tomcat开机启动

systemctl disable tomcat

设置tomcat开机启动

systemctl enable tomcat

说明:设置tomcat自启动后建议使用命令方式直接启动或关闭等操作。

脚本启动tomcat

cd /opt/apache-tomcat-9.0.2/bin
./startup.sh

脚本停止tomcat

cd /opt/apache-tomcat-9.0.2/bin
./shutdown.sh

查看当前日志

/opt/apache-tomcat-9.0.2/logs
cat catalina.out

跟踪查看当前日志(与windows一致,实时更新控制台输出,方便debug)

/opt/apache-tomcat-9.0.2/logs
tail -f catalina.out

异常处理

提示内容:

Job for tomcat.service failed because the control process exited with error code. See "systemctl status tomcat.service" and "journalctl -xe" for details.

表明你的tomcat自启动文件有误,请重新按照步骤添加tomcat自启动

提示内容:

Failed to start tomcat.service: Access denied

表示服务无法正常启动,执行下面命令然后再执行启动tomcat命令一般可以解决

重新启动systemd

systemctl daemon-reexec