CentOS7部署安装Tomcat

时间:2023-01-12 16:41:10

本文内容主要用于个人的学习,所以一些本人已经烂熟于心的东西会省略掉。
1、配置JAVA相关内容。
这里选择了最新了JDK9,JDK9里面有个新特性,那就是移除了JRE,所以为了保证软件能够正常运行,因此JDK和JRE都下载了。这里贴一下JDK和JRE的配置文件内容:

JAVA_HOME=/home/jdk-9.0.4
JRE_HOME=/home/jre-9.0.4
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JRE_HOME/lib/ext:$JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH

2、下载解压Tomcat,这里下载的是8.0的版本。
3、设置Tomcat服务的启动和关闭
编辑创建文件:vi /etc/rc.d/init.d/tomcat
文件内容:

#!/bin/bash
# /etc/rc.d/init.d/tomcat
# init script for tomcat precesses
# processname: tomcat
# description: tomcat is a j2se server
# chkconfig: 2345 86 16
# description: Start up the Tomcat servlet engine.

if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
else
echo -e "\atomcat: unable to locate functions lib. Cannot continue."
exit -1
fi
RETVAL=$?
CATALINA_HOME="/home/apache-tomcat-8.5.24" #tomcat安装目录,你安装在什么目录下就复制什么目录
case "$1" in
start)
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
$CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
$CATALINA_HOME/bin/shutdown.sh
fi
;;
*)
echo $"Usage: $0 {start|stop}"
exit 1
;;
esac
exit $RETVAL

修改文件权限:chmod 755 /etc/rc.d/init.d/tomcat
添加到服务中:chkconfig --add /etc/rc.d/init.d/tomcat
编辑tomcat中的catalina.sh文件:cat /home/apache-tomcat-8.5.24/bin/catalina.sh
添加内容在文件开头:

export JAVA_HOME=/home/jdk-9.0.4
export CATALINA_HOME=/home/apache-tomcat-8.5.24
export CATALINA_BASE=/home/apache-tomcat-8.5.24
export CATALINA_TMPDIR=/home/apache-tomcat-8.5.24/temp

4、设置Tomcat开机启动:

export JAVA_HOME=/home/jdk-9.0.4  
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
export CATALINA_HOME=/usr/local/kencery/tomcat/
/usr/local/kencery/tomcat/bin/startup.sh

5、配置防火墙。
关闭firewall服务:

systemctl status firewalld.service #检测是否开启了firewall
systemctl stop firewalld.service #关闭firewall
sytsemctl disable firewalld.service #禁止firewall开机自启

安装iptables-services服务:

yum install iptables-services
vi /etc/sysconfig/iptables
iptables的内容:
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT #这里是开放的端口
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

重启iptables服务

systemctl restart iptables.service  
systemctl enable iptables.service 

6、重启系统,浏览器访问 http://ip:8080 如果成功看到Tomcat配置界面表示服务器配置成功