原文笔记链接请点击文章尾部链接:
  最近闲来无事,利用周末在家时间,写了个一键源码安装.现在无需一个一个的输入命令,无需值守,编译安装优化编译参数,提高性能,解决不必要的
  软件间依赖.
#!/bin/bash 
#Version:1.0
# USER        YYYY-MM-DD - ACTION
# bruce fu      20017-08-08 - Created
# mail   270064522@qq.com
#The software list:Nginx,MySQL,PHP,Memcached,Tomcat,Java. 
#This script can automatically install all software on your machine.
script_description(){
echo "+-----------------------------------------------------------------------+"
echo "|            Addons script for LNMP V1.0, Written by Baojia             |"
echo "+-----------------------------------------------------------------------+"
echo "|        The software list:Nginx,Mysql,PHP,Memcached,Tomcat,Java.       |"
echo "+-----------------------------------------------------------------------+"
echo "|            Hope to work with you to optimize this Script.             |"
echo "+-----------------------------------------------------------------------+"
echo "|                       devops1995@gmail.com                            |"
echo "+-----------------------------------------------------------------------+"
}   
version(){
    version_systemctl=`cat /etc/redhat-release |grep 6|awk '{print $3}'|awk -F '[.]' '{print $1}'`
    language    if [ $? -eq 0 ];then
        if [  ${version_systemctl} -ne 6 ];then
            echo "对不起,当前脚本只适用于centOS6.x"
            exit
        fi
    else
        if [ ${version_systemctl} -ne 6 ];then
            echo "Sorry, the current script only applies to centOS6.x."
            exit
        fi
    fi}  
   
#Determine the language environment(确定语言环境) 脚本会检测目标主机的语音环境,如果目标主机运行中文环境,则脚本运行中的所有提示信息均为中文,反之则提示信息为英文。language(){ 
 echo $LANG |grep -q zh   #显示当前字符集,grep过滤zh关键字 -q 不现实任何信息
 if [ $? -eq 0 ];then #如果是zh说明是中文字符集,返回0.
  return 0 #r如果返回的0,说明是zh字符集,所以我们这里返回0.
 else  #否则如果过滤到的不是zh,那么上面的返回的就是1,于是就会执行else代码块.
  return 1 
 fi} 
 
 
 
#Define user selection menu. (定义用户选择菜单)menu(){     #菜单
 clear #清除当前屏幕所有信息
 version    #判断当前Server版本
 language     #调用language函数判断当前系统字符集而选择对应的字符提示.
 if [ $? -eq 0 ];then #判断返回值是0就是中文哦,我们上面grep -q zh 过滤的结果
    echo "  ##############----Menu----##############"
    echo "# 1. 安装Nginx"
    echo "# 2. 安装MySQL"
    echo "# 3. 安装PHP"
    echo "# 4. 安装Memcached"
    echo "# 5. 安装Java"
    echo "# 6. 安装Tomcat"
    echo "# 7. 安装以上所有软件"
    echo "# 8. 退出程序"
    echo "  ########################################"
 else      #不是中文,那么就执行英文的哈
    echo "  ##############----Menu----##############"
    echo "# 1. Install Nginx"
    echo "# 2. Install MySQL"
    echo "# 3. Install PHP"
    echo "# 4. Install Memcached"
    echo "# 5. Install Java"
    echo "# 6. Install Tomcat"
    echo "# 7. Install all above"
    echo "# 8. Exit Program"
    echo "  ########################################"
 fi}#Read user's choice  (读取用户的选择) #Read User's INPUT (读取用户的输入)choice(){ #定义一个函数
 language #调用language函数,判断用户当前环境是zh还是其他环境
 if [ $? -eq 0 ];then     #read -p 指定读取值时的提示符  ,read 命令后面 select是变量名,读取的数据将被自动复制给这个变量.
  read -p "请选择一个菜单[1-8]:" select else
  read -p "Please choice a menu[1-8]:" select fi} 
  
rotate_line(){ 
#当执行一个比较费时的操作时,如果能在终端显示进度指示器,则对用户来说是比较有好的。#下面的代码展示了如何达到这种效果。#函数rotate_line是一个死循环,会在终端每隔一秒交替显示不同的字符(-|/)。#主程序调用rotate_line,使其在后台执行,用$!获得其进程ID号;#接下来调用比较费时的操作,这里简单模拟一下,sleep 30秒,#当费时的操作执行完之后,就杀掉在后台执行的rotate_line,操作完成。
 
 INTERVAL=0.1 
 TCOUNT="0"
 while : #while 死循环
 do
  TCOUNT=`expr $TCOUNT + 1` #expr 是表达式计算工具,+ - \* / % (加减乘除取余) 
  case $TCOUNT in       #case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令。
    "1") 
          echo -e '-'"\b\c" #-e 激活转义字符,-b 删除前一个字符, -c 最后不加上换行符号.
          sleep $INTERVAL 
          ;; 
    "2") 
          echo -e '\\'"\b\c"
          sleep $INTERVAL 
          ;; 
    "3") 
          echo -e "|\b\c"
          sleep $INTERVAL 
          ;; 
    "4") 
          echo -e "/\b\c"
          sleep $INTERVAL 
          ;; 
    *) 
          TCOUNT="0";; 
  esac
 done}#Install failed error messages.(安装失败的错误消息) error_install(){     #定义一个安装错误函数
 language #调用language函数,判断当前字符集.
 if [ $? -eq 0 ];then
    clear     echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
    echo -e "\033[1;31m错误:编译安装[ ${1} ]失败! \033[0m"   #更改字体颜色!
    echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
       exit
 else
      clear     echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
      echo -e "\033[1;31mERROR:Install[ ${1} ]Failed!\033[0m"
   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    exit
 fi} 
 
 
