Skip to main content

java和tomcat安装

tip

注意下载地址,里面包括了版本号 两个东西都装在/usr/local里,并且生成了软链接

#!/bin/bash


yum install -y wget tar
download="https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz"
#download="https://??????/jdk-8u202-linux-x64.tar.gz"
jdk_file=`basename $download`
jdk_dir="/usr/local"


download_tomcat="https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.7/bin/apache-tomcat-10.1.7.tar.gz"
#download_tomcat="https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.87/bin/apache-tomcat-8.5.87.tar.gz"
tomcat_file=`basename $download_tomcat`
tomcat_dir="/usr/local"


color(){
resCol=60
moveToCol="\E[${resCol}G"
successCol="\E[1;32m"
failCol="\E[1;31m"
warnCol="\E[1;33m"
end="\E[0m"
out=${1}$moveToCol
case $2 in
0) out=${out}'['${successCol}' OK '
;;
1) out=${out}'['${failCol}' FAILD '
;;
*) out=${out}'['${warnCol}' WARNING '
;;
esac
echo -e ${out}${end}']'
}

install_jdk(){

[ -d $jdk_dir/jdk ] && color "$jdk_dir/jdk 目录已经存在,请检查是否已安装" && exit 0
#检查是否当前目录有二进制包
[ ! -f "./$jdk_file" ] && color "当前目录$jdk_file 不存在,开始下载jdk到当前目录" && wget $download
#解压到/usr/local
tar xzvf ./$jdk_file -C $jdk_dir
#制作软连接方便以后升级
ln -s $jdk_dir/jdk* $jdk_dir/jdk
#环境变量配置一下
cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$jdk_dir/jdk
export PATH=\$PATH:\$JAVA_HOME/bin
#export JRE_HOME=\$JAVA_HOME/jre
#export CLASSPATH=.:\$JAVA_HOME/lib/:\$JRE_HOME/lib/
EOF
. /etc/profile.d/jdk.sh
java -version && color "JDK 安装完成,请重置当前shell" 0 || color "JDK 安装失败" 1
}


install_tomcat(){
[ -d $tomcat_dir/tomcat ] && color "$tomcat_dir/tomcat 目录已经存在,请检查是否已安装" && exit 0
[ ! -f "./$tomcat_file" ] && color "当前目录$tomcat_file 不存在,开始下载jdk到当前目录" && wget $download_tomcat
tar xzvf ./$tomcat_file -C $tomcat_dir
#apache-tomcat-8.5.87.tar.gz 去掉后缀.tar.gz ${tomcat_file%.tar.gz} 表示删除匹配到的后缀.tar.gz
ln -s $tomcat_dir/${tomcat_file%.tar.gz} $jdk_dir/tomcat

echo "export PATH=$tomcat_dir/tomcat/bin:\$PATH" > /etc/profile.d/tomcat.sh
#添加tomcat用户
id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat

cat > $tomcat_dir/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=$JAVA_HOME
EOF

chown -R tomcat.tomcat $tomcat_dir/tomcat/


#创建tomcat.service
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 &> /dev/null
systemctl is-active tomcat.service &> /dev/null && color "tomcat 安装完成,请重置shell" 0 || color "tomcat 安装失败" 1


}




install_jdk
install_tomcat