LNMP自动安装部署脚本_Linux

我们的业务使用了nginx、MySQL、php、和tomcat,写了一个自动化安装这些软件的脚本,虽然网上类似的自动安装脚本很多,但这个是自己写的,特此mark一下,希望能给别人一些参考,同时也灰常欢迎看到的朋友们提些改进意见。话不多说,直接上脚本。

#!/bin/sh
  alias echo='echo -e'
  ###判断系统,此脚本只适用于64位系统
  machine=`uname -m`
  if [ $machine != x86_64 ] ;then
    echo "\e[1;31m \nyour system is 32bit,but this script is only run on 64bit !\n \e[0m"
    exit -1
  fi
  ###创建保存下载文件的目录
  mkdir /usr/local/src/auto_install &> /dev/null
  source_dir=/usr/local/src/auto_install
  ###定义一个交互式的选择提示
function interact {
  echo "Please input \"yes\" or \"no\""
  read choice
  case "$choice" in
    no)
     exit 0;;
    yes)
     echo;;
    *)
   echo "\e[1;31mInput Errot! \e[0m"
     exit -1;;
  esac
}
   ### 定义一个函数检查上一条命令的执行状态
function check {
  if [ $? -ne 0 ];then
    echo "\e[1;31m\n the last commond exec failed,please check it !\e[0m \n"
  sleep 1
  exit -1
  fi
}
### 定义下载各软件的函数(此处我隐藏了我们的下载地址,需各位自行设置下载地址)
function download_install_epel {
  cd $source_dir
  rm -f epel* &> /dev/null
  echo "\e[1;32m ---yum install epel repo--- \e[0m \n"
  sys_version=`uname -r|cut -d '.' -f 4`
  if [ $sys_version = el7 ];then
    rpm -qa|grep -i epel > /dev/null
    if [ $? -ne 0 ];then
     wget -nv http://www.example.com/epel-release-7-2.noarch.rpm
     rpm -ivh $source_dir/epel-release-7-1.noarch.rpm
     check;
    fi
  elif [ $sys_version = el6 ];then
    rpm -qa|grep -i epel > /dev/null
    if [ $? -ne 0 ];then
     wget -nv http://www.example.com/epel-release-6-8.noarch.rpm
     rpm -ivh $source_dir/epel-release-6-8.noarch.rpm
     check;
    fi
  fi
}
function download_nginx {
  cd $source_dir
  echo "\e[1;32m\n ---download nginx-1.4.7---\e[0m \n"
  ls nginx-1.4.7.tar.gz &> /dev/null
  if [ $? -ne 0 ];then
     wget -nv http://www.example.com/nginx-1.4.7.tar.gz
     check;
  fi
}
function download_mysql {
  cd $source_dir
  echo "\e[1;32m\n ---download MySQL-5.5.39---\e[0m \n"
  ls mysql-5.5.39-linux2.6-x86_64.tar.gz &> /dev/null
  if [ $? -ne 0 ];then
     wget -nv http://www.example.com/mysql-5.5.39-linux2.6-x86_64.tar.gz
     check;
  fi
}
function download_php {
  cd $source_dir
  echo "\e[1;32m\n ---download php-5.3.28---\e[0m \n"
  ls php-5.3.28.tar.gz &> /dev/null
  if [ $? -ne 0 ];then
      wget -nv http://www.example.com/php-5.3.28.tar.gz
      check;
  fi
}
function download_jre {
  cd $source_dir
  echo "\e[1;32m ---download jre-6u33-linux-x64.bin---\e[0m"
  sleep 1
  ls jre-6u33-linux-x64.bin &> /dev/null
  if [ $? -ne 0 ];then
    wget -nv http://www.example.com/jre-6u33-linux-x64.bin
    check;
  fi
}
    ###定义依赖包安装函数
function dependence_install {
  download_install_epel;
  echo "\e[1;32m ---yum install the Dependencies software--- \e[0m \n"
  sleep 1
  pgrep yum|xargs kill -9 > /dev/null
  rm -f /var/run/yum.pid > /dev/null
  echo "yum install gcc zlib cmake libxml2 libxml2-devel bzip2 bzip2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel pspell-devel libmcrypt libm
crypt-devel freetype pcre-devel openssl openssl-devel libaio*"
  yum -y install gcc zlib cmake libxml2 libxml2-devel bzip2 bzip2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel pspell-devel libmcrypt libmcry
pt-devel freetype pcre-devel openssl openssl-devel libaio* >/dev/null 2>&1
  check;
}
    ### 定义mysql安装函数###
