Linux基础服务二进制一键安装shell脚本

Posted 礁之

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux基础服务二进制一键安装shell脚本相关的知识,希望对你有一定的参考价值。

  • 服务版本:

mysql-8.0.20
redis-5.0.0
jdk1.8.0_321
tomcat-8.0.1
minio-RELEASE.2021-10-23T03-28-24Z
nginx-1.20.2

  • 压缩包地址:

链接:https://pan.baidu.com/s/1xz5Ag0rHWUhMovTPiCgm4g?pwd=8dyr
提取码:8dyr

  • 解压之后执行前需要修改脚本,init.sh为安装脚本,这个压缩包自带所需的离线yum源,根据自己需求定义函数
#!/bin/bash
#此脚本所有的安装目录全部都在$max_dir下,这个变量的会筛选出当前机器空间最大的挂载目录
#请在解压目录执行此脚本

file=$PWD
max_dir=$(df|sed '1d'|awk '$1 ~ "/dev/"print $4,$6'|awk 'BEGINmax=0size=$1;if(size>=max)max=size;point=$2ENDprint point')
if [ $max_dir != "/home" ] ; then 
	[ ! -d "$max_dir/fosafer" ] && mkdir $max_dir/fosafer 
	[ ! -L "/home/fosafer" ] && ln -sf $max_dir/fosafer /home/fosafer 
	max_dir="$max_dir/fosafer"
else
	[ ! -d "/home/fosafer" ] && mkdir /home/fosafer 
	max_dir="/home/fosafer"
fi

function init_yum()
[ ! -d "/etc/yum.repos.d/bak" ] && mkdir /etc/yum.repos.d/bak 
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
cat << EOF >  /etc/yum.repos.d/init.repo
[init]
name=init
baseurl=file://$file/rpm
gpgcheck=0    
enabled=1
EOF
yum clean all && \\
yum makecache 
if [ $? -eq 0 ];then
    echo -e "\\033[32m yum is ok \\033[0m"
else
    echo -e "\\033[31m yum no ok \\033[0m"
    exit
fi


#安装mysql8.0.20
function mysql_install()
rpm_mysql=`rpm -qa | grep mysql | wc -l`
rpm_mariadb=`rpm -qa | grep mariadb | wc -l`
if [[ $rpm_mysql -ge 1 ]];then
    for i in `rpm -qa | grep mysql`;do
        rpm -e --nodeps $i
    done
fi    
if [[ $rpm_mariadb -ge 1 ]];then
    for i in `rpm -qa | grep mariadb`;do
        rpm -e --nodeps $i
    done
fi

yum -y install jemalloc openssl-devel libaio 2&>/dev/null && \\
cd $max_dir && \\
#wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz && \\
tar -xf $file/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz && \\
mv mysql-8.0.20-linux-glibc2.12-x86_64 $max_dir/mysql && \\
groupadd mysql && \\
useradd -r -g mysql -s /sbin/nologin mysql && \\
mkdir -p $max_dir/mysql/data && \\
mkdir -p $max_dir/mysql/logs && \\
mkdir -p $max_dir/mysql/conf && \\
mkdir -p $max_dir/mysql/status && \\
chown -R mysql:mysql $max_dir/mysql/data/ && \\
chown -R mysql:mysql $max_dir/mysql/logs/ && \\
chown -R mysql:mysql $max_dir/mysql/status/ && \\
chmod -R 755 $max_dir/mysql/status/ && \\
chmod -R 755 $max_dir/mysql/data/ && \\
chmod -R 755 $max_dir/mysql/logs/ && \\

cat << -EOF > $max_dir/mysql/conf/my.cnf  
[mysqld]
port=3306
basedir=$max_dir/mysql
datadir=$max_dir/mysql/data
log-error=$max_dir/mysql/logs/error.log
socket=$max_dir/mysql/status/mysql.sock
user=mysql
default_authentication_plugin=mysql_native_password
skip_host_cache
skip-name-resolve=1
#skip-grant-tables
default-time_zone = '+8:00'
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

[client]
port=3306
socket=$max_dir/mysql/status/mysql.sock
-EOF

$max_dir/mysql/bin/mysqld --defaults-file=$max_dir/mysql/conf/my.cnf --initialize --user=mysql && \\

passwd=`tail -1 $max_dir/mysql/logs/error.log  | awk 'print $13' `
echo "mysql初始化密码为:$passwd"
$max_dir/mysql/bin/mysqld_safe --defaults-file=$max_dir/mysql/conf/my.cnf &


#修改mysql密码为fosafer.com
function mysql_init()
if_mysql=`netstat -ntpl | grep mysqld | wc -l`
if [[ $if_mysql -ge 1 ]];then
    $max_dir/mysql/bin/mysqladmin -uroot -p"$1" -S $max_dir/mysql/status/mysql.sock password 'fosafer.com' 
    $max_dir/mysql/bin/mysql -uroot -pfosafer.com -S $max_dir/mysql/status/mysql.sock -e "show databases" 
    if [ $? -eq 0 ];then
        echo -e "\\033[32m mysql inited \\033[0m"
    fi