#Couldn't use yum tool  #不能使用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} 
 
 
#Test target system whether have yum repo. #测试当前系统是否有yum 源#Return 0 dedicate yum is enable. #返回0yum源是启用。 #Return 1 dedicate yum is disable. #返回1yum源是禁用。test_yum(){ 
#set yum configure file do not display Red Hat Subscription Management info. (设置yum配置文件不显示红帽订阅管理信息。)#sed -i '/enabled/s/1/0/' /etc/yum/pluginconf.d/subscription-manager.conf #关闭Redhat自带的插件subscription-manager.
 yum clean all &>/dev/null   #清除缓存目录下的软件包及旧的headers
 repolist=$(yum repolist 2>/dev/null |awk '/repolist:/{print $2}'|sed 's/,//') #yum repolist:查看当前的可用的yum仓库。  如果yum源库创建成功,会显示其库的大小,状态值不为0;#[root@mac init.d]# yum repolist 2>/dev/null |awk '/repolist:/'#repolist: 19,567   #其实就是要看看这个库的总大小们这里我们会取到19567
 if [ $repolist -le 0 ];then #repolist的结果如果小于等于0,那么肯定是有问题,所以我们这里就调用error_yum函数.
  error_yum 
 fi} 
 
 
#This function will check depend software and install them. (此功能将检查依赖软件并安装它们。)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
 
 case $1 in
  nginx) 
          rpmlist="gcc pcre-devel openssl-devel zlib-devel make"
          ;; 
  cmake) 
          rpmlist="gcc gcc-c++ make"
          ;; 
  mysql) 
          rpmlist="ncurses-devel"
          ;; 
  mhash) 
          rpmlist="mhash mcrypt"
          ;; 
  libmcrypt) 
          rpmlist="libmcrypt-devel"
          ;; 
  php) 
          rpmlist="gcc zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel libmcrypt-devel mhash mcrypt freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel"
          ;; 
  libevent) 
          rpmlist=" libevent libevent-devel nc telnet"
          ;; 
  memcached) 
          rpmlist="gcc"
          ;; 
  memcache) 
          rpmlist="autoconf"
          ;; 
  java) 
          rpmlist="glibc.i686"
 esac#for 循环上述rpmlist变量里的依赖软件包名字
 for i in $rpmlist 
 do
     rpm -q $i &>/dev/null       #先用rpm 装,如果失败则调用yum 安装.
     if [ $? -ne 0 ];then     #-ne  //不等于
        yum -y install $i &>/dev/null     fi
 done} 
 
 
#Determing how to uncompress a tar file. (确定如何解压缩tar文件)untar(){ 
 type=$(file $1 |awk '{print $2}')    #使用file 命令判断文件的格式,如果是gzip格式那么就执行以下.
 if [ "$type" == "gzip" ];then
  language #引用判断当前字符集函数
  if [ $? -eq 0 ];then
      echo -e "\033[1;34m正在解压$1,请稍后...\033[0m"
  else
      echo -e "\033[1;34mUncompress $1,Please wait a moment...\033[0m"
  fi
  tar -xzf $1 
 elif [ "$type" == "bzip2" ];then #如果是bzip2 则使用以下代码块进行解压
  language 
  if [ $0 -eq 0 ];then
      echo -e "\033[1;34m正在解压$1,请稍后...\033[0m"
  else
      echo -e "\033[1;34mUncompress $1,Please wait a moment...\033[0m"
  fi
  tar -xjf $1 
 else      #如果是其他的压缩格式则报错.
  language 
  if [ $? -eq 0 ];then
          clear         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          echo -e "\033[1;34m错误:未知的压缩包类型.\033[0m"
          echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          exit
  else
          clear         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          echo -e "\033[1;34mERROR:Unknow compress file type.\033[0m"
          echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          exit
  fi
 fi } 
 
 
#Display a begin mesages (显示开始讯息)begin(){ #开始函数
 language 
 if [ $? -eq 0 ];then
          clear         echo -e "\033[1;36m----------------------------------\033[0m"
          echo -e "\033[1;32m\t现在开始安装${1}!\033[0m"
          echo -e "\033[1;36m----------------------------------\033[0m"
 else
          clear         echo -e "\033[1;36m-----------------------------------\033[0m"
          echo -e "\033[1;32m\tInstall ${1} Now!\033[0m"
          echo -e "\033[1;36m-----------------------------------\033[0m"
 fi} 
 
 
#If not found the software package, this script will be exit. (如果没有找到软件包,这个脚本将退出。)error_nofile(){ 
 language 
        if [ $? -eq 0 ];then
              clear              echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              echo -e "\033[1;34m错误:未找到[ ${1} ]软件包,请下载软件包至${install_package_dir}目录.\033[0m"
              echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              exit
        else
              clear              echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              echo -e "\033[1;34mERROR:Not found [ ${1} ] package in ${install_package_dir} directory, please download it.\033[0m"
              echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              exit
        fi} 
 
 
