[CentOS7]安装tomcat并开启自启动

安装jdk

1、CentOS 6.X 和 7.X 自带有OpenJDK runtime environment (openjdk)。它是一个在linux上实现开源的Java 平台。
  yum search java | grep -i --color JDK
2、在CentOS linux安装 JAVA SDK 在命令行终端以root用户 输入以下命令yum安装 OpenSDK :
 yum install java-1.8.0-openjdk  java-1.8.0-openjdk-devel  #安装openjdk
3、配置环境变量
 编辑 
vi /etc/profile
写入:
  export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
        export CLASSPATH=$JAVA_HOME/lib/*.*
        export PATH=$PATH:$JAVA_HOME/bin
生效:
  source /etc/profile #让设置立即生效。
确认是否成功:
  echo $JAVA_HOME
        echo $CLASSPATH
        echo $PATH
4、查看是否安装成功
java -version    

安装tomcat

1、安装wget
yum install wget
2、下载、解压安装tomcat
   wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.13/bin/apache-tomcat-9.0.13.tar.gz
    tar xvf apache-tomcat-9.0.13.tar.gz -C /usr/local/ ## 解压
    mv /usr/local/apache-tomcat-9.0.13/ /usr/local/tomcat/  ##剪切到tomcat目录下
3、 把startup.sh和shutdown.sh这两个脚本里的最前面加上下面一段:
    export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
        export TOMCAT_HOME=/usr/local/tomcat
        export CATALINA_HOME=/usr/local/tomcat
        export CLASS_PATH=$JAVA_HOME/bin/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tool.jar
        export PATH=$PATH:/usr/local/java/bin:/usr/local/tomcat/bin
4、启动tomcat
 /usr/local/tomcat/bin/startup.sh  

5、开放默认端口8080

firewall-cmd --zone=public --add-port=8080/tcp --permanent
 #重启
       systemctl restart firewalld.service
       #重新加载载
       firewall-cmd --reload

6、浏览器中访问http://192.168/226/129:8080,出现下面页面,证明安装成功

[CentOS7]安装tomcat并开启自启动

7、设置开机自启

vi /etc/rc.d/init.d/tomcat

写入以下内容:

#!/bin/bash
#
# kenny [email protected]
# /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="/usr/local/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

8、添加权限,使得脚本文件可执行

chmod 755 /etc/rc.d/init.d/tomcat

9、添加到服务中

chkconfig --add /etc/rc.d/init.d/tomcat

10、在catalina.sh中添加如下内容

#auto startup tomcat config
        export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
        export TOMCAT_HOME=/usr/local/tomcat
        export CATALINA_HOME=/usr/local/tomcat
        export CLASS_PATH=$JAVA_HOME/bin/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tool.jar
        export PATH=$PATH:/usr/local/java/bin:/usr/local/tomcat/bin

重启tomcat

结语

注意环境变量的配置,jdk的路径问题。