欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

shell脚本自动化创建虚拟机的基本配置之tomcat--mysql--jdk--maven

程序员文章站 2022-05-30 13:20:26
自动化shell脚本ps:此处安装的jdk,maven,tomcat均在/opt目录下,如需其他目录或者其他版本,可根据需要改动shell脚本即可。ps:所有代码全部手敲,已亲测能够使用,全部分享出来...

自动化shell脚本

ps:此处安装的jdk,maven,tomcat均在/opt目录下,如需其他目录或者其他版本,可根据需要改动shell脚本即可。
ps:所有代码全部手敲,已亲测能够使用,全部分享出来。

一:虚拟机初始化

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成虚拟机初始化

#!/bin/bash

#1.改变机器名
hostnamectl set-hostname $1

#2.打通ip地址
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.关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

 二:jdk自动化安装

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成jdk自动化安装

#!/bin/bash
jdktar="/opt/jdk-8u111-linux-x64.tar.gz"
# 检查原先是否已安装
checkexist(){
	jdkline1=$(grep -n "export java_home=.*" /etc/profile | cut -f1 -d':')
  if [ -n "$jdkline1" ];then
    echo "存在java_home路径,将其删除"
    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路径,将其删除"
    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路径,将其删除"
    sed -i "${jdkline3}d" /etc/profile
  fi
}
# 查询是否有jdk压缩包
if [ -e $jdktar ];then
	echo "--------存在jdk安装包-------"
	echo "正在解压jdk安装包。。。"
	tar -zxvf /opt/jdk-8u111-linux-x64.tar.gz
	if [ -e "/opt/soft2" ];then
		echo "存在该文件夹,删除。。"
		rm -rf /opt/soft2
	fi
	echo "---------------------------------"
	echo "---------------------------------"
	echo "正在建立jdk安装路径。。"
	echo "---------------------------------"
	echo "---------------------------------"
	mkdir -p /opt/soft2/jdk180
	mv -f jdk1.8.0_111 /opt/soft2/jdk180
	# 检查配置信息
	checkexist	
	echo "---------------------------------"
	echo "---------------------------------"
	echo "正在配置jdk环境。。。"
	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 "配置环境已完成。。。"
	echo 'this is the end'
	echo "---------------------------------"
 echo "---------------------------------"
 echo "配置版本信息如下:"
 source /etc/profile
 java -version
else
	echo "未检测到安装包,请将安装包放到opt目录下"
fi

 三:maven自动化安装

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成maven自动化安装

#!/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>"

# 检查原先是否已安装
checkexist(){
	mavenline1=$(grep -n "export maven_home=.*" /etc/profile | cut -f1 -d':')
  if [ -n "$mavenline1" ];then
    echo "存在maven_home路径,将其删除"
    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路径,将其删除"
    sed -i "${mavenline3}d" /etc/profile
  fi
}


# 查询是否有maven压缩包
if [ -e $maventar ];then

	echo "--------存在maven安装包-------"
	echo "正在解压maven安装包。。。"
	tar -zxvf /opt/apache-maven-3.6.1-bin.tar.gz
	if [ -e "/opt/soft2/maven361" ];then
		echo "存在该文件夹,删除。。"
		rm -rf /opt/soft2/maven361
	fi
	echo "---------------------------------"
	echo "---------------------------------"
	echo "正在建立maven安装路径。。"
	echo "---------------------------------"
	echo "---------------------------------"
	mkdir -p /opt/soft2/maven361
	mv -f apache-maven-3.6.1 /opt/soft2/maven361
	# 检查配置信息
	checkexist	
	echo "---------------------------------"
	echo "---------------------------------"
	echo "正在配置maven环境。。。"
	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 "----正在配置maven镜像源和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 "配置环境已完成。。。"
	echo 'this is the end'
	echo "---------------------------------"
 echo "---------------------------------"
 echo "配置版本信息如下:"
 source /etc/profile
 mvn -version
else
	echo "未检测到安装包,请将安装包放到opt目录下"

fi

 四:tomcat自动化安装

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成tomcat自动化安装

#!/bin/bash

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


# 查询是否有tomcat压缩包
if [ -e $tomcattar ];then

	echo "--------存在tomcat安装包-------"
	echo "正在解压maven安装包。。。"
	tar -zxvf /opt/apache-tomcat-8.5.54.tar.gz
	if [ -e "/opt/soft2/tomcat85" ];then
		echo "存在该文件夹,删除。。"
		rm -rf /opt/soft2/tomcat85
	fi
	echo "---------------------------------"
	echo "---------------------------------"
	echo "正在建立tomcat安装路径。。"
	echo "---------------------------------"
	echo "---------------------------------"
	mkdir -p /opt/soft2/tomcat85
	mv -f apache-tomcat-8.5.54 /opt/soft2/tomcat85
	echo "---------------------------------"
	echo "---------------------------------"
	echo "配置tomcat环境已完成。。。"
	echo 'this is the end'
	echo "---------------------------------"
	echo "---------------------------------"

else
	echo "未检测到安装包,请将安装包放到opt目录下"
fi

五:mysql自动化安装

创建shell脚本,把命令放进.sh脚本里,./xx.sh运行此脚本可以完成mysql自动化安装

#!/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)
# 检查是否已安装了mysql,未安装再安装
if [ ! -n "${mysqlinstall}" ];then
  # 检查centos是否自带了mariadb,若有则删除
  if [ -n "${mariadbinstall}" ];then
		echo "自带mariadb---删除mariadb中----"
    rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
		echo "--------------------------------"
		echo "--------------------------------"
		echo "--------------------------------"
	else
		echo "centos里没有mariadb软件"
		echo "-----------------------"
		echo "-----------------------"
  fi
	if [ -n "${wgetinstall}" ];then
		echo "-------存在wget下载工具-------"
		echo "------------------------------"
		echo "------------------------------"
	else		
		echo "-------安装下载工具wget-------"
		echo "------------------------------"
		echo "------------------------------"
		yum install -y wget
	fi
		echo "-------在线下载5.7mysql并安装-------"
		wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
		echo "-----------下载mysql成功-------------"
		echo "-------------------------------------"
		echo "-----------正在安装mysql中----------"
		echo "-------------------------------------"
		rpm -ivh mysql-community-release-el7-5.noarch.rpm
		echo "-----正在检测mysql安装环境是否成功----"
else
	echo "-------------已安装过mysql--------------"
fi
installmysqlserver(){
	if [ "${mysqlrepoinstall1}" = "mysql-community.repo" -a "${mysqlrepoinstall2}" = "mysql-community-source.repo" ];then
      echo "存在2个mysql安装环境,下载安装完成。。。"
      echo "----------------------------------------"
      echo "----------------------------------------"
			echo "-----------安装mysql-server中-----------"
      echo "----------------------------------------"
      echo "----------------------------------------"
			if [ ! -e "/var/lib/mysql" ];then
				yum install -y mysql-server
			else
				echo "-----已安装过mysql-server-----"
			fi
      echo "修改访问权限为当前用户。。。"
      chown -r root:root /var/lib/mysql
      ls -l /var/lib
      echo "重启mysql服务中"
      service mysqld restart
      echo "安装完成---请登录mysql修改密码和字符集"
  else
      echo "${mysqlrepoinstall1}"
      echo "${mysqlrepoinstall2}"
      echo "----没有安装成功----"
	fi
}
installmysqlserver

总结

到此这篇关于shell脚本自动化创建虚拟机的基本配置--tomcat--mysql--jdk--maven的文章就介绍到这了,更多相关shell脚本自动化创建虚拟机的基本配置--tomcat--mysql--jdk--maven内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!