#Display a hint for configure (显示配置提示)configure_info(){ 
 language 
           if [ $? -eq 0 ];then
                    echo -e "\033[1;34m正在检测编译环境并生成编译文件,请稍后...\033[0m"
            else
                    echo -e "\033[1;34mCheck system environment and configure,Please wait a moment...\033[0m"
            fi} 
 
 
#Display a hint for make and make install (显示make和make install的提示) make_info(){ 
 language 
            if [ $? -eq 0 ];then
                    echo -e "\033[1;34m正在编译安装软件,请稍后...\033[0m"
            else
                    echo -e "\033[1;34mComplie and install software,Please wait a moment...\033[0m"
            fi} 
 
 
#Install cmake to complie MySQL (安装cmake来编译mysql)install_cmake(){ 
        test_yum        #测试yum是否可用
        solve_depend cmake #调用solve_depend解决依赖的函数并赋值 cmake 给他,于是就是自动安装cmake的依赖.
        cd ${install_package_dir}
        if [ -f ${cmake_version} ];then #if [ -f ] 判断是否为文件,是则为真.这里的版本号和后缀是我们前面定义函数给的哦.
                untar ${cmake_version} #调用untar函数来解压.
                cmake_dir=`ls -d */|grep cmake` #找到cmake目录
                cd ${cmake_dir} #进入解压后的cmake目录
                configure_info #调用显示配置信息的函数configure_info#主程序调用rotate_line,使其在后台执行,用$!获得其进程ID号;#接下来调用比较费时的操作,当费时的操作执行完之后,#就kill -9 PID杀掉在后台执行的rotate_line,操作完成。
 
                rotate_line &  #只要在命令的尾部加上符号&,启动的进程就会成为"后台任务"。如果要让正在运行的"前台任务"变为"后台任务",可以先按ctrl + z,然后执行bg命令(让最近一个暂停的"后台任务"继续执行)。
                  disown $! # $!表示Shell最后运行的后台Process的PID
                ./bootstrap  --prefix=/usr/local/cmake &>/dev/null
                  result=$? 
                  kill -9 $! 
                make_info 
                language 
                if [ $? -eq 0 ];then
                        echo -e "\033[1;32m正常情况下软件被安装在/usr/local/cmake目录.\033[0m"
                else
                        echo -e "\033[1;32mSoftware will be installed to /usr/local/cmake.\033[0m"
                fi
                if [ $result -eq 0 ];then
                      rotate_line & 
                      disown $! 
                    make &> /dev/null && make install &>/dev/null
                      result=$? 
                      kill -9 $! 
                      cd .. 
                        if [ $result -ne 0 ];then
                            error_install cmake 
                        fi
                fi
        else
                error_nofile cmake 
        fi}install_mhash(){ #安装MHASH函数#mhash是基于离散数学原理不可逆向的PHP加密方式扩展库,其在默认情况下不会开启。#mhash可以用于创建校验数值、消息摘要、消息认证码,#以及无需原文的关键信息保存(如密码)等。它为PHP提供了多种散列算法,如MD5、SHA1、GOST等。#可以通过MHASH_hashname()查看其支持的算法有哪些。# 不过,需要注意的是:# ?该扩展不能提供最新的散列算法。#   ?该扩展结果原则上运算不可逆。#同理,我们选择更简单的yum安装方法来安装mhash。安装PHP需要的相关包mhash的命令如下:
 solve_depend mhash   #安装软件的依赖函数
      if [ $? -ne 0 ];then
          error_install mhash 
      fi} 
 
install_libmcrypt(){ 
#这是一个使用动态加载的模块化的libmcrypt。libmcrypt对于在程序运行时添加/移除算法是有用的。 #limbcrypt-nm目前不再被官方支持,#其软件地址为http://mcrypt.hellug.gr/lib/,编译PHP的过程中,libmcrypt库不是必须要安装的包。#很多同学采用的是很复杂的编译安装libmcrypt的方法,这里我们选择更简单的yum安装方法。 #在CentOS默认的yum源里没有libmcrypt-devel,因此需要事先配置epel第三方yum源,具体命令如下:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
solve_depend libmcryptif [ $? -ne 0 ];then
    errot_nofile libmcryptfi} 
 
#Install libevent 安装libeventinstall_libevent(){ 
#nc 和telnet 是客户端工具,libevent是事件处理机制。
 solve_depend libevent 
 if [ $? -ne 0 ];then
      error_nofile libevent 
 fi} 
 
#Install Nginx 安装nginx install_nginx(){ 
 begin nginx 
 test_yum 
 solve_depend nginx 