function mysql_install {

    ###判断本机是否已在运行mysql,或者开机启动项有没有mysql###
    ###判断机器上是否以运行mysql实例
  echo "\e[1;32m \n ---check if there is already installed mysql instence---\e[0m\n"
  ps -ef|grep -w mysqld|grep -v "grep" &> /dev/null
  if [ $? -eq 0 ];then
    mysqlbasedir=`ps -ef|grep -w "mysqld"|grep -v "grep"|awk '{print $9}'|tr -d '\-\-'`
    mysqldatadir=`ps -ef|grep -w "mysqld"|grep -v "grep"|awk '{print $10}'|tr -d '\-\-'`
    echo "\e[1;32m your system has run a mysql instance already \n mysqlbasedir is: "$mysqlbasedir"\n mysqldatadir is: "$mysqldatadir"\n do you still want to install a new mysql-server? \e[0m\n"
    interact;
  fi
    ###判断系统是否已经有rpm方式安装的mysql
   echo "MySQL-server-5.5.39-2.el6.x86_64.rpm"|while read line
   do
     prefix=`echo "$line"|awk -F "-" '{print $1"-"$2}'`               ##to get like "MySQL-server"
     rpm -qa|grep -i "$prefix" &> /tmp/check_mysql.log
   if [ $? -eq 0 ];then
      already_install=`cat /tmp/check_mysql.log|awk -F "-" '{print $1"-"$2"-"$3}'`  ##to get the already installed mysql version
      echo "\e[1;31m ---the $already_install is already installed by rpm!--- \n ---Do you really want to install a new mysql?--- \e[0m\n"
      interact;
     fi
   done
    ###判断chkconfig里面是否有mysql启动脚本并且为开机启动
  chkconfig |grep mysql|awk '{print $5" "$7}'|grep -e "启用" -e "on" &> /dev/null
  if [ $? -eq 0 ];then
    echo " there is a bootstart mysql script in the chkconfig,please check"
    exit 0
  fi

    ###判断/etc/rc.local文件里是否有mysql启动命令
  cat /etc/rc.local|grep "mysqld" &> /dev/null
  if [ $? -eq 0 ];then
    echo " there is a bootstart mysql commond in the /etc/rc.local,please check it"
    exit 0
  fi
    ######判断结束######

  ###检查是否有系统自带的mysql-lib-5.1,有则删除
  rpm -qa|grep -i -e "mysql.*5\.1.*"|xargs rpm -e --nodeps 2> /dev/null
    ###交互式选择mysql安装路径
  echo "\e[1;31m where do you want to install mysql(default is /usr/local/mysql)\e[0m"
  read mybasedir
  if [ "$mybasedir" = "" ];then
    mybasedir=/usr/local/mysql
  fi
    ###选择data目录
  echo "\e[1;31m where do you want to storage mysql data(default is /usr/local/mysql/data)\e[0m"
  read mydatadir
  if [ "$mydatadir" = "" ];then
    mydatadir=/usr/local/mysql/data
  fi  

    ###开始安装mysql
  echo "\e[1;32m ---now start to install mysql---\e[0m\n"
  sleep 1
  #dependence_install;
  echo "\e[1;32m ---add user mysql---\e[0m"
  useradd mysql -s /sbin/nologin

  echo "\e[1;32m\n ---unpack mysql-5.5.39-linux2.6-x86_64.tar.gz---\e[0m"
  cd $source_dir
  tar zxf mysql-5.5.39-linux2.6-x86_64.tar.gz
  check;
  rm -rf $mybasedir
  mv mysql-5.5.39-linux2.6-x86_64 $mybasedir

    ###判断/etc下是否有my.cnf,若有,重命名为my.cnf.bak
  ls /etc/my.cnf &> /dev/null
  if [ $? = 0 ];then
    \mv /etc/my.cnf /etc/my.cnf.bak
    echo "\e[1;32m\n ---detected /etc/my.cnf is already exit,rename it to /etc/my.cnf.bak--- \e[0m \n"
  fi
  cp $mybasedir/support-files/my-huge.cnf /etc/my.cnf
    ###拷贝开机启动脚本到/etc/init.d/
  ls /etc/init.d/mysql &> /dev/null
  if [ $? = 0 ];then
    \mv /etc/init.d/mysql /etc/init.d/mysql.bak
    echo "\e[1;32m ---detected /etc/init.d/mysql is already exit,rename it to /etc/init.d/mysql.bak---\e[0m"
  fi
  cp $mybasedir/support-files/mysql.server /etc/init.d/mysql
    ###修改/etc/init.d/mysql脚本里面basedir和datadir
  sed -i -e "/^basedir*=$/i\basedir=$mybasedir" -e "/^datadir*=$/i\datadir=$mydatadir" /etc/init.d/mysql
  sed -i -e '/^basedir*=$/d' -e '/^datadir*=$/d' /etc/init.d/mysql
    ###初始化数据库
  echo "\e[1;32m\n ---initialize mysql database---\e[0m"
  sleep 1
  chown -R mysql $mydatadir $mybasedir >/dev/null 2>&1
  $mybasedir/scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --basedir=$mybasedir --datadir=$mydatadir
  check;
    ###将basedir/bin/添加到PATH
  echo "PATH=$PATH:$mybasedir/bin" >> /etc/profile
  export PATH=$PATH:$mybasedir/bin

  echo "\e[1;32m install mysql success,you can run \"service mysql start\" to start the mysql \n do not forget to set a password for mysql \e[0m"
  ###至此mysql安装完毕
}
  ### 定义nginx安装函数
