超简单的Liunx软件自动安装脚本(JAVA MySQL Nginx TomCat)

1. 前言

安装环境:

2. Linux下JAVA自动安装脚本

  • JDK1.8下载地址:官方网址
  • 本次JDK版本:jdk-8u231-linux-x64.tar.gz
  • 本次安装目录:/usr/local/
  • 需要任意JDK安装包
  1. 将任意 JDK 版本的安装包拷贝到服务器根目录。
  2. 创建脚本并粘贴脚本命令:vim java_install.sh
  3. 给文件加执行权限:chmod +x java_install.sh
  4. 执行脚本:sh java_install.sh
  5. 重载系统环境:source /etc/profile
  6. 输入命令查看 java : java -version
#!/bin/bash
#offline jdk install
ipath="/usr/local"
installpath=$(cd `dirname $0`; pwd)
j=`whereis java`
java=$(echo ${j} | grep "jdk")
if [[ "$java" != "" ]]
then
    echo "java was installed!"
else
    echo "java not installed!"
    echo;
    echo;
    echo "解压 jdk-*-linux-x64.tar.gz"
    tar -zxvf jdk-*-linux-x64.tar.gz >/dev/null 2>&1
    echo;
    echo;
    cd jdk* && jdkname=`pwd | awk -F '/' '{print $NF}'`
    echo "获取jdk版本: ${jdkname}"
    echo;
    echo;
    cd ${installpath}
    echo "获取当前目录:${installpath}"
    echo;
    echo;
    mv ${jdkname} ${ipath}
    echo "转移${jdkname}文件到${ipath}安装目录"
    echo "jdk安装目录:${ipath}/${jdkname}"
    echo;
    echo;
    echo "#java jdk" >> /etc/profile
    echo "export JAVA_HOME=${ipath}/${jdkname}" >> /etc/profile
    echo 'export JRE_HOME=${JAVA_HOME}/jre' >> /etc/profile
    echo 'export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib' >> /etc/profile
    echo 'export PATH=${JAVA_HOME}/bin:$PATH' >> /etc/profile
    source /etc/profile > /dev/null 2>&1
	cd $HOME
	rm jdk-*-linux-x64.tar.gz
    echo "jdk 安装完毕!"
    echo "请执行以下命令以使jdk环境生效"
    echo "source /etc/profile"
fi

3. Linux下MySQL自动安装脚本

  1. 将 MySQL 安装包拷贝到服务器根目录
  2. 安装一个依赖包:yum -y install autoconf
  3. 服务器根目录创建脚本并粘贴脚本命令:vim mysql_install.sh
  4. 安装并启动脚本:sh mysql_install.sh install
  5. 重载系统环境:source /etc/profile
  6. 登陆MySQL数据库:mysql -uroot -proot
  7. 脚本命令:
    mysql启动:sh mysql_install.sh start
    mysql关闭:sh mysql_install.sh stop
    mysql重启:sh mysql_install.sh restart
    mysql状态:sh mysql_install.sh check_status
    mysql卸载:sh mysql_install.sh uninstall
#!/bin/bash
# Mysql安装包所在路径,需要带上包名,示例:PACKAGE_FULL_WAY=/root/mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz
readonly PACKAGE_FULL_WAY=mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz
# Mysql安装主目录,示例:INSTALL_HOME=/usr/local/mysql
readonly INSTALL_HOME=/usr/local/mysql
# Mysql数据库root用户密码,示例:USER_PASSWD=root
readonly USER_PASSWD=root
 
# 检验用户
if [[ "$UID" -ne 0 ]]; then
    echo "ERROR: the script must run as root"
    exit 3
fi
 
function log_info() {
    echo "[$(date -d today +"%Y-%m-%d %H:%M:%S %:::z")] $1"
}
 
function log_error() {
    echo -e "[$(date +"%Y-%m-%d %H:%M:%S %Z%:z")] [ERROR] $* \n"
    exit 1
 
}
 
function check_result() {
    local ret_code=$1
    shift
    local error_msg=$*
    if [[ ${ret_code} -ne 0 ]]; then
        log_error ${error_msg}
    fi
}
 
# 校验参数
function check_param() {
    if [[ ! -n ${PACKAGE_FULL_WAY} ]] || [[ ! -n ${INSTALL_HOME} ]] || [[ ! -n ${USER_PASSWD} ]]; then
        log_error "Param: PACKAGE_FULL_WAY INSTALL_HOME USER_PASSWD can not be null"
    fi
    if [[ ! -f ${PACKAGE_FULL_WAY} ]]; then
        log_error "Please check the config of PACKAGE_FULL_WAY dose config Mysql package name"
    fi
}
 