#查看是否有nginx用户
 grep -q nginx /etc/passwd#没有则创建 nginx用户
 if [ $? -ne 0 ];then
    useradd nginx -s /sbin/nologin -M #-s指定用户登录SHELL,这里是禁止用户登录,-M:不创建家目录。
 ficd ${install_package_dir}
 if [ -f ${nginx_version} ];then
          untar ${nginx_version}
          nginx_dir=`ls -d */|grep nginx` #找到nginx目录
          cd $nginx_dir
          configure_info 
          rotate_line & 
          disown $!
          ./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module >/dev/null#编译Nginx软件时,可以使用./configure--help 查看相关参数帮助。#下面是本次编译时指定的参数及简单说明:#--prefix=PATH set installation prefix#设置安装路径#--user=USER set non-privileged user for worker processes #进程用户权限#--with-http_stub_status_module enable ngx_http_stub_status_module#激活状态信息。#--with-http_ssl_module enable ngx_http_ssl_module #激活ssl功能#提示:Nginx的大部分模块功能都会默认编译到软件中,不需要单独指定编译参数。
          result=$? 
          kill -9 $!
          make_info
          language      if [ $? -eq 0 ];then
          echo -e "\033[1;32m正常情况下软件被安装在/usr/local/nginx目录.\033[0m"
      else
          echo -e "\033[1;32mSoftware will be installed to /usr/local/nginx.\033[0m"
      fi
      if [ $result -eq 0 ];then
          rotate_line & 
          disown $! 
       make &>/dev/null && make install &>/dev/null
          result=$? 
          kill -9 $! 
          cd .. 
          if [ $result -ne 0 ];then
            error_install nginx 
          fi
      fi
 else
  error_nofile Nginx 
 fi} 
 
 
#Install MySQL 安装MySQL install_mysql(){ 
 begin mysql #提示正在安装MySQL
 test_yum     #测试yum 是否可以
 install_cmake  #安装cmake
 solve_depend mysql   #安装MySQL依赖 #判断mysql用户是否存在不存在即创建
 grep -q mysql /etc/passwd  #判断MySQL用户是否存在,不存在即创建MySQL用户.
 if [ $? -ne 0 ];then
     useradd -s /sbin/nologin mysql -M ficd ${install_package_dir}
 if [ -f ${mysql_version} ];then
       untar ${mysql_version}
       mysql_dir=`ls -d */|grep mysql` #找到mysql目录
          cd ${mysql_dir} 
          configure_info 
          rotate_line & 
          disown $!
          /usr/local/cmake/bin/cmake . &>/dev/null
          result=$? 
          kill -9 $! 
          make_info 
          language 
            if [ $? -eq 0 ];then
                echo -e "\033[1;32m正常情况下软件被安装在/usr/local/mysql目录.\033[0m"
            else
                echo -e "\033[1;32mSoftware will be installed to /usr/local/mysql.\033[0m"
            fi
 
            if [ $result -eq 0 ];then
                  rotate_line & 
                  disown $! 
                make &> /dev/null && make install &>/dev/null
                  result=$? 
                  kill -9 $!                  if [ $result -ne 0 ];then
                    error_install MySQL 
                  fi
                  cd .. 
            fi 
              language 
                  if [ $? -eq 0 ];then
                      echo -e "\033[1;34m正在初始化数据库...\033[0m"
                  else
                      echo -e "\033[1;34mInitialization database...\033[0m"
                  fi#初始化MySQL
  /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql/ &>/dev/null
  chown -R mysql.mysql /usr/local/mysql
  chown -R mysql /usr/local/mysql/data#生成简单的/etc/init.d/mysqld 就可以启动和关闭MySQL
  /bin/cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
  chmod +x /etc/init.d/mysqld
  /bin/cp -f /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf 
#ldconfig 命令的用途主要是搜索/etc/ld.so.cpnf(动态库配置文件) or /lib or /usr/lib内所列的目录下,#搜索出克共享的动态链接库(格式如lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件.#缓存文件默认为/etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表,为了让动态链接库为系统所共享,#需运行动态链接库的管理命令ldconfig,此执行程序存放在/sbin目录下.
  echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf 
#ldconfig通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行安装这个命令.
  ldconfig 
#把mysql的路径加入环境变量cat <> /etc/profile 
PATH=\$PATH:/usr/local/mysql/bin/export PATH 
EOF#重新加载环境变量./etc/profile#设置开机自启动chkconfig --add mysqld
chkconfig --list mysqld else
   error_nofile mysql 
 fi }#Install PHP 安装PHP install_php(){ 
 begin php 
 test_yum 
 install_mhash 
 install_libmcrypt 
 solve_depend php#安装yum无法安装的libiconv库
 wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
 tar zxf libiconv-1.14.tar.gz cd libiconv-1.14
 ./configure --prefix=/usr/local/libiconv
  
      rotate_line & 
      disown $! 
    make &>/dev/null && make install &>/dev/null    return=$?
    language    if [ $? -eq 0 ];then
        if [ $return -ne 0 ];then
            echo "libiconv库编译失败."
        else
            echo "libiconv库编译成功."
        fi
    else
        if [ $return -ne 0 ];then
            echo "Libiconv library failed to compile."
        else
            echo "The libiconv library was compiled successfully."
        fi
    fi
    result=$? 
      kill -9 $!  
       
 cd ../#开始解压编译安装PHPcd ${install_package_dir}
 if [ -f ${php_version} ];then
          untar ${php_version}
          php_dir=`ls -d */|grep php` #找到php目录
          cd ${php_dir} 
          configure_info 
          rotate_line & 
          disown $! 
          ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --enable-fpm  --enable-mbstring --with-mcrypt --with-mhash --with-config-file-path=/usr/local/php5/etc --with-mysqli=/usr/local/mysql/bin/mysql_config &>/dev/null#PHP FastCGI模式的设置说明:如果是PHP5.3及以上版本,所使用的编译参数为--enable-fpm,如果是PHP5.2版本,编译参数则为--enable-fastcgi--enable-fpm--enable-force-cgi。#?--enable-fpm:表示激活PHP-FPM方式服务,即以FastCGIF方式运行PHP服务。
          result=$? 
          kill -9 $! 
       make_info 
                language 
                    if [ $? -eq 0 ];then
                        echo -e "\033[1;32m正常情况下软件被安装在/usr/local/php5目录.\033[0m"
                    else
                        echo -e "\033[1;32mSoftware will be installed to /usr/local/php5.\033[0m"
                    fi
                    if [ $result -eq 0 ];then
                       rotate_line & 
                          disown $! 
                        make &> /dev/null && make install &>/dev/null
                          result=$? 
                          kill -9 $!  
                          if [ $result -ne 0 ];then
                            error_install php 
                          fi
                          /bin/cp -f php.ini-production /usr/local/php5/etc/php.ini 
                          /bin/cp -f /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf 
                          cd .. 
                    fi
 else
   error_nofile php 
 fi} 
 
 
