一個自動安裝lnmp環境的shell腳本

#/bin/bash
#定義nginx源碼包版本,腳本會根據版本名稱和後綴檢查文件是否在當前目錄若是你須要安裝不一樣版本的nginx能夠修改這兩個變量 
nginx_version=nginx-1.8.0
format1=tar.gz
#定義php-fpm安裝包的文件名,若是你須要安裝其餘版本能夠更改這個變量
php_fpm_package_name=php-fpm-5.4.16-36.el7_1.x86_64.rpm
#判斷系統語言是否是中文是返回0不是返回1
language(){
    echo $LANG |grep -q zh
    [ $? -eq 0 ] && return 0 || return 1
}
#yum倉庫的錯誤輸出函數
error_yum(){
    #根據系統默認語言輸出錯誤提示
    language
    if [ $? -eq 0 ];then
        clear
        echo
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo "錯誤:本機YUM不可用,請正確配置YUM後重試."
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo
        exit
    else
        clear
        echo
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo "ERROR:Yum is disable,please modify yum repo file then try again."
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo
        exit
    fi
}
#yum倉庫檢查函數
yumcheck(){
    #清空yum緩存
    yum clean all &>/dev/null
    #獲得yum倉庫中可用rpm包個數
    repolist=$(yum repolist 2>/dev/null |awk '/repolist:/{print $2}'|sed 's/,//')
    if [ $repolist -le 0 ];then
        #若是可用軟件包數是0則調用報錯函數error_yum提示yum錯誤
        error_yum  
    fi
}
#解決nginx依賴關係
solve_depend(){
    language
    if [ $? -eq 0 ];then
        echo -en "\033[1;34m正在安裝依賴包,請稍後...\033[0m"
    else
        echo -e "\033[1;34mInstalling dependent software,please wait a moment...\033[0m"
    fi
        rpmlist="gcc pcre-devel openssl-devel zlib-devel make"
    #循環安裝nginx全部依賴包
    for i in $rpmlist
      do
        #查看該包是否安裝若是每有安裝則安裝不然跳過
        rpm -q $i &>/dev/null || yum -y install $i 
      done
}
#當前目錄找不到源碼包提示錯誤函數
error_nofile(){
    language
        if [ $? -eq 0 ];then
               clear
               echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
               echo -e "\033[1;34m錯誤:未找到[ ${1} ]軟件包,請下載軟件包至當前目錄.\033[0m"
               echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
               exit
        else
               clear
               echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
               echo -e "\033[1;34mERROR:Not found [ ${1} ] package in current directory, please download it.\033[0m"
               echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
               exit
        fi
}
#安裝nginx函數
install_nginx(){
    #調用yum倉庫檢查
    yumcheck
    #安裝nginx依賴包
    solve_depend
    grep -q nginx /etc/passwd
    #若是系統中不存在nginx這個用戶則添加一個nginx用戶
    [ $? -ne 0 ] && useradd -s /sbin/nologin nginx
    if [ -f ${nginx_version}.${format1} ];then
        tar -xf ${nginx_version}.${format1}
        cd $nginx_version
        ./configure --prefix=/usr/local/nginx --with-http_ssl_module
        make
        make install
        ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
        cd ..
    else
        #若是找不到nginx安裝文件則報錯
        error_nofile Nginx
    fi
}
#yum安裝錯誤提示函數
error_yum(){
    language
        if [ $? -eq 0 ];then
           clear
               echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
               echo -e "\033[1;34m錯誤: ${1}安裝失敗.\033[0m"
               echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
               exit
    else
           clear
               echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
               echo -e "\033[1;34merror: ${1}install error.\033[0m"
               echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
               exit

    fi
}

#安裝PHP Mariadb 相關組件函數
yum_install(){
    yum_list="php php-mysql mariadb mariadb-server mariadb-devel"
    for i in $yum_list
    do
         #查看該包是否安裝若是每有安裝則安裝不然跳過
                rpm -q $i &>/dev/null || yum -y install $i
        [ $? -nq 0 ] && error_yum $i
    done
}
#安裝php後臺程序函數
install_php_fpm(){
    if [ -f ${php_fpm_package_name} ]
    then
    rpm -q $php_fpm_package_name &>/dev/null || rpm -hiv $php_fpm_package_name
    else
    error_yum  ${php_fpm_package_name}
    fi
}
#啓動提示
start_message(){
    language
      if [ $? -eq 0 ];then
        if [ $2 -eq 0 ];then
                echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                echo -e "\033[1;34m啓動: ${1}啓動成功.\033[0m"
                echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        else
            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                echo -e "\033[1;34m啓動: ${1}啓動失敗.\033[0m"
                echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        fi
    else
         if [ $2 -eq 0 ];then
                echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                echo -e "\033[1;34mstart: ${1}start is successful.\033[0m"
                echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        else
            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                echo -e "\033[1;34mstart: ${1}start is error.\033[0m"
                echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        fi

    fi
}

#啓動lnmp環境函數
start_lnmp(){
    systemctl start php-fpm.service &>/dev/null
    netstat -tupan |grep php-fpm
    [ $? -eq 0 ] && start_message php-fpm 0 || start_message php-fpm 1
    /usr/local/nginx/sbin/nginx &>/dev/null
    netstat -tupan |grep nginx &>/dev/null
    [ $? -eq 0 ] && echo start_message nginx 0 || start_message nginx 1
    systemctl start mariadb &>/dev/null
    [ $? -eq 0 ] && start_message mariadb 0 || start_message mariadb 1
    #啓動環境結束後暫停5秒;退出程序
    sleep 5
    exit 
}
#腳本菜單函數
menu(){
    clear
    language
    if [ $? -eq 0 ];then
       echo "  ##############----Menu----##############"
       echo "# 1. 安裝lump環境"
       echo "# 2. 啓動lump環境"
       echo "# 9. 退出程序"
       echo "  ########################################"
    else
       echo "  ##############----Menu----##############"
       echo "# 1. Install lnmp"
       echo "# 2. Start lnmp"
       echo "# 9. Exit Program"
       echo "  ########################################"
    fi
}
#功能選擇函數
choice(){
    language
    if [ $? -eq 0 ];then
        read -p "請選擇一個菜單[1 2 9]:" select
    else
        read -p "Please choice a menu[1 2 9]:" select
    fi
}
#主程序開始
while :
do
    menu
    choice
    case $select in 
    1)
    install_nginx
    yum_install
    install_php_fpm;;
    2)
    start_lnmp;;
    9)
    exit;;
    *)
    echo "choice error"
    esac
done
本腳本基於CentOS7.x平臺編寫;若有錯誤請留言;謝謝!
相關文章
相關標籤/搜索