function nginx_install {
  echo
  echo "\e[1;32m ---start installing nginx,it's basedir is /usr/local/nginx--- \e[0m \n"
  sleep 1
  cd $source_dir
  ls -l |grep "^d"|grep nginx|xargs rm -rf &> /dev/null
  tar zxf nginx-1.4.7.tar.gz
  check;
  cd nginx-1.4.7
echo "\e[1;32m ---configure: configure information in /tmp/configure_nginx.log--- \e[0m \n"
  sleep 1
  ./configure --with-http_stub_status_module --prefix=/usr/local/nginx &> /tmp/configure_nginx.log
  check;
echo "\e[1;32m ---make: make information in /tmp/make_nginx.log--- \e[0m \n"
  sleep 1
  make &> /tmp/configure_nginx.log
  check;
echo "\e[1;32m ---make install: install into /usr/local/nginx--- \e[0m \n"
  sleep 1
  make install &> /tmp/make_install.log
  check;
  echo "\e[1;32m nginx is installed successfully \e[0m \n"
}
  ### 定义php安装函数
function php_install {
  dependence_install;
  cd $source_dir
  ls -l |grep "^d"|grep php|xargs rm -rf &> /dev/null
  tar zxvf php-5.3.28.tar.gz &> /dev/null
  check;
  cd php-5.3.28
  echo
  echo "\e[1;32m ---configure php: information in /tmp/php_configure.log--- \e[0m \n"
  echo "./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-mysqli --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-iconv-dir=/usr/local --with-zlib --enable-zip --with-curl"
  ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-mysqli --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-iconv-dir=/usr/local --with-zlib --enable-zip --with-curl &> /tmp/php_configure.log
  check;

  ###make
  echo "\e[1;32m ---make php: information in /tmp/php_make.log--- \e[0m \n"
  make &> /tmp/php_make.log
  check;
  ###make install
  echo "\e[1;32m ---make install php: install into /usr/local/php--- \n ---php cofigure file in the /usr/local/php/etc--- \e[0m \n"
  make install > /tmp/make_install_php.log
  check;

  echo "\e[1;32m ---prepare php configure files--- \e[0m \n"
  echo "cp $source_dir/php-5.3.28/php.ini-production /usr/local/php/etc/php.ini"
  cp php.ini-production /usr/local/php/etc/php.ini
  echo "cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf"
  cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  echo "\e[1;32m\n php is installed successfully!\n now,please start php and modification the php.ini \e[0m"
}
  ###安装jre