#Install memcached install_memcached(){ 
 begin memcached 
 test_yum 
 install_libevent 
 solve_depend memcached 
  
 cd ${install_package_dir}
 if [ -f ${memcached_version} ];then
       untar ${memcached_version}
       memcached_dir=`ls -d */|grep memcached` #找到memcached目录
       cd ${memcached_dir} 
        configure_info 
          rotate_line & 
          disown $! 
          ./configure &>/dev/null && make &>/dev/null && make install &>/dev/null
          result=$? 
          kill -9 $! 
      if [ $result -ne 0 ];then
          error_install memcached 
      fi
      cd .. 
 else
    error_nofile memcached 
 fi} 
 
 
#Install JRE    安装JREinstall_java(){ 
 begin JAVA 
 solve_depend java 
cd ${install_package_dir}
 if [ -f ${jdk_version} ];then
       untar ${jdk_version}
       jdk_dir=`ls -d */|grep jdk` #找到jdk目录
          if [ ! -e /usr/local/jdk ];then
              mv ${jdk_dir} /usr/local/jdk        fi
          cat <> /etc/profile 
export JRE_HOME=/usr/local/jdkexport JAVA_BIN=/usr/local/jdk/binexport PATH=/usr/local/jdk/bin:\$PATH export CLASSPATH=.:/usr/local/jdk/lib/dt.jar:\$JRE_HOME/lib/tools.jar 
export JRE_HOME JAVA_BIN PATH CLASSPATH 
EOF       source /etc/profile else
          error_nofile JAVA 
 fi} 
 
 
