自己开发shell脚本实现一键化安装。

自己开发shell脚本实现一键化安装。

一、说明在现实环境中可能需要批量部署服务器,那么在我们已经部署好一台服务以后如果实现剩下的服务批量安装呢:

   使用shell能否实现功能:

   假设我们要部署lamp或者是lnmp如何实现脚本部署?

  使用以下代码可实现:

  部署方法1:

  
 1 #!/bin/sh
 2 menu ( ){
 3 
 4 cat<<END
 5         1.[install lamp]
 6         2.[install lnmp]
 7         3.[exit]
 8         pls input the num that you want:
 9 END
10 
11 }
12 menu
13 read num
14 echo "you choice $num"
15 if [ "$num" -eq 1 ]
16       then
17         echo "begin install lamp"
18         /bin/sh /server/scripts/test///install-lamp.sh
19         if  [ $? -eq 0 ]
20             then
21                 echo "lamp is been install"
22                 exit 0
23          else
24                 echo "lamp install error"
25                 exit 0
26         fi
27     elif [ "$num" -eq 2 ]
28       then
29         echo "begin install lamp"
30         /bin/sh /server/scripts/test///install-lnmp.sh
31         if  [ $? -eq 0 ]
32              then
33                 echo "lnmp is been install"
34                 exit 0
35          else
36                 echo "lnmp install error"
37                 exit 0
38         fi
39 
40     elif [ "$num" -eq 3 ]
41         then
42         echo "logout"
43         exit 0
44     fi
45 fi   
部署方法1

  测试:

  

  
 1 [root@localhost script]# sh manu.sh
 2         1.[install lamp]
 3         2.[install lnmp]
 4         3.[exit]
 5         pls input the num that you want:
 6 1
 7 you choice 1
 8 begin install lamp
 9 /bin/sh: /server/scripts/test///install-lamp.sh: 没有那个文件或目录
10 lamp install error
11 [root@localhost script]# sh manu.sh
12         1.[install lamp]
13         2.[install lnmp]
14         3.[exit]
15         pls input the num that you want:
16 2
17 you choice 2
18 begin install lamp
19 /bin/sh: /server/scripts/test///install-lnmp.sh: 没有那个文件或目录
20 lnmp install error
21 [root@localhost script]# sh manu.sh
22         1.[install lamp]
23         2.[install lnmp]
24         3.[exit]
25         pls input the num that you want:
26 3
27 you choice 3
28 logout
29 
30 #由于真正的安装脚本没有开发所以每次执行安装都会报错没有文件或者目录,生产环境中将安装脚本写进去可实现一键化安装。
测试

    部署方法2:

  
 1 #!/bin/sh
 2 menu ( ){  
 3 
 4 cat<<END
 5         1.[install lamp]
 6         2.[install lnmp]
 7         3.[exit]
 8         pls input the num that you want:
 9 END
10 
11 }
12 menu
13 read num
14 echo "you choice $num"
15 
16 function lamp(){
17         if [ "$num" -eq 1 ] 
18                 then
19                         echo "begin install lamp"
20                         /bin/sh /server/scripts/test///install-lamp.sh
21                 if  [ $? -eq 0 ]
22                          then  
23                          echo "lamp is been install"
24                          exit 0
25 
26                 else 
27                         echo "lamp install error"
28                         exit 0
29                 fi
30         fi
31 }
32 
33 function lnmp(){
34         if [ "$num" -eq 2 ] 
35                 then
36                         echo "begin install lamp"
37                         /bin/sh /server/scripts/test///install-lnmp.sh
38                 if  [ $? -eq 0 ]
39                         then  
40                                 echo "lnmp is been install"
41                                 exit 0
42                  else 
43                         echo "lnmp install error"
44                         exit 0
45                 fi
46         fi
47 }
48 function quit(){
49         if [ "$num" -eq 3 ] 
50                 then 
51                         echo "logout..."
52                         exit 0
53         fi
54 }
55 case $num in
56         1)
57         lamp
58 ;;
59         2)
60         lnmp
61 ;;
62         3)
63         quit
64 ;;
65 
66         *)
67         echo "USAG:start|stop|restart|status"
68 esac 
部署方法2

  测试:

  

  
 1 [root@localhost script]# sh  manu2.sh  
 2         1.[install lamp]
 3         2.[install lnmp]
 4         3.[exit]
 5         pls input the num that you want:
 6 1
 7 you choice 1
 8 begin install lamp
 9 /bin/sh: /server/scripts/test///install-lamp.sh: 没有那个文件或目录
