首页 > 系统相关 >linux初始化脚本(修改版)

linux初始化脚本(修改版)

时间:2022-10-18 22:14:18浏览次数:53  
标签:初始化 nginx color mysql dev echo etc 修改版 linux

修改了老版本,编译安装nginx和二进制安装mysql合到一起,里面还是存在一些问题,在修改网卡那块,

 

 

#!/bin/bash
#********************************************************************
#Author:       HEhandsome
#QQ:          2700565402
#Date:        2022-09-30
#FileName:    csh-v7.2.sh
#URL:          https://www.cnblogs.com/smlience
#email:       [email protected]
#Description: 路漫漫其修远兮,吾将上下而求索
#********************************************************************


. /etc/os-release

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "
    elif [ $2 = "failure" -o $2 = "1"  ] ;then
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo
}

disable_firewalld(){
        systemctl disable --now firewalld                                 
        color "防火墙关闭成功 " 0
}
disable_selinux(){
    sed -i s/SELINUX=enforcing/SELINUX=disabled/    /etc/selinux/config
    color "SELINUX关闭完成" 0
    reboot
}

install_software(){
yum -y install bash-completion psmisc lrzsz  tree man-pages redhat-lsb-core wget tcpdump ftp rsync vim lsof net-tools  iproute git  
}

