自己开发shell脚本实现一键化安装。
一、说明在现实环境中可能需要批量部署服务器,那么在我们已经部署好一台服务以后如果实现剩下的服务批量安装呢:
使用shell能否实现功能:
假设我们要部署lamp或者是lnmp如何实现脚本部署?
使用以下代码可实现:
部署方法1:
#!/bin/sh
menu ( ){ cat<<END
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want:
END }
menu
read num
echo "you choice $num"
if [ "$num" -eq ]
then
echo "begin install lamp"
/bin/sh /server/scripts/test///install-lamp.sh
if [ $? -eq ]
then
echo "lamp is been install"
exit
else
echo "lamp install error"
exit
fi
elif [ "$num" -eq ]
then
echo "begin install lamp"
/bin/sh /server/scripts/test///install-lnmp.sh
if [ $? -eq ]
then
echo "lnmp is been install"
exit
else
echo "lnmp install error"
exit
fi elif [ "$num" -eq ]
then
echo "logout"
exit
fi
fi
部署方法1
测试:
[root@localhost script]# sh manu.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
begin install lamp
/bin/sh: /server/scripts/test///install-lamp.sh: 没有那个文件或目录
lamp install error
[root@localhost script]# sh manu.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
begin install lamp
/bin/sh: /server/scripts/test///install-lnmp.sh: 没有那个文件或目录
lnmp install error
[root@localhost script]# sh manu.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
logout #由于真正的安装脚本没有开发所以每次执行安装都会报错没有文件或者目录,生产环境中将安装脚本写进去可实现一键化安装。
测试
部署方法2:
#!/bin/sh
menu ( ){ cat<<END
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want:
END }
menu
read num
echo "you choice $num" function lamp(){
if [ "$num" -eq ]
then
echo "begin install lamp"
/bin/sh /server/scripts/test///install-lamp.sh
if [ $? -eq ]
then
echo "lamp is been install"
exit else
echo "lamp install error"
exit
fi
fi
} function lnmp(){
if [ "$num" -eq ]
then
echo "begin install lamp"
/bin/sh /server/scripts/test///install-lnmp.sh
if [ $? -eq ]
then
echo "lnmp is been install"
exit
else
echo "lnmp install error"
exit
fi
fi
}
function quit(){
if [ "$num" -eq ]
then
echo "logout..."
exit
fi
}
case $num in
)
lamp
;;
)
lnmp
;;
)
quit
;; *)
echo "USAG:start|stop|restart|status"
esac
部署方法2
测试:
[root@localhost script]# sh manu2.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
begin install lamp
/bin/sh: /server/scripts/test///install-lamp.sh: 没有那个文件或目录
lamp install error
[root@localhost script]# sh manu2.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
begin install lamp
/bin/sh: /server/scripts/test///install-lnmp.sh: 没有那个文件或目录
lnmp install error
[root@localhost script]# sh manu2.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
logout...
[root@localhost script]# sh manu2.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want:
asdf
you choice asdf
USAG:start|stop|restart|status
方法2测试
经过测试方法2更为优雅,当然其实方法一也能实现方法2中输入错误给个提示。
三、编写一键化安装脚本:
1、LAMP安装这里以只安装apache为例子:
#!/bin/sh
#############################################
# this script is created by xuxuedong. #
# e_mail:@qq.com #
# qqinfo: #
# This install LAMP for auto. #
# version:1.1 #
#############################################
. /etc/init.d/functions
#set env
export PATH=$PATH:/bin:/sbin:/usr/sbin
export LANG="zh_CN.GB18030"
####define CMD and INstall_path
Instal_path=/application/
APP_PACKAGE_PATH=/home/tools
HTTP_RPM=`rpm -qa http*`
mkdir -p ${Instal_path}
if [ `$HTTP_RPM|wc -l` -gt ]
then
for n in $HTTP_RPM;do rpm -e --nodeps $n;done
else
echo "begin install LAMP."
fi
if [ ! -e ${APP_PACKAGE_PATH} ]
then
mkdir ${APP_PACKAGE_PATH}
else
yum install gcc* -y
cd ${APP_PACKAGE_PATH}
wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.32.tar.gz
fi if [ $? -eq ]
then
tar -zxf httpd-2.2..tar.gz
if [ $? -eq ]
then
cd httpd-2.2.
./configure --prefix=/application/apache.2.2. --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-deflate --enable-rewrite
fi
if [ $? -eq ]
then
make
make install
fi
if [ $? -eq ]
then
ln -s /application/apache.2.2. /application/apache
fi
fi
以安装apache为例子
注明在第二项的一键化安装部署的部署脚本路径上/bin/sh /server/scripts/test///install-lamp.sh 给的是实际的脚本路径。
实际路径:
[root@localhost script]# ls | grep lamp
lamp.sh
[root@localhost script]# pwd
/server/script
所以需要将/bin/sh /server/scripts/test///install-lamp.sh修改成/bin/sh /server/script/lamp.sh
测试:

证明已经开始安装。
安装结果已经完成了apache的安装,剩下的mysql和php环境安装也可参考脚本:

备注:以上脚本可根据实际情况优化,例如:不需要在屏幕中输出,只在有报错是输出报错情况,安装完成输出安装成功即可。此部署脚步前期最好是能确定需要那些关联库,执行安装前最好是所有的安装环境已经准备好了。以上脚本只做为参考,部分需要根据实际情况开发脚本。劲量不要照搬。
自己开发shell脚本实现一键化安装。的更多相关文章
- 编写shell脚本实现一键创建KVM虚拟机
shell脚本一键创建虚拟机 代码如下: #!/bin/bashname=$1 #把位置变量$1重新定义为name(创建虚拟机的名字)path1=/var/lib/libvirt/images/ #i ...
- idea开发shell脚本并运行
参考:https://blog.csdn.net/u012443641/article/details/81295999 IEDA中的bashsupport插件支持在IDEA中编写shell脚本文件, ...
- shell脚本之nginx的安装
为了编写nginx自动部署的脚本而刚学习的shell脚本语言.写文章只是为了记录,有错误勿喷. 一.创建shell脚本程序 操作系统是Linux的 CentOS 7 版本. ...
- 写了shell脚本想一键启动三台虚拟机的Zookeeper,却不知道为啥总是启动不了
首先,一键启动的shell脚本是这样的 #! /bin/bash case $1 in "start"){ for i in node01 node02 node03 do ssh ...
- linux shell脚本使用结构化命令
内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...
- 【shell脚本】一键部署LNMP===deploy.sh
一键部署mysql,php,nginx,通过源码安装部署 #!/bin/bash # 一键部署 LNMP(源码安装版本) menu() { clear echo " ############ ...
- shell脚本之一键部署openV~P~N
提前准备:/root目录下: checkpsw.sh ## 官方提供的自定义脚本,可在http://openvpn.se/files/other/checkpsw.sh下载 openvpn@.serv ...
- shell脚本之结构化命令if...then...fi
if的用法日常主要用于数值或者字符串的比较来实现结构化的,模拟人脑,就是如果遇到什么事情,我们应该做什么 语法格式分为 1. if command;then command;fi (如果if满足 ...
- linux shell脚本使用结构化命令(2)
一.for命令 二.while命令 三.until命令 1.for命令基本格式 for var in list do commands done oracle@suse:~/testshell> ...
随机推荐
- Tkinter图片按钮
imgBtn = tk.PhotoImage(file='test.png') tk.Button(image=imgBtn).pack() tk.mainloop() 转载,来源简书评论,地址:ht ...
- HihoCoder1576 子树中的最小权值( dfs序 +线段树 || 树剖)
给定一棵N个节点的树,编号1~N.其中1号节点是根,并且第i个节点的权值是Vi. 针对这棵树,小Hi会询问小Ho一系列问题.每次小Hi会指定一个节点x,询问小Ho以x为根的子树中,最小的权值是多少.为 ...
- Java笔记(四)
13. 集合框架: 集合中存储的都是对象的引用(地址) 迭代器:集合的取出元素的方式 import java.util.ArrayList; import java.util.Iterator; pu ...
- wx:for wx:for-items wx:for-item
data:{ arr:[1,2,3,4,5], arrs:[[1,2,3,4,5],[1,2,3,4,5]] }wx:for 用于循环数组 默认数组的当前项的下标变量名默认为 index,数组当 ...
- 2014年将会受欢迎的IT技能--你有多少哪?
据国外媒体报道,据Global Knowledge等十几家研究机构发布的2014年IT技能和薪金调查报告显示,2014年最受欢迎的十大IT技能如下: 1.编程与应用开发 据美国劳工统计局称,开发者和程 ...
- python--numpy模块、spicy模块、 matplotlib模块
一:numpy模块 ndarray:存储单一数据类型的多维数组 ufunc:能够对数组进行处理的函数(universal function object) #numpy 中arange用法,指定开始值 ...
- Codeforces 1108F MST Unification MST + LCA
Codeforces 1108F MST + LCA F. MST Unification Description: You are given an undirected weighted conn ...
- c语言中#和##的用法
一.一般用法 我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起. 用法: #include<cstdio> #include<climits> using nam ...
- 获取DataGridView上选中的一行并转换为一个DataRow类型
ataGridViewRow gridrow = dataGridView1.SelectedRows[0]; DataRowView row_view = (DataRowView)gridrow. ...
- Fitnesse 之 Script Table
在表中每一行代表一个执行脚本. 第一行中的Script关键字表明表格类型,后面紧跟着类名(Fixture)和构造函数中的参数.在一个测试页中如果没有再指定其它Fixture,将一直沿用上一个Fixtu ...