10 lamp install error
11 [root@localhost script]# sh  manu2.sh 
12         1.[install lamp]
13         2.[install lnmp]
14         3.[exit]
15         pls input the num that you want:
16 2
17 you choice 2
18 begin install lamp
19 /bin/sh: /server/scripts/test///install-lnmp.sh: 没有那个文件或目录
20 lnmp install error
21 [root@localhost script]# sh  manu2.sh 
22         1.[install lamp]
23         2.[install lnmp]
24         3.[exit]
25         pls input the num that you want:
26 3
27 you choice 3
28 logout...
29 [root@localhost script]# sh  manu2.sh 
30         1.[install lamp]
31         2.[install lnmp]
32         3.[exit]
33         pls input the num that you want:
34 asdf
35 you choice asdf
36 USAG:start|stop|restart|status
方法2测试

  经过测试方法2更为优雅,当然其实方法一也能实现方法2中输入错误给个提示。

三、编写一键化安装脚本:

  1、LAMP安装这里以只安装apache为例子:

  

  
 1 #!/bin/sh
 2 #############################################
 3 # this script is created by xuxuedong.                                   #
 4 # e_mail:365686746@qq.com                                              #
 5 # qqinfo:365686746                                                           #
 6 # This install LAMP  for auto.                                                #
 7 # version:1.1                                                                     #
 8 #############################################
 9 . /etc/init.d/functions
10 #set env
11 export PATH=$PATH:/bin:/sbin:/usr/sbin
12 export LANG="zh_CN.GB18030"
13 ####define CMD  and INstall_path
14 Instal_path=/application/
15 APP_PACKAGE_PATH=/home/tools
16 HTTP_RPM=`rpm -qa http*`
17 mkdir -p ${Instal_path}
18 if [ `$HTTP_RPM|wc -l` -gt 0 ]
19         then
20                 for n in $HTTP_RPM;do rpm -e --nodeps $n;done
21         else 
22                 echo "begin install LAMP."
23 fi
24 if [ ! -e ${APP_PACKAGE_PATH} ]  
25         then
26                 mkdir ${APP_PACKAGE_PATH}
27         else
28                 yum install gcc* -y
29                 cd ${APP_PACKAGE_PATH}
30                 wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.32.tar.gz
31 fi
32 
33 if [ $? -eq 0 ]
34         then 
35                 tar -zxf  httpd-2.2.32.tar.gz
36         if [ $? -eq 0 ]
37                 then
38                         cd httpd-2.2.32
39                         ./configure --prefix=/application/apache.2.2.32 --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-deflate --enable-rewrite
40         fi 
41         if [ $? -eq 0 ]
42                 then
43                         make
44                         make install
45         fi
46         if [ $? -eq 0 ]
47                 then
48                         ln -s /application/apache.2.2.32 /application/apache
49         fi
50 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

  测试:

  自己开发shell脚本实现一键化安装。_第1张图片

     证明已经开始安装。

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

  自己开发shell脚本实现一键化安装。_第2张图片

  

  备注:以上脚本可根据实际情况优化,例如:不需要在屏幕中输出,只在有报错是输出报错情况,安装完成输出安装成功即可。此部署脚步前期最好是能确定需要那些关联库,执行安装前最好是所有的安装环境已经准备好了。以上脚本只做为参考,部分需要根据实际情况开发脚本。劲量不要照搬。

  

 

 

  

posted on 2017-04-17 15:03  Steward_Xu 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Steward-Xu/p/6722982.html

你可能感兴趣的:(自己开发shell脚本实现一键化安装。)