Tomcat — MySQL — JDK — maven

Time:2020-8-5

Automated shell scripts

PS: JDK, Maven and Tomcat installed here are all in the / opt directory. If you need other directories or versions, you can change the shell script as needed.
PS: all the code is hand tapping, has been tested and can be used, all shared.

1: Virtual machine initialization

Create a shell script and put the command into the. Sh script/ xx.sh Run this script to complete the virtual machine initialization

#!/bin/bash

#1. Change the machine name
hostnamectl set-hostname $1

#2. Get through the IP address
sed -i 's/dhcp/static/' /etc/sysconfig/network-scripts/ifcfgenp0s3
sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfgenp0s3
echo "IPADDR=$2" >> /etc/sysconfig/network-scripts/ifcfgenp0s3
systemctl restart network

#3. Close the firewall
systemctl stop firewalld
systemctl disable firewalld

 2: JDK automatic installation

Create a shell script and put the command into the. Sh script/ xx.sh Running this script can complete the JDK automatic installation

#!/bin/bash
jdkTar="/opt/jdk-8u111-linux-x64.tar.gz"
#Check that it was originally installed
checkExist(){
	jdkline1=$(grep -n "export JAVA_HOME=.*" /etc/profile | cut -f1 -d':')
  if [ -n "$jdkline1" ];then
    Echo "Java exists_ Home path, delete it“
    sed -i "${jdkline1}d" /etc/profile
  fi
	jdkline2=$(grep -n "export CLASSPATH=.*$JAVA_HOME.*" /etc/profile | cut -f1 -d':')
  if [ -n "$jdkline2" ];then
    Echo "classpath exists, delete it"
    sed -i "${jdkline2}d" /etc/profile
  fi
	jdkline3=$(grep -n "export PATH=.*$JAVA_HOME.*" /etc/profile | cut -f1 -d':')
  if [ -n "$jdkline3" ];then
    Echo "path-java path exists, delete it"
    sed -i "${jdkline3}d" /etc/profile
  fi
}
#Query whether there is JDK package
if [ -e $jdkTar ];then
	Echo "---- JDK installation package exists ------"
	Echo "decompressing JDK installation package... "
	tar -zxvf /opt/jdk-8u111-linux-x64.tar.gz
	if [ -e "/opt/soft2" ];then
		Echo "the folder exists, delete.. "
		rm -rf /opt/soft2
	fi
	echo "---------------------------------"
	echo "---------------------------------"
	Echo "establishing JDK installation path.. "
	echo "---------------------------------"
	echo "---------------------------------"
	mkdir -p /opt/soft2/jdk180
	mv -f jdk1.8.0_111 /opt/soft2/jdk180
	#Check configuration information
	checkExist	
	echo "---------------------------------"
	echo "---------------------------------"
	Echo "configuring JDK environment... "
	sed -i '$a export JAVA_HOME=/opt/soft2/jdk180/jdk1.8.0_111' /etc/profile
	sed -i '$a export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' /etc/profile
	sed -i '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
	echo "---------------------------------"
	echo "---------------------------------"
	Echo "configuration environment completed... "
	echo 'this is the end'
	echo "---------------------------------"
 echo "---------------------------------"
 Echo "the configuration version information is as follows:
 source /etc/profile
 java -version
else
	Echo "no installation package detected, please put the installation package in opt directory"
fi

 3: Maven automation installation

Create a shell script and put the command into the. Sh script/ xx.sh Run this script to complete Maven automated installation

#!/bin/bash

mavenTar="/opt/apache-maven-3.6.1-bin.tar.gz"

mavenMirror=" <mirror>
  <id>alimaven</id>
  <mirrorOf>central</mirrorOf>
  <name>aliyun maven</name>
  <url>https://maven.aliyun.com/repository/central</url>
 </mirror>
 <mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <mirrorOf>central</mirrorOf>
 </mirror>
"
mavenrespo="<localRepository>/opt/soft2/maven361/apache-maven-3.6.1/jarstore</localRepository>"

#Check that it was originally installed
checkExist(){
	mavenline1=$(grep -n "export MAVEN_HOME=.*" /etc/profile | cut -f1 -d':')
  if [ -n "$mavenline1" ];then
    Echo "there is Maven_ Home path, delete it“
    sed -i "${mavenline1}d" /etc/profile
  fi
	mavenline3=$(grep -n "export PATH=.*$MAVEN_HOME.*" /etc/profile | cut -f1 -d':')
  if [ -n "$mavenline3" ];then
    Echo "path-maven path exists, delete it"
    sed -i "${mavenline3}d" /etc/profile
  fi
}