function check_mysql_process() {
    local mysql_process_count=`ps -ef |grep ${INSTALL_HOME}|grep -vwE "grep|vi|vim|tail|cat"|wc -l`
    if [[ ${mysql_process_count} -gt 0 ]]; then
        log_error "please stop and uninstall the mysql first"
    fi
}
 
# 新建mysql用户
function add_user() {
    #create group mysql
    grep "^mysql" /etc/group &> /dev/null
    if [[ $? -ne 0 ]]; then
        groupadd mysql
    fi
 
    #create user mysql
    id mysql &> /dev/null
    if [[ $? -ne 0 ]]; then
        useradd -g mysql mysql
        chage -M 99999 mysql
    fi
}
 
# 安装Mysql
function install_mysql() {
    # 创建安装主目录
    mkdir -p ${INSTALL_HOME}
    # 解压mysql到安装主目录
    tar -zxvf ${PACKAGE_FULL_WAY} -C ${INSTALL_HOME} > /dev/null 2>&1
    check_result $? "unzip Mysql package error"
    local package_name=`ls ${INSTALL_HOME} |grep mysql`
    mv ${INSTALL_HOME}/${package_name}/* ${INSTALL_HOME}
    rm -rf ${INSTALL_HOME}/${package_name}
    cd ${INSTALL_HOME}
 
    # 新建数据库目录
    mkdir -p ${INSTALL_HOME}/data/mysql
    chown -R mysql:mysql ${INSTALL_HOME}
 
    # 安装并指定用户和data文件夹位置
    ./scripts/mysql_install_db --user=mysql --datadir=${INSTALL_HOME}/data/mysql
 
    # 复制mysql到服务自动启动里面
    cp -pf ${INSTALL_HOME}/support-files/mysql.server /etc/init.d/mysqld
    chmod 755 /etc/init.d/mysqld
    # 复制配置文件到etc下
    cp -pf ${INSTALL_HOME}/support-files/my-default.cnf /etc/my.cnf
    chmod 755 /etc/my.cnf
    # 修改basedir和datadir
    sed -i "s#^basedir=.*#basedir=${INSTALL_HOME}#" /etc/init.d/mysqld
    sed -i "s#^datadir=.*#datadir=${INSTALL_HOME}\/data\/mysql#" /etc/init.d/mysqld
    # 加入环境变量,方便使用mysql命令,但是需要source /etc/profile
    echo "###MYSQL_PATH_ENV_S" >>/etc/profile
    echo "export PATH=${INSTALL_HOME}/bin:\$PATH" >> /etc/profile
    echo "###MYSQL_PATH_ENV_E" >> /etc/profile
    # 启动Mysql
    start
    # 修改Mysql用户root密码
    ./bin/mysqladmin -u root -h localhost.localdomain password ${USER_PASSWD}
    cd ${INSTALL_HOME}
 
# 开启远程登录权限
./bin/mysql -h127.0.0.1 -uroot -p${USER_PASSWD} << EOF
grant all privileges on *.* to root@'%' identified by 'root'; flush privileges;
EOF
    chown -R mysql:mysql ${INSTALL_HOME}
}
 
# 安装Mysql
function install() {
    log_info "+++++++++++ step 1 ++++++++++++++++"
    check_param
    log_info "check_param finish"
 
    log_info "+++++++++++ step 2 ++++++++++++++++"
    check_mysql_process
    log_info "check_mysql_process finish"
 
    log_info "+++++++++++ step 3 ++++++++++++++++"
    add_user
    log_info "add_user finish"
 
    log_info "+++++++++++ step 4 ++++++++++++++++"
    install_mysql
    log_info "install_mysql finish"
}
 
# 卸载Mysql
function uninstall() {
    # 如果Mysql仍启动则停止Msql
    local mysql_process_count=`ps -ef |grep ${INSTALL_HOME}|grep -vwE "grep|vi|vim|tail|cat"|wc -l`
    if [[ ${mysql_process_count} -gt 0 ]]; then
        stop
    fi
 
    # 删除创建的文件
    rm -rf ${INSTALL_HOME}
    rm -rf /etc/init.d/mysqld
    rm -rf /etc/my.cnf
 
    # 删除sock文件
    if [[ -f /tmp/mysql.sock ]]; then
        rm -rf /tmp/mysql.sock
    fi
 
    # 删除配置的环境变量
    sed -i '/###MYSQL_PATH_ENV_S/,/###MYSQL_PATH_ENV_E/d' /etc/profile
 
    #删除用户和用户组
    id mysql &> /dev/null
    if [[ $? -eq 0 ]]; then
        userdel mysql
    fi
    log_info "uninstall Mysql success"
}
 
# 停止Mysql
function stop() {
    su - mysql -c "service mysqld stop"
}
 
# 启动Mysql
function start() {
    su - mysql -c "service mysqld start"
}
 
# Mysql状态检查
function check_status() {
    su - mysql -c "service mysqld status"
}
 
function usage() {
    echo "Usage: $PROG_NAME {start|stop|install|uninstall|check_status}"
    exit 2
 
}
 
PROG_NAME=$0
ACTION=$1
 
case "$ACTION" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    install)
        install
    ;;
    uninstall)
        uninstall
    ;;
    check_status)
        check_status
    ;;
    *)
        usage
    ;;
esac

4. Linux下Nginx自动安装脚本

  1. 服务器根目录创建脚本并粘贴脚本命令:vim nginx_install.sh
  2. 给脚本加执行权限:chmod +x nginx_install.sh
  3. 执行脚本并在命令后加入需要安装的版本号和安装目录:sh nginx_install.sh 1.16.1 /usr/lcoal/nginx
  4. 浏览器输入ip查看nginx页面。
#!/bin/bash

NGX_USR="www" 
NGX_VER="$1" #第一个参数nginx版本号
NGX_YUM="yum -y install"	
NGX_SRC="nginx-${NGX_VER}" 
NGX_DIR="$2" #第二个参数自定义目录
NGX_URL="http://nginx.org/download"
NGX_SOFT="${NGX_SRC}.tar.gz"
NGX_ARGS="--user=$NGX_USR --group=$NGX_USR --with-http_stub_status_module"
if [ $# -ne 2 ]; then
    echo "第一个参数请输入nginx版本号例如:1.16.1"
	echo "第二个参数请输入nginx安装目录例如:/usr/local/nginx"
    exit
else
	cd $HOME
    $NGX_YUM wget gzip tar make gcc
    $NGX_YUM pcre pcre-devel zlib-devel openssl-devel
    wget -c $NGX_URL/$NGX_SOFT    #根据参数自动下载
    ls -l   $NGX_SOFT 
    tar -xzf $NGX_SOFT 
    cd $NGX_SRC 
    useradd -s /sbin/nologin $NGX_USR -M
    ./configure --prefix=$NGX_DIR $NGX_ARGS  #安装路径
	if [ $? -eq 0 ];then
		make
	else
		echo  "please chenck nginx error"
	fi
	if [ $? -eq 0 ];then
		make install
	else
		echo "plase chenck install nginx error"
	fi
    $NGX_DIR/sbin/nginx  #启动
	cd $HOME
	rm -rf $NGX_SRC  #删除根文件夹文件夹
	rm $NGX_SOFT	#删除安装包
	cp $NGX_DIR/sbin/nginx /usr/local/bin #添加全局环境
    ps -ef|grep nginx
    setenforce 0
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --reload
fi

5. Linux下Tomcat自动安装脚本

  1. 服务器根目录创建脚本文件并粘贴命令:vim tomcat_install.sh
  2. 加上脚本执行权限:chmod +x tomcat_install.sh
  3. 执行脚本文件:sh tomcat_install.sh
  4. 打开浏览器输入ip加8080端口查看tomcat页面
#! /bin/bash
main_path=/usr/local/tomcat
install_name=apache-tomcat-9.0.30.tar.gz
env_fun()
{
echo "---检测本机环境----"
username=`ps -ef|grep apache-tomcat |grep -v grep`
if [[ -z $username ]]; then
    echo "tomcat不存在"
    return 10
else
    echo "tomcat存在"
    return 12
fi
}
install_fun(){
echo "建立tomcat文件夹"
mkdir -p $main_path
cd $main_path
echo "正在下载。请稍等..."
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gz
echo "正在解压,请稍等..."
tar -zxvf $install_name
echo "安装完成"
rm $main_path/apache-tomcat-9.0.30.tar.gz
echo "rm tar packet over"
cd apache-tomcat-9.0.30/bin
sh startup.sh
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
}
main(){
echo "***安装tomcat***"
sleep 1
env_fun
re=$?
if [ 10 -eq $re ] ;then
    install_fun
else
    echo "tomcat已存在,不需要安装"
fi
}
main
exit 0

6.shell脚本常用操作

命令解释
-eq等于
-ne不等于
-gt大于
-lq小于
-le小于等于
-ge大于等于
-z空串
-n非空串
-r可读为真
-w可写为真
-x可执行为真
-f正规文件为真
-d存在文件目录为真
-c文件为字符特殊文件为真
-b文件为块特殊文件为真
-s文件大小非0时为真
-t当文件描述符(默认为1)指定的设备为终端时为真
-a
-o
!
str1 = str2当两个串有相同内容、长度时为真
str1 != str2当串str1和str2不等时为真
-n str1当串的长度大于0时为真
-z str1当串的长度为0时为真
str1当串str1为非空时为真

end!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值