else
    echo -e "\\033[31m mysql no started \\033[0m"
fi 


#安装redis5.0
function redis_install()
yum -y install lrzsz gcc  2&>/dev/null && \\
cd $max_dir && \\
#wget http://download.redis.io/releases/redis-5.0.0.tar.gz && \\
tar -xf $file/redis-5.0.0.tar.gz  && \\
cd $max_dir/redis-5.0.0 && \\
make   
make install   
cd $max_dir/redis-5.0.0 && \\
cp redis.conf redis.conf_bak  && \\
sed -i 's/bind 127\\.0\\.0\\.1/bind 0\\.0\\.0\\.0/g' redis.conf && \\
sed -i 's/protected-mode yes/protected-mode no/g' redis.conf && \\
sed -i 's/daemonize no/daemonize yes/g' redis.conf && \\
cd src/ && \\
./redis-server $max_dir/redis-5.0.0/redis.conf  
sleep 5
if_redis=`netstat -ntpl | grep redis|wc -l`
if [ $if_redis -ge 1 ];then
    echo -e "\\033[32m redis installed \\033[0m"
    #[ ! -L "/usr/local/bin/redis-server" ] && ln -s $max_dir/redis-5.0.0/src/redis-server /usr/local/bin/redis-server
    #[ ! -L "/usr/local/bin/redis-cli" ] && ln -s $max_dir/redis-5.0.0/src/redis-cli /usr/local/bin/redis-cli
    #source /etc/profile
else
    echo -e "\\033[31m redis no started \\033[0m"
fi


#安装jdk1.8
function jdk_install()
cd $max_dir && \\
tar -xf $file/jdk-8u321-linux-x64.tar.gz && \\
ln -s jdk1.8.0_321/ jdk && \\

echo "export JAVA_HOME=$max_dir/jdk" >/etc/profile.d/jdk.sh && \\
source /etc/profile.d/jdk.sh && \\
echo "export PATH=$PATH:$JAVA_HOME/bin" >> /etc/profile.d/jdk.sh && \\
source /etc/profile.d/jdk.sh  && \\
java -version 2&>/dev/null
if [ $? -eq 0 ];then
    echo -e "\\033[32m jdk installed \\033[0m"
else
    echo -e "\\033[31m jdk install error \\033[0m"
fi


#部署tomcat8.5
function tomcat_install()
cd $max_dir && \\
tar -xf $file/apache-tomcat-8.0.1.tar.gz && \\
mv apache-tomcat-8.0.1 tomcat && \\
cd $max_dir/tomcat/bin && \\
./startup.sh  2&>/dev/null && \\
sleep 5
if_tomcat=`netstat -ntpl|grep "java" | wc -l`
if [ $if_tomcat -ge 1 ];then
    echo -e "\\033[32m tomcat installed \\033[0m"
else
    echo -e "\\033[31m tomcat no started \\033[0m"
fi


#部署minio
function minio_install()
cd $max_dir && \\
tar -xf $file/minio_amd64_install.tar.gz && \\
cd minio_amd64_install && \\
bash ctl.sh install && \\
bash ctl.sh start 
sleep 5
if_minio=`netstat -ntpl | grep minio | wc -l`
if [ $if_minio -ge 1 ];then
    echo -e "\\033[32m minio installed \\033[0m"
else
    echo -e "\\033[31m minio install error \\033[0m"
fi


#部署nginx
function nginx_install()
yum -y install gcc gcc-c++ zlib-devel openssl openssl-devel 2&>/dev/null && \\
cd $max_dir && \\
tar -xf $file/nginx-1.20.2.tar.gz && \\
tar -xf $file/pcre-8.40.tar.gz && \\
cd $max_dir/pcre-8.40 && \\
./configure && make && make install && \\
cd $max_dir/nginx-1.20.2 && \\
./configure --prefix=$max_dir/nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module && \\
make && make install && \\
ln -s $max_dir/nginx/sbin/nginx /usr/local/sbin/ 
nginx
sleep 5
if_nginx=`netstat -ntpl | grep "nginx" | wc -l `
if [ $if_nginx -ge 1 ];then
    echo -e "\\033[32m nginx installed \\033[0m"
else
    echo -e "\\033[31m nginx no started \\033[0m"
fi


function init_1()
init_yum
minio_install
jdk_install
tomcat_install
nginx_install
redis_install
mysql_install


#第一次执行,进行安装服务
init_1
#最后需要手动输入一下回车键,然后修改脚本开始第二次执行即可
#第二次修改mysql密码即可
#等待上面几个函数执行完成之后,执行下面这个修改mysql密码,执行脚本后面跟初始化密码,可以使用单引号括住,以免识别密码错误,执行时要把上面的几个函数调用注释掉
#mysql_init $1


以上是关于Linux基础服务二进制一键安装shell脚本的主要内容,如果未能解决你的问题,请参考以下文章

高效率shell脚本一键安装Tomcat服务

linux一键式脚本安装node

Linux-shell脚本基础

Tomcat多实例一键部署shell脚本

Linux环境一键自动化安装oracle软件的构想(附shell脚本)

Centos7一键安装jdk1.8 shell脚本