#Query for Maven packages
if [ -e $mavenTar ];then

	Echo "---- Maven installation package exists ------"
	Echo "extracting Maven package... "
	tar -zxvf /opt/apache-maven-3.6.1-bin.tar.gz
	if [ -e "/opt/soft2/maven361" ];then
		Echo "the folder exists, delete.. "
		rm -rf /opt/soft2/maven361
	fi
	echo "---------------------------------"
	echo "---------------------------------"
	Echo "establishing Maven installation path.. "
	echo "---------------------------------"
	echo "---------------------------------"
	mkdir -p /opt/soft2/maven361
	mv -f apache-maven-3.6.1 /opt/soft2/maven361
	#Check configuration information
	checkExist	
	echo "---------------------------------"
	echo "---------------------------------"
	Echo "configuring Maven environment... "
	sed -i '$a export MAVEN_HOME=/opt/soft2/maven361/apache-maven-3.6.1' /etc/profile
	sed -i '$a export PATH=$PATH:$MAVEN_HOME/bin' /etc/profile
	echo "---------------------------------"
	echo "---------------------------------"
	Echo "- configuring Maven mirror source and mirror ---"
	sed -i '/<\/mirror>/i ${mavenMirror}' /opt/soft2/maven361/apache-maven-3.6.1/conf/settings.xml
	sed -i '/<\/settings>/i ${mavenrespo}' /opt/soft2/maven361/apache-maven-3.6.1/conf/settings.xml
	echo "---------------------------------"
	echo "---------------------------------"
	Echo "configuration environment completed... "
	echo 'this is the end'
	echo "---------------------------------"
 echo "---------------------------------"
 Echo "the configuration version information is as follows:
 source /etc/profile
 mvn -version
else
	Echo "no installation package detected, please put the installation package in opt directory"

fi

 4: Tomcat automatic installation

Create a shell script and put the command into the. Sh script/ xx.sh Run this script to complete the Tomcat automated installation

#!/bin/bash

tomcatTar="/opt/apache-tomcat-8.5.54.tar.gz"
 


#Query whether there is a Tomcat package
if [ -e $tomcatTar ];then

	Echo "---- Tomcat installation package exists ------"
	Echo "extracting Maven package... "
	tar -zxvf /opt/apache-tomcat-8.5.54.tar.gz
	if [ -e "/opt/soft2/tomcat85" ];then
		Echo "the folder exists, delete.. "
		rm -rf /opt/soft2/tomcat85
	fi
	echo "---------------------------------"
	echo "---------------------------------"
	Echo "establishing Tomcat installation path.. "
	echo "---------------------------------"
	echo "---------------------------------"
	mkdir -p /opt/soft2/tomcat85
	mv -f apache-tomcat-8.5.54 /opt/soft2/tomcat85
	echo "---------------------------------"
	echo "---------------------------------"
	Echo "configuration of Tomcat environment completed... "
	echo 'this is the end'
	echo "---------------------------------"
	echo "---------------------------------"

else
	Echo "no installation package detected, please put the installation package in opt directory"
fi

5: Automatic installation of MySQL

Create a shell script and put the command into the. Sh script/ xx.sh Running this script can complete MySQL automatic installation

#!/bin/bash
mysqlInstall=$(rpm -qa | grep mysql)
mysqlRepoInstall1=$(find /etc/yum.repos.d/ -name "mysql-community.repo" | sed 's#.*/##')
mysqlRepoInstall2=$(find /etc/yum.repos.d/ -name "mysql-community-source.repo" | sed 's#.*/##')
wgetInstall=$(rpm -qa | grep wget)
mariadbInstall=$(rpm -qa | grep mariadb)
#Check whether MySQL has been installed, do not install and then install
if [ ! -n "${mysqlInstall}" ];then
  #Check whether CentOS has built-in MariaDB. If so, delete it
  if [ -n "${mariadbInstall}" ];then
		Echo "bring your own MariaDB --- delete from MariaDB ---"
    rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
		echo "--------------------------------"
		echo "--------------------------------"
		echo "--------------------------------"
	else
		Echo "there is no MariaDB software in CentOS"
		echo "-----------------------"
		echo "-----------------------"
  fi
	if [ -n "${wgetInstall}" ];then
		Echo "---- there is WGet download tool ------"
		echo "------------------------------"
		echo "------------------------------"
	else		
		Echo "- ------ install and download tool WGet -------"
		echo "------------------------------"
		echo "------------------------------"
		yum install -y wget
	fi
		Echo "---- download and install 5.7mysql Online
		wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
		Echo "---- MySQL downloaded successfully --------"
		echo "-------------------------------------"
		Echo "---- installing MySQL --"
		echo "-------------------------------------"
		rpm -ivh mysql-community-release-el7-5.noarch.rpm
		Echo "---- detecting whether the MySQL installation environment is successful ---"
else
	Echo "---- MySQL installed -------"
fi
installmysqlServer(){
	if [ "${mysqlRepoInstall1}" = "mysql-community.repo" -a "${mysqlRepoInstall2}" = "mysql-community-source.repo" ];then
      Echo "there are two MySQL installation environments. Download and install are completed... "
      echo "----------------------------------------"
      echo "----------------------------------------"
			Echo "---- installing MySQL server
      echo "----------------------------------------"
      echo "----------------------------------------"
			if [ ! -e "/var/lib/mysql" ];then
				yum install -y mysql-server
			else
				Echo "---- MySQL server has been installed"
			fi
      Echo "modify the access rights to the current user... "
      chown -R root:root /var/lib/mysql
      ls -l /var/lib
      Echo "restarting MySQL service"
      service mysqld restart
      Echo "installation complete - please login Mysql to change password and character set"
  else
      echo "${mysqlRepoInstall1}"
      echo "${mysqlRepoInstall2}"
      Echo "- not installed successfully ---"
	fi
}
installmysqlServer

summary

This article about shell script automatic creation of virtual machine basic configuration — Tomcat — MySQL — JDK — Maven article introduced here, more related shell script automatic creation of virtual machine basic configuration — Tomcat — MySQL — JDK — Maven content, please search the previous articles of developpaer or continue to browse the related articles below, I hope you can support developpaer more in the future!