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

#!/bin/shalias echo='echo -e'###判断系统,此脚本只适用于64位系统machine=`uname -m`if [ $machine != x86_64 ] ;thenecho "\e[1;31m \nyour system is 32bit,but this script is only run on 64bit !\n \e[0m"exit -1fi ###创建保存下载文件的目录mkdir /usr/local/src/auto_install &> /dev/nullsource_dir=/usr/local/src/auto_install###定义一个交互式的选择提示
function interact {echo "Please input \"yes\" or \"no\""read choicecase "$choice" inno)exit 0;;yes)echo;;*)echo "\e[1;31mInput Errot! \e[0m"exit -1;;esac
}### 定义一个函数检查上一条命令的执行状态
function check {if [ $? -ne 0 ];thenecho  "\e[1;31m\n the last commond exec failed,please check it !\e[0m \n"sleep 1exit -1fi
}
### 定义下载各软件的函数(此处我隐藏了我们的下载地址,需各位自行设置下载地址)
function download_install_epel {cd $source_dirrm -f epel* &> /dev/nullecho "\e[1;32m ---yum install epel repo--- \e[0m \n"        sys_version=`uname -r|cut -d '.' -f 4`if [ $sys_version = el7 ];thenrpm -qa|grep -i epel > /dev/nullif [ $? -ne 0 ];thenwget -nv http://www.example.com/epel-release-7-2.noarch.rpmrpm -ivh $source_dir/epel-release-7-1.noarch.rpmcheck;fielif [ $sys_version = el6 ];thenrpm -qa|grep -i epel > /dev/nullif [ $? -ne 0 ];thenwget -nv http://www.example.com/epel-release-6-8.noarch.rpmrpm -ivh $source_dir/epel-release-6-8.noarch.rpmcheck;fifi
}
function download_nginx {cd $source_direcho  "\e[1;32m\n ---download nginx-1.4.7---\e[0m \n"ls  nginx-1.4.7.tar.gz &> /dev/nullif [ $? -ne 0 ];thenwget -nv  http://www.example.com/nginx-1.4.7.tar.gzcheck;fi
}
function download_mysql {cd $source_direcho  "\e[1;32m\n ---download MySQL-5.5.39---\e[0m \n"ls mysql-5.5.39-linux2.6-x86_64.tar.gz &> /dev/nullif [ $? -ne 0 ];thenwget -nv  http://www.example.com/mysql-5.5.39-linux2.6-x86_64.tar.gzcheck;fi
}
function download_php {cd $source_direcho  "\e[1;32m\n ---download php-5.3.28---\e[0m \n"ls php-5.3.28.tar.gz &> /dev/nullif [ $? -ne 0 ];thenwget -nv  http://www.example.com/php-5.3.28.tar.gzcheck;fi
}
function download_jre {cd $source_direcho "\e[1;32m ---download jre-6u33-linux-x64.bin---\e[0m"sleep 1ls jre-6u33-linux-x64.bin &> /dev/nullif [ $? -ne 0 ];thenwget -nv  http://www.example.com/jre-6u33-linux-x64.bincheck;fi
}###定义依赖包安装函数
function dependence_install {download_install_epel;echo "\e[1;32m ---yum install the Dependencies software--- \e[0m \n"  sleep 1pgrep yum|xargs kill -9 > /dev/nullrm -f /var/run/yum.pid > /dev/nullecho "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>&1check;
}### 定义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/nullif [ $? -eq 0 ];thenmysqlbasedir=`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方式安装的mysqlecho "MySQL-server-5.5.39-2.el6.x86_64.rpm"|while read linedoprefix=`echo "$line"|awk -F "-" '{print $1"-"$2}'`                             ##to get like "MySQL-server"rpm -qa|grep -i "$prefix" &> /tmp/check_mysql.logif [ $? -eq 0 ];thenalready_install=`cat /tmp/check_mysql.log|awk -F "-" '{print $1"-"$2"-"$3}'`   ##to get the already installed mysql versionecho "\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;fidone###判断chkconfig里面是否有mysql启动脚本并且为开机启动chkconfig |grep mysql|awk '{print $5" "$7}'|grep -e "启用" -e "on" &> /dev/nullif [ $? -eq 0 ];thenecho " there is a bootstart mysql script in the chkconfig,please check"exit 0fi###判断/etc/rc.local文件里是否有mysql启动命令cat /etc/rc.local|grep "mysqld" &> /dev/nullif [ $? -eq 0 ];thenecho " there is a bootstart mysql commond in the /etc/rc.local,please check it"exit 0fi   ######判断结束#########检查是否有系统自带的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 mybasedirif [ "$mybasedir" = "" ];thenmybasedir=/usr/local/mysqlfi###选择data目录echo  "\e[1;31m where do you want to storage mysql data(default is /usr/local/mysql/data)\e[0m"read mydatadirif [ "$mydatadir" = "" ];thenmydatadir=/usr/local/mysql/datafi    ###开始安装mysqlecho "\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/nologinecho "\e[1;32m\n ---unpack mysql-5.5.39-linux2.6-x86_64.tar.gz---\e[0m"cd $source_dirtar zxf mysql-5.5.39-linux2.6-x86_64.tar.gzcheck;rm -rf $mybasedirmv mysql-5.5.39-linux2.6-x86_64 $mybasedir###判断/etc下是否有my.cnf,若有,重命名为my.cnf.bakls /etc/my.cnf &> /dev/nullif [ $? = 0 ];then\mv /etc/my.cnf /etc/my.cnf.bakecho "\e[1;32m\n ---detected /etc/my.cnf is already exit,rename it to /etc/my.cnf.bak--- \e[0m \n"ficp $mybasedir/support-files/my-huge.cnf /etc/my.cnf###拷贝开机启动脚本到/etc/init.d/ls /etc/init.d/mysql &> /dev/nullif [ $? = 0 ];then\mv /etc/init.d/mysql /etc/init.d/mysql.bakecho "\e[1;32m ---detected /etc/init.d/mysql is already exit,rename it to /etc/init.d/mysql.bak---\e[0m"ficp $mybasedir/support-files/mysql.server /etc/init.d/mysql###修改/etc/init.d/mysql脚本里面basedir和datadirsed -i -e "/^basedir*=$/i\basedir=$mybasedir" -e "/^datadir*=$/i\datadir=$mydatadir" /etc/init.d/mysqlsed -i -e '/^basedir*=$/d' -e '/^datadir*=$/d' /etc/init.d/mysql   ###初始化数据库echo "\e[1;32m\n ---initialize mysql database---\e[0m"sleep 1chown -R mysql $mydatadir $mybasedir >/dev/null 2>&1$mybasedir/scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --basedir=$mybasedir --datadir=$mydatadircheck;###将basedir/bin/添加到PATHecho "PATH=$PATH:$mybasedir/bin" >> /etc/profileexport PATH=$PATH:$mybasedir/binecho "\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 {echoecho "\e[1;32m ---start installing nginx,it's basedir is /usr/local/nginx--- \e[0m \n"sleep 1cd $source_dirls -l |grep "^d"|grep nginx|xargs rm -rf &> /dev/nulltar zxf nginx-1.4.7.tar.gzcheck;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.logcheck;
echo "\e[1;32m ---make: make information in /tmp/make_nginx.log--- \e[0m \n"sleep 1make &> /tmp/configure_nginx.logcheck;
echo "\e[1;32m ---make install: install into /usr/local/nginx--- \e[0m \n"sleep 1make install &> /tmp/make_install.logcheck;echo "\e[1;32m nginx is installed successfully \e[0m \n"
}### 定义php安装函数
function php_install {dependence_install;cd $source_dirls -l |grep "^d"|grep php|xargs rm -rf &> /dev/nulltar zxvf php-5.3.28.tar.gz &> /dev/nullcheck;cd php-5.3.28echoecho "\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.logcheck;###makeecho "\e[1;32m ---make php: information in /tmp/php_make.log--- \e[0m \n"make &> /tmp/php_make.logcheck;###make installecho "\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.logcheck;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.iniecho "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.confecho "\e[1;32m\n php is installed successfully!\n now,please start php and modification the php.ini \e[0m"
}###安装jre
function jre_install {###删除系统自带的javarpm -qa|grep java|xargs rpm -e --nodepscd $source_dirrm -rf jre1.6.0_33 &> /dev/nullchmod +x jre-6u33-linux-x64.bin &> /dev/null./jre-6u33-linux-x64.bin >/dev/null 2>&1check;ls -d /usr/javaif [ $? -ne 0 ];thenmkdir /usr/java &> /dev/nullelseecho echo "\e[1;31m /usr/java has already exits,move to /usr/java_bak !\e[0m"mkdir /usr/java_bak  &> /dev/nullmv /usr/java/* /usr/java_bak/fimv  jre1.6.0_33 /usr/java/echo "\e[1;32m\n ---add java path to /etc/profile---\e[0m"sleep 1echo  >>/etc/profileecho 'JAVA_HOME=/usr/java/jre1.6.0_33'>>/etc/profileecho 'CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar'>>/etc/profileecho 'PATH=$JAVA_HOME/bin:$PATH:$HOME/bin:$JAVA_HOME/jre/bin'>>/etc/profileecho 'export  PATH JAVA_HOME'>>/etc/profilesource /etc/profileecho "\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.gzdependence_install;tar zxf cronolog-1.6.2.tar.gzcd cronolog-1.6.2./configure &> /tmp/cronolog_configure.log && make > /dev/null && make install > /dev/nullcheck;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 ?" inputif [ $input = 1 ];thendownload_nginx;dependence_install;nginx_install;elif [ $input = 2 ];thendownload_mysql;mysql_install;elif [ $input = 3 ];thenecho "\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 choicecase "$choice" in1)cd $sourcedirecho "\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.rpmwget -nv http://www.example.com/MySQL-devel-5.5.39-2.el6.x86_64.rpmwget -nv http://www.example.com/MySQL-shared-5.5.39-2.el6.x86_64.rpmecho "\e[1;32m\n ---install mysql-client in the way of rpm---\e[0m"sleep 1rpm -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.rpmdownload_php;###make soft link from /usr/lib64/libmysqlclient* to /usr/lib/ls -l /usr/lib64/libmysqlclient*|grep -v "^l"|awk '{print $NF}'|while read linedousrlib_fulldir=`ls -l /usr/lib64/libmysqlclient*|grep -v "^l"|awk '{print $NF}'|sed 's/lib64/lib/'`ln -s $line $usrlib_fulldirdonephp_install;;;2)exit 0;;3)download_php;php_install;;;*)echo "Input Errot!" && exit -1;;esacelif [ $input = 4 ];thendownload_jre;jre_install;elseecho "\e[1;31m your input is worng!\e[0m"sleep 1exit -1fi

转载于:https://blog.51cto.com/kaifly/1558760

LNMP自动部署脚本相关推荐

  1. Linux 多应用程序docker自动部署脚本

    2019独角兽企业重金招聘Python工程师标准>>> Linux 多应用程序docker自动部署脚本可以结合jenkins分布式部署 参数: 镜像名:端口的格式:版本号 例如:sp ...

  2. 解析Linux 多应用程序docker自动部署脚本

    2019独角兽企业重金招聘Python工程师标准>>> 摘要: Linux 多应用程序docker自动部署脚本可以结合jenkins分布式部署 Linux 多应用程序docker自动 ...

  3. kubernetesV1.13.1一键部署脚本(k8s自动部署脚本)

    kubernetesV1.13.1一键部署脚本(k8s自动部署脚本)  devops的那些事 https://www.jianshu.com/p/c26af5647865 请关注公众号,技术获得k8s ...

  4. lnmp一键部署脚本

    LNMP一键部署脚本 LNMP代表的是:linux系统下 nginx+mysql+php 网站服务器架构 一键部署的意思是,执行脚本,解决所有依赖关系,没有报错.脚本执行完成,环境部署完成 所以在做一 ...

  5. java自动部署脚本

    java自动部署脚本 背景 每次开发了新功能和修复了bug上线前都需要打包上传到服务端运行 偶尔来几次还行 次次都这样就烦人了 今天终于忍无可忍 无需在忍 写了个脚本一键打包上传部署 实现思路 通过m ...

  6. python自动部署环境_在 CentOS 上初始化 Python 环境的自动部署脚本

    . ├── 0_start.sh ├── 1_shell_init.sh ├── 2_deploy_firewall.sh ├── 3_install_git.sh ├── 4_install_pip ...

  7. SHELL编写NGINX自动部署脚本

    1.功能描述 1. 安装支持包,从软件源下载自定义的NGINX包,创建NGINX用户和用户组. 2. 安装并初始化NGINX配置. 3. 运行NGINX并检测运行状态. 2.实现 源码如下: #!/b ...

  8. tomcat自动部署脚本

    为什么80%的码农都做不了架构师?>>> # !bin/bashlog_time=`date +%Y%m%d%H%M%S` tomcat_home=/usr/local/zmqsd/ ...

  9. 利用Git搭建自动部署的Laravel环境

    目标:服务器上搭建Laravel环境,本地使用IDE进行开发,使用Homestead做本地调试环境,代码提交后自动部署到服务器Root目录下. 下面是整个流程的示意图: 1. 准备工作,搭建LNMP环 ...

  10. gitlab 钩子 php,gitlab通过webhook.php自动部署标签

    //git webhook 自动部署脚本 $savePath = "/home/house/wxorder/"; $requestBody = file_get_contents( ...

最新文章

  1. 什么是线程安全,你真的了解吗?
  2. flowable 任务节点多实例使用
  3. 决策树之前要不要处理缺失值_不要使用这样的决策树
  4. 二级mysql教程下载_全国计算机等级考试教程:二级MySQL数据库程序设计
  5. C++笔记整理(参考整理自各大博客)
  6. Android新增usb Audio(mic)设备
  7. vim格式化代码实际上就是 缩进代码, 命令是等号=
  8. 卡方分布分位数_数理统计第五讲(三大分布)
  9. 题目264-国王的魔镜
  10. 文字处理技术:文字形状绕排不是挺简单的事吗,怎么搞得这么复杂
  11. 【密码学基础】02 数论基础
  12. 文件MD5查看linuxwindows
  13. 应用程序图标_如何制作完美的应用程序图标
  14. 在哪个平台可以自助打印文件资料
  15. ClickHouse在各大厂的最佳实践
  16. 从高通手动搜网代码流程看sd脚本的执行
  17. 荣耀50手机系列正式发布,售价2399元起,主要竞争对手瞄准苹果
  18. HTML+H5基础——常用标签
  19. 信息学奥赛对大学计算机专业,区别大盘点:信息学竞赛、信息学奥赛、NOI和IOI傻傻分不清楚...
  20. vn.py项目安装经验分享

热门文章

  1. 无线通信基础(一):高斯随机变量
  2. Dijkstra + 堆优化
  3. 【2019南京icpc网络赛 I】Washing clothes【李超树】
  4. 【Gym 102134-E】Kth subtree【权值树状数组、二分统计第k大+dfs离线操作】
  5. leetcode 65. Valid Number
  6. 【J2EE规范】什么是JNDI
  7. 编写java程序手动挡car_阅读下列说明、图和Java代码,填补空缺。[说明] 已知对某载客车辆(Car)进行类建模,如图13-2所示,其 - 赏学吧...
  8. linux wc -l命令,Linux wc sort和uniq的用法
  9. u盘启动盘安装centos7.5操作系统
  10. 2015年热恋的肉肉们