#下载包到/root目录
[root@tomcat ~]#ll jdk-8u212-linux-x64.tar.gz apache-tomcat-8.5.82.tar.gz
-rw-r--r-- 1 root root 10610025 Aug 27 17:14 apache-tomcat-8.5.82.tar.gz
-rw-r--r-- 1 root root 195013152 Aug 27 17:08 jdk-8u212-linux-x64.tar.gz
#编写脚本
[root@tomcat ~]#cat tomcat8.5.sh
#!/bin/bash
#================================================================
# Copyright (C) 2022 IEucd Inc. All rights reserved.
#
# 文件名称:tomcat8.5.sh
# 创 建 者:TanLiang
# 创建日期:2022年08月27日
# 描 述:This is a test file
#
#================================================================
. /etc/init.d/functions
DIR=`pwd`
JDK_FILE="jdk-8u212-linux-x64.tar.gz"
TOMCAT_FILE="apache-tomcat-8.5.82.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"
install_jdk(){
if ! [ -f "$DIR/$JDK_FILE" ];then
action "$JDK_FILE 文件不存在" false
exit;
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk1.8.* jdk
cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=\$JAVA_HOME/jre
export CLASSPATH=\$JAVA_HOME/lib/:\$JRE_HOME/lib/
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
. /etc/profile.d/jdk.sh
java -version && action "JDK 安装完成" || { action "JDK 安装失败" false ; exit; }
}
install_tomcat(){
if ! [ -f "$DIR/$TOMCAT_FILE" ];then
action "$TOMCAT_FILE 文件不存在" false
exit;
elif [ -d $TOMCAT_DIR/tomcat ];then
action "TOMCAT 已经安装" false
exit
else
[ -d "$TOMCAT_DIR" ] || mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd $TOMCAT_DIR && ln -s apache-tomcat-*/ tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"'$PATH' > /etc/profile.d/tomcat.sh
id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat
cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=$JDK_DIR/jdk
EOF
chown -R tomcat.tomcat $TOMCAT_DIR/tomcat/
cat > /lib/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now tomcat.service
systemctl is-active tomcat.service &> /dev/null && action "TOMCAT 安装完成" || {
action "TOMCAT 安装失败" false ; exit; }
}
install_jdk
install_tomcat
#运行脚本安装tomcat
[root@tomcat ~]#bash tomcat8.5.sh
java version "1.8.0_212"
Java(TM) SE Runtime Environment (build 1.8.0_212-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)
JDK 安装完成 [ OK ]
Created symlink /etc/systemd/system/multi-user.target.wants/tomcat.service → /usr/lib/systemd/system/tomcat.service.
TOMCAT 安装完成 [ OK ]
[root@tomcat ~]#
#验证
[root@tomcat ~]#echo "`hostname`" > /tomcat/webapps/ROOT/index.html
[root@tomcat ~]#cat /usr/local/tomcat/webapps/ROOT/index.html
tomcat
[root@tomcat ~]#curl localhost:8080
tomcat
标签:8.5,tomcat,JDK,一键,jdk,root,tar,DIR
From: https://www.cnblogs.com/tanll/p/17748124.html