set_yum_rocky8(){
    [ ! -d /data/backup ] && mkdir -p /data/backup
    mv /etc/yum.repos.d/*  /data/backup
    mkdir /mnt/cdrom
    mount /dev/sr0 /mnt/cdrom
    cat > /etc/yum.repos.d/base.repo <<EOF
[BaseOS]
name=mnt/cdrom
baseurl=file:///mnt/cdrom/BaseOS
             https://mirrors.aliyun.com/rockylinux/\$releasever/BaseOS/x86_64/os/
             http://mirrors.163.com/rocky/\$releasever/BaseOS/x86_64/os/
             https://mirrors.nju.edu.cn/rocky/\$releasever/BaseOS/x86_64/os/
             https://mirrors.sjtug.sjtu.edu.cn/rocky/\$releasever/BaseOS/x86_64/os/
             http://mirrors.sdu.edu.cn/rocky/\$releasever/BaseOS/x86_64/os/
gpgcheck=0


[AppStream]
name=mnt/cdrom
baseurl=file:///mnt/cdrom/AppStream
             https://mirrors.aliyun.com/rockylinux/\$releasever/AppStream/x86_64/os/
             http://mirrors.163.com/rocky/\$releasever/AppStream/x86_64/os/
             https://mirrors.nju.edu.cn/rocky/\$releasever/AppStream/x86_64/os/
             https://mirrors.sjtug.sjtu.edu.cn/rocky/\$releasever/AppStream/x86_64/os/
             http://mirrors.sdu.edu.cn/rocky/\$releasever/AppStream/x86_64/os/
gpgcheck=0

[extras]
name=extras
baseurl=https://mirrors.aliyun.com/rockylinux/\$releasever/extras/x86_64/os
        http://mirrors.163.com/rocky/\$releasever/extras/x86_64/os
        https://mirrors.nju.edu.cn/rocky/\$releasever/extras/x86_64/os
        https://mirrors.sjtug.sjtu.edu.cn/rocky/\$releasever/extras/x86_64/os
        http://mirrors.sdu.edu.cn/rocky/\$releasever/extras/x86_64/os
gpgcheck=0
EOF

    yum clean all
    yum makecache
    yum repolist
if [ $? -eq 0 ];then    
    color "yum源配置完毕 " 0    
else
        color "yum源配置失败 " 1     
        exit
fi
    cat >> /etc/fstab << EOF
/dev/sr0               /mnt/cdrom              iso9660  defaults        0 0
EOF
}

set_epel_rocky8(){
 cat > /etc/yum.repos.d/epel.repo <<-EOF
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/\$releasever/Everything/x86_64/
        https://mirror.tuna.tsinghua.edu.cn/epel/\$releasever/Everything/x86_64/
        https://mirrors.cloud.tencent.com/epel/\$releasever/Everything/x86_64/
        https://mirrors.huaweicloud.com/epel/\$releasever/Everything/x86_64/
gpgcheck=0
EOF
    dnf clean all &> /dev/null
    dnf repolist &> /dev/null
if [ $? -eq 0 ];then    
     color "EPEL源设置完成!" 0
else
     color "EPEL源设置失败!" 1
fi
}


set_yum_centos7(){
    [ ! -d /data/bak ] && mkdir -p /data/bak
        mv /etc/yum.repos.d/*  /data/bak
        mkdir /mnt/cdrom
        mount /dev/sr0 /mnt/cdrom
    cat > /etc/yum.repos.d/base.repo <<EOF
[BaseOS]
name=mnt/cdrom
baseurl=file:///mnt/cdrom
https://mirrors.aliyun.com/centos/\$releasever/os/x86_64/
gpgcheck=0


[AppStream]
name=mnt/cdrom
baseurl=file:///mnt/cdrom    
gpgcheck=0

[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/x86_64/
gpgcheck=0
EOF
        yum clean all
        yum makecache
        yum repolist
if [ $? -eq 0 ];then     
         color "yum源配置完毕 " 0
else
    color "yum源配置失败 " 0
    exit
fi
}


set_epel_centos7(){
cat > /etc/yum.repos.d/epel.repo <<-EOF
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/\$releasever/x86_64/
gpgcheck=0
EOF
    yum clean all &> /dev/null
    yum repolist &> /dev/null
if [ $? -eq 0 ];then
    color "EPEL源设置完成!"  0
else
    color "EPEL源设置失败!"  1
    exit
fi
}

revise_IP(){

read -p "输入网卡名: " NET
read -p "输入ip: " IP
cat > /etc/sysconfig/network-scripts/ifcfg-$NET <<EOF
NAME=$NET
DEVICE=$NET
IPADDR=$IP
BOOTPROTO=none
PREFIX=24
GATEWAY=10.0.0.2
ONBOOT=yes
DNS1=10.0.0.2
DNS2=8.8.8.8
EOF
nmcli con reload
nmcli con up $NET
if [ $? -eq 0 ];then
    color "网卡配置完成 " 0
else
        color "网卡配置失败 " 1
        exit
fi    
}

set_alias(){
cat >> ~/.bashrc <<EOF
alias cdnet="cd /etc/sysconfig/network-scripts/"
EOF
exec bash
if [ $? -eq 0 ];then
        color "别名修改成功 " 0
else
    color "别名修改失败 " 1
        exit
fi
}

set_PS1(){
if [ $ID = 'centos' -o $ID = 'rocky' ];then
    echo  "PS1='\[\e[1;36m\][\[\e[34m\]\u\[\e[35m\]@\[\e[32m\]\h\[\e[31m\]\W\[\e[36m\]]\\$\[\e[0m\]'" >> /etc/bashrc
    exec bash
else
    echo "PS1='\[\e[1;35m\][\[\e[35m\]\u\[\e[35m\]@\[\e[35m\]\h \[\e[36m\]\W\[\e[35m\]]\\$\[\e[0m\]'" >> ~/.bashrc
    exec bash
fi
}

set_host(){
read -p "请输入主机名: " HOST
hostnamectl set-hostname $HOST
exec bash
    color "修改完成"  0
}

set_netname1(){
sed -i '/^GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#'  /etc/default/grub
    grub2-mkconfig -o /etc/grub2.cfg
if [ $? -eq 0 ];then    
    color  "配置完成,重启生效" 0
else
    color  "配置失败" 1
fi
}


set_netname2(){
sed -i '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#'  /etc/default/grub
        grub-mkconfig -o /boot/grub/grub.cfg
if [ $? -eq 0 ];then
     color  "配置完成,重启生效" 0
else
         color  "配置失败" 1
fi
}

set_vim(){
cat >>~/.vimrc<<EOF
set ignorecase
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#********************************************************************")
call setline(3,"#Author:      HE-handsome")
call setline(4,"#QQ:         2700565402")
call setline(5,"#Date:       ".strftime("%Y-%m-%d"))
call setline(6,"#FileName:   ".expand("%"))
call setline(7,"#email:      [email protected]")
call setline(8,"#URL:        https://www.cnblogs.com/smlience")
call setline(9,"#Description:路漫漫其修远兮,吾将上下而求索")
call setline(10,"#********************************************************************")
call setline(11,"")
endif
endfunc
autocmd BufNewFile * normal G
EOF
    color  "设置完成" 0
}

ntp(){
rm -f /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai   /etc/localtime
echo `date`
}



version=5.7.36
URL=https://downloads.mysql.com/archives/get/p/23/file/mysql-${version}-linux-glibc2.12-x86_64.tar.gz
DATA_DIR=/data/mysql

install_mysql(){
    echo -e "\e[1;36m 安装依赖包 \e[0m"
    if [ $ID == "rocky" -o $ID == "centos" ];then
        yum -y install libaio numactl-libs ncurses-compat-libs
    else
        apt update &> /dev/null
        apt -y install libaio numactl-libs ncurses-compat-libs
    fi

    if id mysql &> /dev/null;then
        echo -e "\e[1;36m 用户已存在 \e[0m"
    else
        groupadd -g 336 -r mysql && useradd -g mysql -s /sbin/nologin -d /data/mysql -r -u 336 mysql
        echo -e "\e[1;36m MySQL用户创建完成 \e[0m"
    fi

    if [ -e mysql-${version}-linux-glibc2.12-x86_64.tar.gz ];then
        echo -e "\e[1;36m 文件已存在,准备安装... \e[0m"
    else
        echo -e "\e[1;36m 开始下载源文件... \e[0m"
    wget $URL
    fi
    echo -e "\e[1;36m 正在解压源文件... \e[0m" 
    tar xvf mysql-${version}-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/
    echo -e "\e[1;36m 解压完成! \e[0m"
    sleep 1
    echo -e "\e[1;36m 创建软链接 \e[0m"    
    ln -s /usr/local/mysql-${version}-linux-glibc2.12-x86_64/ /usr/local/mysql
    chown -R root.root /usr/local/mysql/
    sleep 1
    echo -e "\e[1;36m 建立环境变量 \e[0m"
    echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh 
    . /etc/profile.d/mysql.sh
    cp /etc/my.cnf{,.bak}
    cat > /etc/my.cnf <<EOF
[mysqld]
datadir=$DATA_DIR
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
    [ ! -d /data/mysql ] && mkdir -p /data/mysql
    mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
    sleep 1
    echo -e "\e[1;36m 复制系统service文件 \e[0m"
    cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
    chkconfig --add mysqld
    service mysqld start
    echo -e "\e[1;36m 可以通过systemctl启动mysqld \e[0m"
    [ $? -eq 0 ] && echo -e "\e[1;36m 数据库启动成功 \e[0m" || { echo -e "\e[1;36m 数据库启动失败 \e[0m";exit; }
    echo -e "\e[1;5;35m 现在是空密码,需要自己修改密码 \e[0m"
    echo -e "\e[1;5;35m 修改密码命令mysqladmin -uroot password 新密码 \e[0m"

exec bash

}

install_mariadb(){
    yum -y insatll mariadb-server || systemctl start mariadb.service
    systemctl status mariadb.service
    echo -e "修改二进制日志位置"
    sleep 2
    mkdir -p /data/mysql/logbin
        chown -R mysql.mysql /data/mysql
    cat >> /etc/my.cnf <<EOF
[mysqld]
log_bin=/data/mysql/logbin/mariadb-bin
EOF
    color  "修改二进制位置成功"  0

}

install_nginx(){
version=1.20.2
    if [ -e nginx-${version}.tar.gz ];then
    echo "文件存在,安装进行中。。。"
    else
        echo "开始下载源码文件。。。"
    wget http://nginx.org/download/nginx-${version}.tar.gz
    [ $? -eq 0 ] || { echo "下载失败!立即退出!";exit; }
    fi

    if id nginx &> /dev/null;then
        echo "用户存在"
    else
        groupadd -g 990 -r nginx && useradd -g nginx -s /sbin/nologin -r -u 990 nginx
        echo "创建nginx用户"
    fi
    
    echo "安装依赖包"
    if [ $ID == "rocky" -o $ID == "centos" ];then
        yum -y install make  gcc gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
    else
        apt update &> /dev/null
    apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null
    fi

    tar xf nginx-${version}.tar.gz -C /usr/local/src
    cd /usr/local/src/nginx-${version}
    ./configure --prefix=/apps/nginx \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --with-pcre \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_realip_module
    make && make install
    [ $? -eq 0 ] && color "nginx编译" 0 ||  { color "nginx编译" 1 ;exit; }
    chown -R nginx.nginx /apps/nginx
    ln -s /apps/nginx/sbin/nginx /usr/sbin/

    cat > /lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target


[Service]
Type=forking
#PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
LimitNOFILE=100000


[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null
    [ $? -eq 0 ] && color "nginx启动 " 0 || { color "nginx启动" 1 ;exit; }
}

set_all(){
while :;do
    echo -e  "\E[$[RANDOM%7+31];1m"
    cat << EOF
***************************************************************************
*****         路漫漫其修远兮                       ************************
***                 吾将上下而求索                  ******************* 
*****        初始化脚本菜单                       *************************    
**********                                                     ************
**********                                                     ************
**********   1.关闭防火墙                                      ************
**********   2.关闭selinux                                     ************
**********   3.安装初始化软件                                  ************
**********   4.安装yum源适合rocky8.5                           ************
**********   5.安装yum源适合centos7                            ************
**********   6.修改网卡                                        ************
**********   7.修改别名                                        ************
**********   8.修改主机名颜色                                  ************
**********   9.修改主机名                                      ************
**********   10.修改网卡名为eth0适合redhat/centos/Rocky        ************
**********   11.修改网卡名为eth0适合ubuntu                     ************
**********   12.安装yum_epel适合rocky8                         ************
**********   13.安装yum_epel适合centos7                        ************
**********   14.设置vim                                        ************ 
**********   15.时间同步                                       ************
**********   0.退出                                            ************
**********                                                     ************
***************************************************************************       
EOF
    echo -e "\E[0m"

read -p "$(echo -e '\e[1;36m 请输入序号: \e[0m')" Menu 
case $Menu in


1)    disable_firewalld
    ;;

2)    disable_selinux
    ;;

3)      install_software
        ;;    

4)    set_yum_rocky8
    ;;

5)    set_yum_centos7
    ;;

6)    revise_IP
    ;;

7)    set_alias
    ;;

8)    set_PS1
    ;;

9)    set_host
    ;;

10)    set_netname1
    ;;

11)    set_netname2
    ;;    

12)     set_epel_rocky8
    ;;

13)     set_epel_centos7
    ;;

14)     set_vim
        ;;

15)    ntp
       ;;

0)    exit
    ;;

esac
done
}

install_service(){
    while :;do
    echo -e "\E[$[RANDOM%7+31];1m"
    cat << EOF
*****************************************************
***        路漫漫其修远兮                ***********
******              吾将上下而求索            *****
********      初始化linux脚本             ***********
********                                  ***********
********      1.安装MySQL                 ***********
********      2.安装mariadb              ***********
********      3.安装dns                   ***********
********      4.安装nginx                 ***********
********      0.退出                      ***********
********                                  ***********
*****************************************************

EOF
    echo -e "\E[0m"



read -p "$(echo -e '\e[1;36m 请输入序号:  \e[0m')" Menu

case $Menu in

1)    install_mysql
    ;;

2)    install_mariadb
    ;;

3)      install_dns
        ;;

4)     install_nginx
    ;;

0)    exit
    ;;
esac
done
}


while :;do
    echo -e "\E[$[RANDOM%7+31];1m"
    cat << EOF
*****************************************************
***         路漫漫其修远兮                ***********
******                吾将上下而求索            *****
*********       初始化linux脚本           ***********
*********                                 *********** 
*********       1.系统设置                ***********
*********       2.安装服务                *********** 
*********       0.退出                    ***********
*********                                 *********** 
*****************************************************

EOF
    echo -e "\E[0m"

read -p "$(echo -e '\e[1;34m   输入选项:    \e[0m')" option
case $option in

1)
    set_all
    ;;

2)    install_service
    ;;

0)     break
    ;;    
esac
done

 

标签:初始化,nginx,color,mysql,dev,echo,etc,修改版,linux
From: https://www.cnblogs.com/smlience/p/16804373.html

相关文章

  • linux 内核编译问题汇总
    一、编译设备树时找不到设备树包含的头文件设备树包中包含的头文件会到kernel/scripts/dtc/include-prefixes/dt-bindings目录下去查找(新版本内核),而dt-bindings目录......
  • C语言基础-数组得初始化
    #include<stdio.h>intmain(){inta[10];intsize=sizeof(a)/sizeof(a[0]);//计算数组得大小for(inti=0;i<size;i++){a[i]=i*100;......
  • linux基础命令
    转载:https://www.cnblogs.com/wwjjll/p/16804230.html虚拟机关键配置名词解释远程链接工具xshell基本使用linux命令准则系统运行命令常用快捷方式文件命令操作快......
  • linux基础
    今日内容概要虚拟机关键配置名词解释远程链接工具xshell基本使用linux命令准则系统运行命令常用快捷方式文件命令操作快照功能文件编辑命令文件编辑异常情况......
  • SpringMVC执行流程-2、DispatcherServlet初始化过程
    DispatcherServlet初始化过程DispatcherServlet本质上是一个Servlet,所以天然的遵循Servlet的生命周期。所以宏观上是Servlet生命周期来进行调度。  a>初始化We......
  • Linux命令每天都要使用,但又太长记不住怎么办?教你1个方法
    序言各位好啊,我是会编程的蜗牛,作为java开发者,我们肯定会与linux服务器打交道,关于linux服务器的连接工具,可以参考我的文章Tabby,一款老外都在用的 SSH工具,竟然还支持网页操......
  • linux网络 PXE批量安装 Kickstart无人值守安装
    PXE批量网络装机PXE(预启动执行环境,在操作系统之前运行)是由Intel公司开发的网络引导技术,工作在Client/Server模式,允许客户机通过网络从远程服务器下载引导镜像,并加载安装......
  • golang中的init初始化函数
    0.1、索引https://waterflow.link/articles/16660905308801、概念1.1、源文件里的代码执行顺序init函数是用于初始化应用程序状态的函数。它不接受任何参数并且不返回......
  • golang中的init初始化函数
    0.1、索引https://waterflow.link/articles/16660905308801、概念1.1、源文件里的代码执行顺序init函数是用于初始化应用程序状态的函数。它不接受任何参数并且不返......
  • Linux中僵尸进程是什么意思?怎么查看僵尸进程?
    进程就是系统运行中的程序。是正在执行的一个程序或者命令,每一个进程都是一个运行的实体,都有自己的地址空间,并占用一定的系统资源。说起进程,那么你知道Linux中僵尸进程......