function jre_install {
  ###删除系统自带的java
  rpm -qa|grep java|xargs rpm -e --nodeps

  cd $source_dir
  rm -rf jre1.6.0_33 &> /dev/null
  chmod +x jre-6u33-linux-x64.bin &> /dev/null
  ./jre-6u33-linux-x64.bin >/dev/null 2>&1
  check;
  ls -d /usr/java
  if [ $? -ne 0 ];then
    mkdir /usr/java &> /dev/null
  else
    echo echo "\e[1;31m /usr/java has already exits,move to /usr/java_bak !\e[0m"
    mkdir /usr/java_bak &> /dev/null
    mv /usr/java/* /usr/java_bak/
  fi
  mv jre1.6.0_33 /usr/java/
  echo "\e[1;32m\n ---add java path to /etc/profile---\e[0m"
  sleep 1

  echo >>/etc/profile
  echo 'JAVA_HOME=/usr/java/jre1.6.0_33'>>/etc/profile
  echo 'CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar'>>/etc/profile
  echo 'PATH=$JAVA_HOME/bin:$PATH:$HOME/bin:$JAVA_HOME/jre/bin'>>/etc/profile
  echo 'export PATH JAVA_HOME'>>/etc/profile
  source /etc/profile
  echo "\e[1;32m\n JRE is installed success !\e[0m"
  echo "\e[1;32m\n ---install cronolog---\e[0m"
  wget -nv http://www.example.com/cronolog-1.6.2.tar.gz
  dependence_install;
  tar zxf cronolog-1.6.2.tar.gz
  cd cronolog-1.6.2
  ./configure &> /tmp/cronolog_configure.log && make > /dev/null && make install > /dev/null
  check;
  echo "\e[1;32m cronolog is installed success\e[0m\n"
}
  ###下面开始选择那些软件需要安装
echo "\e[1;31m\n ---选择哪些软件需要安装--- \e[0m \n"
echo "\e[1;32m \"Nginx\"   INPUT \"1\" \e[0m \n"
echo "\e[1;32m \"Mysql\"   INPUT \"2\" \e[0m \n"
echo "\e[1;32m \"Php\"    INPUT \"3\" \e[0m \n"
echo "\e[1;32m \"JRE\"    INPUT \"4\" \e[0m \n"
#echo "\e[1;32m \"Tomcat\"   INPUT \"5\" \e[0m \n"
read -p "please choice which software do you want to install ?" input

  if [ $input = 1 ];then
      download_nginx;
      dependence_install;
      nginx_install;
  elif [ $input = 2 ];then
    download_mysql;
      mysql_install;
  elif [ $input = 3 ];then
      echo "\e[1;32m\n php needs mysqlclient \e[0m"
      echo "\e[1;32m so if you had already install a mysqlclient\n please make a softlink for all \"libmysqlclient.so.*\" file to both /var/lib64 and /var/lib \e[0m\n"
      echo "\e[1;31m Please input your choice\n\e[0m \e[1;32m1: go on by default\(install mysql-client-5.5.39\)\n 2: I will check my \"libmysqlclient.so.*\" file and redo this script later\n 3: I had make the softlinks and I want to going on \e[0m"
      read choice
      case "$choice" in
       1)
        cd $sourcedir
        echo "\e[1;32m ---download mysql-client rpm files---\e[0m"
        wget -nv http://www.example.com/MySQL-client-5.5.39-2.el6.x86_64.rpm
        wget -nv http://www.example.com/MySQL-devel-5.5.39-2.el6.x86_64.rpm
        wget -nv http://www.example.com/MySQL-shared-5.5.39-2.el6.x86_64.rpm
        echo "\e[1;32m\n ---install mysql-client in the way of rpm---\e[0m"
        sleep 1
        rpm -ivh MySQL-client-5.5.39-2.el6.x86_64.rpm MySQL-devel-5.5.39-2.el6.x86_64.rpm MySQL-shared-5.5.39-2.el6.x86_64.rpm
        download_php;
        ###make soft link from /usr/lib64/libmysqlclient* to /usr/lib/
        ls -l /usr/lib64/libmysqlclient*|grep -v "^l"|awk '{print $NF}'|while read line
         do
          usrlib_fulldir=`ls -l /usr/lib64/libmysqlclient*|grep -v "^l"|awk '{print $NF}'|sed 's/lib64/lib/'`
          ln -s $line $usrlib_fulldir
         done

        php_install;
        ;;
      2)
        exit 0;;
      3)
        download_php;
        php_install;
        ;;
      *)
        echo "Input Errot!" && exit -1;;
     esac

   elif [ $input = 4 ];then
      download_jre;
      jre_install;
   else
    echo "\e[1;31m your input is worng!\e[0m"
    sleep 1
    exit -1
   fi

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索安装
, 部署
lnmp
linux自动部署脚本、linux自动化部署脚本、linux 部署脚本、linux 环境部署脚本、shell自动化部署脚本,以便于您获取更多的相关知识。

时间: 2024-10-26 00:46:42

LNMP自动安装部署脚本_Linux的相关文章

script_tool_for_linux.bash: Linux 环境下的 hosts 一键部署脚本_linux shell

Linux 环境下的 hosts 一键部署脚本,由 @lstoars 贡献; @fluviusmagnus 提供增强版本. 官方网站:https://github.com/racaljk/hosts/tree/master/hosts_tools #!/bin/sh # # script_tool_for_linux # # Use command: `sudo sh script_tool_for_linux.sh` or # `su -c 'sh script_tool_for_linux.

linux下mysql如何自动备份shell脚本_linux shell

Linux 服务器上的程序每天都在更新 MySQL 数据库,于是就想起写一个 shell 脚本,结合 crontab,定时备份数据库.其实非常简单,主要就是使用 MySQL 自带的 mysqldump 命令. #!/bin/bash # Shell script to backup MySql database # To backup Nysql databases file to /backup dir and later pick up by your # script. You can s

linux下实现ftp自动备份shell脚本_linux shell

利用here文档 复制代码 代码如下: #!/bin/sh ftp -ivn 210.29.28.124 <<EOF user yun yun2011 lcd /home/veyun cd /home/veyun/yhb get v bye EOF 解释: -i :关闭多文件传输过程中的交互提示,所以不会再有让用户输入用户名和密码的提示 -n:阻止了初始连接时的自动登录 -v:将ftp命令设置为verbose模式,从而可以在会话时看到其中的命令 在本例中使用用户名yun和密码yun2011登录

只安装redis的redis自动安装脚本

Redis自动安装部署 本文总共分为4个部分: redis自动安装脚本目录结构说明: redis自动安装脚本内容: redis的操作系统服务脚本说明: 本文引用的资料链接地址. 说明:本文未涉及redis内存分配器部分的内容. 第一部分 Redis自动安装脚本目录结构说明: # ls 查看当前目录包含以下3个文件 -- install_redis.sh        # redis自动安装脚本 -- redis-2.8.9.tar.gz      # redis安装包(需make,make te

Hadoop自动化安装shell脚本

之前写过一些如何安装Cloudera Hadoop的文章,安装hadoop过程中,最开始是手动安装apache版本的hadoop,其次是使用Intel的IDH管理界面安装IDH的hadoop,再然后分别手动和通过cloudera manager安装hadoop,也使用bigtop-util yum方式安装过apache的hadoop. 安装过程中参考了很多网上的文章,解压缩过cloudera的cloudera-manager-installer.bin,发现并修复了IDH shell脚本中关于p

生产库自动化MySQL5.6安装部署详细教程_Mysql

自动化运维是一个DBA应该掌握的技术,其中,自动化安装数据库是一项基本的技能,本文中的安装脚本已通过测试,作为生产库来说没有问题,鉴于每个公司存储规划要求不同,可以按需自行修改脚本. 脚本中已经注释说明一些基本的安装信息 本脚本默认启用5.6部分新特性 innodb_buffer_pool_dump_at_shutdown=1 它dump的不是数据,是Id号 innodb_buffer_pool_load_at_startup=1 开启这个两个参数当数据库重启后把这些热数据重新加载回去 只有正常

BAT批处理、VBScript批量安装字体脚本分享_vbs

根据新系统要求,经常要部署一些原来系统里没有的字体,原先我为了图省事经常会要求用户手动安装字体文件,虽然Windows的易用性做得相当不错,但是仍然要照顾一些不会安装字体的人,其实把这些字体打包进安装包更为方便,不过我觉得总不能每有新字体都要搞个安装包那么麻烦吧.更重要的是仍然有人会问我字体怎么安装,以前清一色的Windows XP系统,我倒也方便,直接告知打开控制面板找到字体文件夹,把要安装的字体拖进去即可:现在有Windows 7还是Windows 8等各种版本Windows系统,对于安装字

Linux服务器下MariaDB 10自动化安装部署_MongoDB

去MariaDB官网下载MariaDB本文用的是MariaDB 10.1.16 https://downloads.mariadb.org 选择二进制版本,下载到/root目录下 mariadb-10.1.16-linux-x86_64.tar.gz 开始安装 [root@HE3 ~]# cat mariadb_auto_install.sh ###### 二进制自动安装数据库脚本root密码MANAGER将脚本和安装包放在/root目录即可############### ######数据库目录

阿里云主机一键安装lamp、lnmp环境的shell脚本分享_linux shell

阿里云主机一键安装lamp,lnmp,自动安装脚本,由阿里云主机分享 一键安装包下载地址:点击下载 1.阿里云分享的一键安装lamp,lnmp,此安装包包含的软件及版本为: 复制代码 代码如下: nginx:1.0.15.1.2.5.1.4.4 apache:2.2.22.2.4.2 mysql:5.1.73.5.5.35.5.6.15 php:5.3.18.5.4.23.5.5.7 php扩展:memcache.Zend Engine/ OPcache ftp:(yum/apt-get安装)