#Install Tomcat     安装Tomcatinstall_tomcat(){ 
 begin Tomcat cd ${install_package_dir}
 if [ -f ${tomcat_version} ];then
    untar ${tomcat_version}
    tomcat_dir=`ls -d */|grep tomcat` #找到tomcat目录
      if [ ! -e /usr/local/tomcat ];then
        mv ${tomcat_dir} /usr/local/tomcat      fi
      if [ -f session.tar.gz ];then
          tar -xzf session.tar.gz &> /dev/null
          cp session/* /usr/local/tomcat/lib/     fi
 else
      error_nofile tomcat 
 fi} 
 
 
while : 
do
 
    menu
    choicecase $select in
    1) 
       install_nginx  2>&1 | tee /root/nginx-install.log
       ;; 
    2) 
       install_mysql 2>&1 | tee /root/mysql-install.log
       ;; 
    3) 
       install_php 2>&1 | tee /root/php-install.log
       ;; 
    4) 
       install_memcached 2>&1 | tee /root/memcached-install.log
       ;; 
    5) 
       install_java 2>&1 | tee /root/java-install.log
       ;; 
    6) 
       install_tomcat 2>&1 | tee /root/tomcat-install.log
       ;; 
    7) 
       install_nginx  2>&1 | tee /root/nginx-install.log
       install_mysql 2>&1 | tee /root/mysql-install.log
       install_php  2>&1 | tee /root/php-install.log
       install_memcached 2>&1 | tee /root/memcached-install.log
       install_java 2>&1 | tee /root/java-install.log
       install_tomcat 2>&1 | tee /root/tomcat-install.log
       ;; 
    8|exit|quit) 
       exit
       ;; 
    *) 
       echo -e "\033[1;32mEnter your chice (1, 2, 3, 4, 5, 6, 7 or 8:exit)\033[0m"
        sleep 3esacdone
Original script(原始脚本)
#!/bin/bash #Data:2017-04-18#Version:1.0#Author:Baojia(devops1995@gmail.com) #The software list:Nginx,MySQL,PHP,Memcached,Tomcat,Java. #This script can automatically install all software on your machine.script_description(){echo "+-----------------------------------------------------------------------+"echo "|            Addons script for LNMP V1.0, Written by Baojia             |"echo "+-----------------------------------------------------------------------+"echo "|        The software list:Nginx,Mysql,PHP,Memcached,Tomcat,Java.       |"echo "+-----------------------------------------------------------------------+"echo "|            Hope to work with you to optimize this Script.             |"echo "+-----------------------------------------------------------------------+"echo "|                       devops1995@gmail.com                            |"echo "+-----------------------------------------------------------------------+"}
 
install_package_dir=/tmp/
nginx_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /nginx/'{print $9}'` 
mysql_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /mysql/'{print $9}'` 
cmake_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /cmake/'{print $9}'` 
php_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /php/'{print $9}'` 
memcached_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /memcached/'{print $9}'` 
jdk_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /jdk/'{print $9}'`
tomcat_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /tomcat/'{print $9}'` 
 
version(){
    version_systemctl=`cat /etc/redhat-release |grep 6|awk '{print $3}'|awk -F '[.]' '{print $1}'`
    language    if [ $? -eq 0 ];then
        if [  ${version_systemctl} -ne 6 ];then
            echo "对不起,当前脚本只适用于centOS6.x"
            exit
        fi
    else
        if [ ${version_systemctl} -ne 6 ];then
            echo "Sorry, the current script only applies to centOS6.x."
            exit
        fi
    fi}  
 
#Determine the language environmentlanguage(){ 
 echo $LANG |grep -q zh if [ $? -eq 0 ];then
  return 0
 else
  return 1 
 fi} 
 
# Check if user is rootcheck_user(){if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1fi}#Define user selection Menu.menu(){
 check_user
 clear
 version
 script_description
 language if [ $? -eq 0 ];then
    echo "  ##############----Menu----##############"
    echo "# 1. 安装Nginx"
    echo "# 2. 安装MySQL"
    echo "# 3. 安装PHP"
    echo "# 4. 安装Memcached"
    echo "# 5. 安装Java"
    echo "# 6. 安装Tomcat"
    echo "# 7. 安装以上所有软件"
    echo "# 8. 退出程序"
    echo "  ########################################"
 else
    echo "  ##############----Menu----##############"
    echo "# 1. Install Nginx"
    echo "# 2. Install MySQL"
    echo "# 3. Install PHP"
    echo "# 4. Install Memcached"
    echo "# 5. Install Java"
    echo "# 6. Install Tomcat"
    echo "# 7. Install all above"
    echo "# 8. Exit Program"
    echo "  ########################################"
 fi}#Read user's choice  #Read User's INPUT choice(){
 language if [ $? -eq 0 ];then      
  read -p "请选择一个菜单[1-8]:" select else
  read -p "Please choice a menu[1-8]:" select fi} 
  
rotate_line(){ 
 INTERVAL=0.1 
 TCOUNT="0"
 while : do
  TCOUNT=`expr $TCOUNT + 1`  case $TCOUNT in
    "1") 
          echo -e '-'"\b\c"
          sleep $INTERVAL 
          ;; 
    "2") 
          echo -e '\\'"\b\c"
          sleep $INTERVAL 
          ;; 
    "3") 
          echo -e "|\b\c"
          sleep $INTERVAL 
          ;; 
    "4") 
          echo -e "/\b\c"
          sleep $INTERVAL 
          ;; 
    *) 
          TCOUNT="0";; 
  esac
 done}#Install failed error messages.error_install(){
 language if [ $? -eq 0 ];then
    clear     echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
    echo -e "\033[1;31m错误:编译安装[ ${1} ]失败! \033[0m"
    echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
       exit
 else
      clear     echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
      echo -e "\033[1;31mERROR:Install[ ${1} ]Failed!\033[0m"
   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    exit
 fi} 
 
 
#Couldn't use yum toolerror_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} 
 
 
test_yum(){ 
 yum clean all &>/dev/null
 repolist=$(yum repolist 2>/dev/null |awk '/repolist:/{print $2}'|sed 's/,//') 
 if [ $repolist -le 0 ];then
  error_yum 
 fi} 
 
 
#This function will check depend software and install them.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
 
 case $1 in
  nginx) 
          rpmlist="gcc pcre-devel openssl-devel zlib-devel make"
          ;; 
  cmake) 
          rpmlist="gcc gcc-c++ cmake"
          ;; 
  mysql) 
          rpmlist="ncurses-devel"
          ;; 
  mhash) 
          rpmlist="mhash mcrypt"
          ;; 
  libmcrypt) 
          rpmlist="libmcrypt-devel"
          ;; 
  php) 
          rpmlist="gcc zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel libmcrypt-devel mhash mcrypt freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel"
          ;; 
  libevent) 
          rpmlist=" libevent libevent-devel nc telnet"
          ;; 
  memcached) 
          rpmlist="gcc"
          ;; 
  memcache) 
          rpmlist="autoconf"
          ;; 
  java) 
          rpmlist="glibc.i686"
 esac
 
 for i in $rpmlist 
 do
     rpm -q $i &>/dev/null     if [ $? -ne 0 ];then
        yum -y install $i &>/dev/null     fi
 done} 
 
 
#Determing how to uncompress a tar file.untar(){ 
 type=$(file $1 |awk '{print $2}') if [ "$type" == "gzip" ];then
  language  if [ $? -eq 0 ];then
      echo -e "\033[1;34m正在解压$1,请稍后...\033[0m"
  else
      echo -e "\033[1;34mUncompress $1,Please wait a moment...\033[0m"
  fi
  tar -xzf $1 
 elif [ "$type" == "bzip2" ];then
  language 
  if [ $0 -eq 0 ];then
      echo -e "\033[1;34m正在解压$1,请稍后...\033[0m"
  else
      echo -e "\033[1;34mUncompress $1,Please wait a moment...\033[0m"
  fi
  tar -xjf $1 
 else
  language 
  if [ $? -eq 0 ];then
          clear         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          echo -e "\033[1;34m错误:未知的压缩包类型.\033[0m"
          echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          exit
  else
          clear         echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          echo -e "\033[1;34mERROR:Unknow compress file type.\033[0m"
          echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
          exit
  fi
 fi } 
 
 
#Display a begin mesagesbegin(){
 language 
 if [ $? -eq 0 ];then
          clear         echo -e "\033[1;36m----------------------------------\033[0m"
          echo -e "\033[1;32m\t现在开始安装${1}!\033[0m"
          echo -e "\033[1;36m----------------------------------\033[0m"
 else
          clear         echo -e "\033[1;36m-----------------------------------\033[0m"
          echo -e "\033[1;32m\tInstall ${1} Now!\033[0m"
          echo -e "\033[1;36m-----------------------------------\033[0m"
 fi} 
 
 
#If not found the software package, this script will be exit.error_nofile(){ 
 language 
        if [ $? -eq 0 ];then
              clear              echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              echo -e "\033[1;34m错误:未找到[ ${1} ]软件包,请下载软件包至${install_package_dir}目录.\033[0m"
              echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              exit
        else
              clear              echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              echo -e "\033[1;34mERROR:Not found [ ${1} ] package in ${install_package_dir} directory, please download it.\033[0m"
              echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              exit
        fi} 
 
 
#Display a hint for configureconfigure_info(){ 
 language 
           if [ $? -eq 0 ];then
                    echo -e "\033[1;34m正在检测编译环境并生成编译文件,请稍后...\033[0m"
            else
                    echo -e "\033[1;34mCheck system environment and configure,Please wait a moment...\033[0m"
            fi} 
 
 
#Display a hint for make and make installmake_info(){ 
 language 
            if [ $? -eq 0 ];then
                    echo -e "\033[1;34m正在编译安装软件,请稍后...\033[0m"
            else
                    echo -e "\033[1;34mComplie and install software,Please wait a moment...\033[0m"
            fi} 
 
 
#Install cmake to complie MySQLinstall_cmake(){ 
        test_yum
        solve_depend cmake        if [ $? -ne 0 ];then
            error_nofile cmake 
        fi}install_mhash(){
 solve_depend mhash     if [ $? -ne 0 ];then
          error_install mhash 
      fi} 
 
install_libmcrypt(){ 
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
solve_depend libmcryptif [ $? -ne 0 ];then
    errot_nofile libmcryptfi} 
 
 
#Install libeventinstall_libevent(){
 solve_depend libevent 
 if [ $? -ne 0 ];then
      error_nofile libevent 
 fi} 
 
 
#Install Nginxinstall_nginx(){ 
 begin nginx 
 test_yum 
 solve_depend nginx 
 grep -q nginx /etc/passwd if [ $? -ne 0 ];then
    useradd nginx -s /sbin/nologin -M ficd ${install_package_dir}
 if [ -f ${nginx_version} ];then
          untar ${nginx_version}
          nginx_dir=`ls -d */|grep nginx`        cd $nginx_dir
          configure_info 
          rotate_line & 
          disown $!
          ./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module >/dev/null
          result=$? 
          kill -9 $!
          make_info
          language      if [ $? -eq 0 ];then
          echo -e "\033[1;32m正常情况下软件被安装在/usr/local/nginx目录.\033[0m"
      else
          echo -e "\033[1;32mSoftware will be installed to /usr/local/nginx.\033[0m"
      fi
      if [ $result -eq 0 ];then
          rotate_line & 
          disown $! 
       make &>/dev/null && make install &>/dev/null
          result=$? 
          kill -9 $! 
          cd .. 
          if [ $result -ne 0 ];then
            error_install nginx 
          fi
      fi
 else
  error_nofile Nginx 
 fi} 
 
 
#Install MySQLinstall_mysql(){ 
 begin mysql test_yum
 install_cmake
 solve_depend mysql
 grep -q mysql /etc/passwd if [ $? -ne 0 ];then
     useradd -s /sbin/nologin mysql -M ficd ${install_package_dir}
 if [ -f ${mysql_version} ];then
       untar ${mysql_version}
       mysql_dir=`ls -d */|grep mysql`#       cd ${mysql_dir} 
          configure_info 
          make_info 
          language 
            if [ $? -eq 0 ];then
                echo -e "\033[1;32m正常情况下软件被安装在/usr/local/mysql目录.\033[0m"
            else
                echo -e "\033[1;32mSoftware will be installed to /usr/local/mysql.\033[0m"
            fi
 
              language 
                  if [ $? -eq 0 ];then
                      echo -e "\033[1;34m正在初始化数据库...\033[0m"
                  else
                      echo -e "\033[1;34mInitialization database...\033[0m"
                  fi
  mv ${mysql_dir} /usr/local/${mysql_dir}
  ln -s /usr/local/${mysql_dir} /usr/local/mysql
  /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql/ &>/dev/null
  chown -R mysql.mysql /usr/local/mysql
  chown -R mysql /usr/local/mysql/data
 
  /bin/cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
  chmod +x /etc/init.d/mysqld
  /bin/cp -f /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf 
 
  echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf 
  ldconfig 
   
cat <> /etc/profile 
PATH=\$PATH:/usr/local/mysql/bin/export PATH 
EOF
 
. /etc/profile
 
chkconfig --add mysqld
chkconfig --list mysqld else
   error_nofile mysql 
 fi }#Install PHPinstall_php(){ 
 begin php 
 test_yum 
 install_mhash 
 install_libmcrypt 
 solve_depend php#install libiconv
 wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
 tar zxf libiconv-1.14.tar.gz cd libiconv-1.14
 ./configure --prefix=/usr/local/libiconv
  
      rotate_line & 
      disown $! 
    make &>/dev/null && make install &>/dev/null    return=$?
    language    if [ $? -eq 0 ];then
        if [ $return -ne 0 ];then
            echo "libiconv库编译失败."
        else
            echo "libiconv库编译成功."
        fi
    else
        if [ $return -ne 0 ];then
            echo "Libiconv library failed to compile."
        else
            echo "The libiconv library was compiled successfully."
        fi
    fi
    result=$? 
      kill -9 $!  
       
 cd ../cd ${install_package_dir}
 if [ -f ${php_version} ];then
          untar ${php_version}
          php_dir=`ls -d */|grep php`        cd ${php_dir} 
          configure_info 
          rotate_line & 
          disown $! 
          ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --enable-fpm  --enable-mbstring --with-mcrypt --with-mhash --with-config-file-path=/usr/local/php5/etc --with-mysqli=/usr/local/mysql/bin/mysql_config &>/dev/null
 
          result=$? 
          kill -9 $! 
       make_info 
                language 
                    if [ $? -eq 0 ];then
                        echo -e "\033[1;32m正常情况下软件被安装在/usr/local/php.5.\033[0m"
                    else
                        echo -e "\033[1;32mSoftware will be installed to /usr/local/php5.\033[0m"
                    fi
                    if [ $result -eq 0 ];then
                       rotate_line & 
                          disown $! 
                        make &> /dev/null && make install &>/dev/null
                          result=$? 
                          kill -9 $!  
                          if [ $result -ne 0 ];then
                            error_install php 
                          fi
                          /bin/cp -f php.ini-production /usr/local/php5/etc/php.ini 
                          /bin/cp -f /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf 
                          cd .. 
                    fi
 else
   error_nofile php 
 fi} 
 
 
#Install memcached install_memcached(){ 
 begin memcached 
 test_yum 
 install_libevent 
 solve_depend memcached 
  
 cd ${install_package_dir}
 if [ -f ${memcached_version} ];then
       untar ${memcached_version}
       memcached_dir=`ls -d */|grep memcached`         cd ${memcached_dir} 
        configure_info 
          rotate_line & 
          disown $! 
          ./configure &>/dev/null && make &>/dev/null && make install &>/dev/null
          result=$? 
          kill -9 $! 
      if [ $result -ne 0 ];then
          error_install memcached 
      fi
      cd .. 
 else
    error_nofile memcached 
 fi} 
 
 
#Install JREinstall_java(){ 
 begin JAVA 
 solve_depend java 
cd ${install_package_dir}
 if [ -f ${jdk_version} ];then
       untar ${jdk_version}
       jdk_dir=`ls -d */|grep jdk`        if [ ! -e /usr/local/jdk ];then
              mv ${jdk_dir} /usr/local/jdk        fi
          cat <> /etc/profile 
export JRE_HOME=/usr/local/jdkexport JAVA_BIN=/usr/local/jdk/binexport PATH=/usr/local/jdk/bin:\$PATH export CLASSPATH=.:/usr/local/jdk/lib/dt.jar:\$JRE_HOME/lib/tools.jar 
export JRE_HOME JAVA_BIN PATH CLASSPATH 
EOF
. /etc/profile else
          error_nofile JAVA 
 fi} 
 
 
#Install Tomcatinstall_tomcat(){ 
 begin Tomcat cd ${install_package_dir}
 if [ -f ${tomcat_version} ];then
    untar ${tomcat_version}
    tomcat_dir=`ls -d */|grep tomcat`      if [ ! -e /usr/local/tomcat ];then
        mv ${tomcat_dir} /usr/local/tomcat      fi
      if [ -f session.tar.gz ];then
          tar -xzf session.tar.gz &> /dev/null
          cp session/* /usr/local/tomcat/lib/     fi
 else
      error_nofile tomcat 
 fi} 
 
 
while : 
do
 
    menu
    choicecase $select in
    1) 
       install_nginx  2>&1 | tee /root/nginx-install.log
       ;; 
    2) 
       install_mysql 2>&1 | tee /root/mysql-install.log
       ;; 
    3) 
       install_php 2>&1 | tee /root/php-install.log
       ;; 
    4) 
       install_memcached 2>&1 | tee /root/memcached-install.log
       ;; 
    5) 
       install_java 2>&1 | tee /root/java-install.log
       ;; 
    6) 
       install_tomcat 2>&1 | tee /root/tomcat-install.log
       ;; 
    7) 
       install_nginx  2>&1 | tee /root/nginx-install.log
       install_mysql 2>&1 | tee /root/mysql-install.log
       install_php  2>&1 | tee /root/php-install.log
       install_memcached 2>&1 | tee /root/memcached-install.log
       install_java 2>&1 | tee /root/java-install.log
       install_tomcat 2>&1 | tee /root/tomcat-install.log
       ;; 
    8|exit|quit) 
       exit
       ;; 
    *) 
       echo -e "\033[1;32mEnter your chice (1, 2, 3, 4, 5, 6, 7 or 8:exit)\033[0m"
打赏作者

Leave a Reply

Your email address will not be published.