小Q:真正勇敢的人,应当能够智慧地忍受最难堪的荣辱,不以身外的荣辱介怀

      ,用息事宁人的态度避免无谓的横祸。                 —— 莎士比亚


=======================================================================

学习了shell编程,我们知道它可以实现各种自动化,自动告警,自动执行多条命令,自动

监控某个服务,但是所有的一切都要基于我们会写shell脚本,而这是个时间积累的过程,

操作积累的过程,多写多练才能熟练;而有了这些后,你还得有思路,才能成功写出你需

要的任何脚本;

接下来我们编写一个比较大的脚本:一键搭建LAMP/LNMP架构;


思路:两种架构需要的软件就包含六种,首先模块化必不可少;说白了两种架构的搭建就

      是安装安装对应的应用,所以每个应用的安装用一个函数代表,之后LAMP和LNMP各

      自建一个模块指定相应的安装顺序;

         如果实在没思路,就把每个程序的安装步骤写上去,每步都检查,逐渐排错,安装

      步骤应该都知道了吧,不知道的话就没法装这个了


注意:1.要想写好一个脚本,必须要在每步操作后都进行检查,这样才能与针对性的改错

        否则一篇几百行的脚本,出错后将无从查起;推荐创建一个检查模块;

      2.执行每步操作前,我们要养成质疑的习惯,先判断有没有什么情况可以导致他运

        行不了,要鸡蛋里挑骨头,你才能减少错误的发生;

      3.模块内只能有大小写字母,下划线,不能有减号;

      4.当变量与字符串比较时只能用==/>=/<=,不能用-eq等字符表示;



下面的程序,不会适合所有的机子,因为环境等等原因都会导致bug出现,仅供参考;

脚本来自于猿课阿铭老师,我只是做了注释和一些修改,方便初学者理解;

vim  1.sh

#!/bin/bash
##auto install LAMP or LNMP
----------------------------------------------------------------------------------------
echo "It will install lamp or lnmp."
sleep 1                                       #打印上面字符串,休眠1秒

##check last command is OK or not.
check_ok() {                                  #定义检查模块,排错,特别重要
if [ $? != 0 ]
then
    echo "Error, Check the error log."        #如果有错,打印字符串,退出安装
    exit 1
fi
}

##get the archive of the system,i686 or x86_64.  
ar=`arch`                                     #arch检查系统版本

##close seliux                                #关闭防火墙
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config  #更改配置文件
selinux_s=`getenforce`          #因为该配置文件重启才能生效,我们又不能重启
if [ $selinux_s == "Enforcing"  -o $selinux_s == "enforcing" ]   #判断selinux状态
then                            #接上面话题,所以要临时关闭一下
    setenforce 0                #setenforce 0 不能随便写,只有满足上面条件才可以
fi
##close iptables
iptables-save > /etc/sysconfig/iptables_`date +%s` #相当于下面保存规则的执行效果
iptables -F                                        #清空iptables
service iptables save

##if the packge installed ,then omit.
myum() {                                            #定义myum模块
if ! rpm -qa|grep -q "^$1"                          #检索$1有没有安装,$1下面有调用
then
    yum install -y $1                               #没有安装,则执行yum安装
    check_ok                                        #检查有无错
else
    echo $1 already installed.                      #调用的$1已经被安装
fi
}

## install some packges.          #安装一些包预防万一,也可以在出错后安装
for p in gcc wget perl perl-devel libaio libaio-devel pcre-devel zlib-devel
do
    myum $p                                         #安装上述包,调用myum模块
done

##install epel.                                     #安装epel yum扩展源
if rpm -qa epel-release >/dev/null                  #输出内容写入黑洞,永久丢失
then
    rpm -e epel-release                             #删除包
fi
if ls /etc/yum.repos.d/epel-6.repo* >/dev/null 2>&1 #说白了就是将所有文件删除掉
then
    rm -f /etc/yum.repos.d/epel-6.repo*
fi
wget -P /etc/yum.repos.d/    #安装

#---------------------------------- myaql/php/httpd ----------------------------------# 
##function of installing mysqld.
install_mysqld() {                                  #创建模块
echo "Chose the version of mysql."                  #交互显示
select mysql_v in 5.1 5.6                           #一个选择的命令,类似for
do
    case $mysql_v in                                #case判断语句
        5.1)                                        #注意要用1或2进行选择
            cd /usr/local/src
            [ -f mysql-5.1.72-linux-$ar-glibc23.tar.gz ] || wget   #||指前面正确,后面不执行 
            tar zxf mysql-5.1.72-linux-$ar-glibc23.tar.gz
            check_ok
            [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_`date +%s`                                                   #&&指前面正确,后面才执行
            mv mysql-5.1.72-linux-$ar-glibc23 /usr/local/mysql    #因为版本格式,加$ar
            check_ok
            if ! grep '^mysql:' /etc/passwd          #判断是否有mysql用户 
            then
                useradd -M mysql -s /sbin/nologin
                check_ok
            fi
            myum compat-libstdc++-33                 #安装需要用到的包
            [ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_`date +%s`
            mkdir -p /data/mysql                     #判断并新建数据库目录
            chown -R mysql:mysql /data/mysql         #改权限
            cd /usr/local/mysql
            ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
            check_ok                                 #编译安装
            /bin/cp support-files/my-huge.cnf /etc/my.cnf 
            check_ok                                 #拷贝配置文件
            sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf #\a指在后面加一行
            /bin/cp support-files/mysql.server /etc/init.d/mysqld   #拷贝启动脚本
            sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld #替换,指定路径
            chmod 755 /etc/init.d/mysqld 
            chkconfig --add mysqld
            chkconfig mysqld on                
            service mysqld start                     #更改权限,开机自启,启动服务
            check_ok
            break
            ;;                                       #break结束本模块,;;结束符
        5.6)                        
            cd /usr/local/src                        #安装原理一样,更改版本号就ok
            [ -f mysql-5.6.26-linux-glibc2.5-$ar.tar.gz ] || wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.26-linux-glibc2.5-$ar.tar.gz
            tar zxf mysql-5.6.26-linux-glibc2.5-$ar.tar.gz
            check_ok
            [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_bak
            mv mysql-5.6.26-linux-glibc2.5-$ar /usr/local/mysql
            if ! grep '^mysql:' /etc/passwd
            then
                useradd -M mysql -s /sbin/nologin
            fi
            myum compat-libstdc++-33
            [ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_bak
            mkdir -p /data/mysql
            chown -R mysql:mysql /data/mysql
            cd /usr/local/mysql
            ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
            check_ok                                     #编译方式,同其他不一样
            /bin/cp support-files/my-default.cnf /etc/my.cnf
            check_ok
            sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
            /bin/cp support-files/mysql.server /etc/init.d/mysqld
            sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld
            chmod 755 /etc/init.d/mysqld
            chkconfig --add mysqld
            chkconfig mysqld on
            service mysqld start
            check_ok
            break
            ;;
         *)
            echo "only 1(5.1) or 2(5.6)"
            exit 1                                 #非以上两版本,输出内容,直接退出
            ;;
    esac
done
}

##function of install httpd.
install_httpd() {                                  #安装Apache
echo "Install apache version 2.2."                 #大体方式同上一样
cd /usr/local/src
[ -f httpd-2.2.16.tar.gz ] || wget  http://syslab.comsenz.com/downloads/linux/httpd-2.2.16.tar.gz
tar zxf  httpd-2.2.16.tar.gz && cd httpd-2.2.16
check_ok
./configure \
--prefix=/usr/local/apache2 \
--with-included-apr \
--enable-so \
--enable-deflate=shared \
--enable-expires=shared \
--enable-rewrite=shared \
--with-pcre
check_ok
make && make install                               #源码包要编译安装
check_ok
}
##function of install lamp's php.                  #安装LAMP中的php
install_php() {                            
echo -e "Install php.\nPlease chose the version of php." #-e 有转义作用,使\n代表回车
select php_v in 5.4 5.6
do
    case $php_v in
        5.4)
            cd /usr/local/src/
            [ -f php-5.4.45.tar.bz2 ] || wget 'http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror' -O php-5.4.45.tar.bz2
            tar jxf php-5.4.45.tar.bz2 && cd php-5.4.45
            for p in openssl-devel bzip2-devel \
            libxml2-devel curl-devel libpng-devel \
            libjpeg-devel freetype-devel libmcrypt-devel\
            libtool-ltdl-devel perl-devel
            do
                myum $p
            done
            check_ok
            ./configure \
            --prefix=/usr/local/php \
            --with-apxs2=/usr/local/apache2/bin/apxs \
            --with-config-file-path=/usr/local/php/etc  \
            --with-mysql=/usr/local/mysql \
            --with-libxml-dir \
            --with-gd \
            --with-jpeg-dir \
            --with-png-dir \
            --with-freetype-dir \
            --with-iconv-dir \
            --with-zlib-dir \
            --with-bz2 \
            --with-openssl \
            --with-mcrypt \
            --enable-soap \
            --enable-gd-native-ttf \
            --enable-mbstring \
            --enable-sockets \
            --enable-exif \
            --disable-ipv6
            check_ok
            make && make install                     #其实完全可以把版本模块化
            check_ok
            [ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production  /usr/local/php/etc/php.ini
            break
            ;;
        5.6)
            cd /usr/local/src/
            [ -f php-5.6.6.tar.gz ] || wget http://mirrors.sohu.com/php/php-5.6.6.tar.gz
            tar zxf php-5.6.6.tar.gz &&   cd php-5.6.6
            for p in openssl-devel bzip2-devel \
            libxml2-devel curl-devel libpng-devel \
            libjpeg-devel freetype-devel libmcrypt-devel\
            libtool-ltdl-devel perl-devel
            do
                myum $p
            done
            ./configure \
            --prefix=/usr/local/php \
            --with-apxs2=/usr/local/apache2/bin/apxs \
            --with-config-file-path=/usr/local/php/etc  \
            --with-mysql=/usr/local/mysql \
            --with-libxml-dir \
            --with-gd \
            --with-jpeg-dir \
            --with-png-dir \
            --with-freetype-dir \
            --with-iconv-dir \
            --with-zlib-dir \
            --with-bz2 \
            --with-openssl \
            --with-mcrypt \
            --enable-soap \
            --enable-gd-native-ttf \
            --enable-mbstring \
            --enable-sockets \
            --enable-exif \
            --disable-ipv6
            check_ok
            make && make install
            check_ok
            [ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production  /usr/local/php/etc/php.ini
            break
            ;;
        *)
            echo "only 1(5.4) or 2(5.6)"
            ;;
    esac
done
}
##function of apache and php configue.          #Apache与php结合
join_apa_php() {
sed -i '/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php .php' /usr/local/apache2/conf/httpd.conf                                 #在Apache配置文件AddType后添加一行
check_ok
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html index.htm/' /usr/local/apache2/conf/httpd.conf                   #替换,使之可以解析php htm
check_ok
cat > /usr/local/apache2/htdocs/index.php <<EOF #把下面内容定向到文件
<?php
   phpinfo();
?>
EOF
if /usr/local/php/bin/php -i |grep -iq 'date.timezone => no value'
then
    sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"'  /usr/local/php/etc/php.ini                                          #-i真实更改内容,而不是检索显示
fi
/usr/local/apache2/bin/apachectl restart        #重启
check_ok
}
#--------------------------------check service running----------------------------------
##function of check service is running or not, example nginx, httpd, php-fpm.
check_service() {
if [ "$1" == "phpfpm" ]                         #用s代表php-fpm,而不是phpfpm
then                                            #因为搜索进程phpfpm没结果
    s="php-fpm"                                 #而且上面模块内不能有-,所以在这
else
    s=$1
fi
n=`ps aux |grep "$s"|wc -l`                     #查看php的进程数,大于1则说明已经启动
if [ $n -gt 1 ]
then
    echo "$1 service is already started."
else
    if [ -f /etc/init.d/$1 ]                    #查看有没有ph-fpm文件
    then
        /etc/init.d/$1 start                    #悠久开启
        check_ok
    else
        install_$1                              #没有的话返回安装
    fi
fi
}
##function of install lamp                      #安装lamp,注意顺序
lamp() {
check_service mysqld
check_service httpd
install_php
join_apa_php
echo "LAMP done,Please use 'http://your ip/index.php' to access."
}
#---------------------------------INSTALL LNMP---------------------------------------# 
##function of install nginx
install_nginx() {
cd /usr/local/src
[ -f nginx-1.8.0.tar.gz ] || wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxf nginx-1.8.0.tar.gz
cd nginx-1.8.0
myum pcre-devel
./configure --prefix=/usr/local/nginx
check_ok
make && make install
check_ok
if [ -f /etc/init.d/nginx ]
then
    /bin/mv /etc/init.d/nginx  /etc/init.d/nginx_`date +%s`
fi
curl http://www.apelearn.com/study_v2/.nginx_init  -o /etc/init.d/nginx
check_ok
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
curl http://www.apelearn.com/study_v2/.nginx_conf -o /usr/local/nginx/conf/nginx.conf
check_ok
service nginx start
check_ok
echo -e "<?php\n    phpinfo();\n?>" > /usr/local/nginx/html/index.php
check_ok
}
##function of install php-fpm
install_phpfpm() {
echo -e "Install php.\nPlease chose the version of php."
select php_v in 5.4 5.6
do
    case $php_v in
        5.4)
            cd /usr/local/src/
            [ -f php-5.4.45.tar.bz2 ] || wget 'http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror' -O php-5.4.45.tar.bz2
            tar jxf php-5.4.45.tar.bz2 && cd php-5.4.45
            for p in  openssl-devel bzip2-devel \
            libxml2-devel curl-devel libpng-devel \
            libjpeg-devel freetype-devel libmcrypt-devel\
            libtool-ltdl-devel perl-devel
            do
                myum $p
            done
            if ! grep -q '^php-fpm:' /etc/passwd
            then
                useradd -M -s /sbin/nologin php-fpm
                check_ok
            fi
            ./configure \
            --prefix=/usr/local/php-fpm \
            --with-config-file-path=/usr/local/php-fpm/etc \
            --enable-fpm \
            --with-fpm-user=php-fpm \
            --with-fpm-group=php-fpm \
            --with-mysql=/usr/local/mysql \
            --with-mysql-sock=/tmp/mysql.sock \
            --with-libxml-dir \
            --with-gd \
            --with-jpeg-dir \
            --with-png-dir \
            --with-freetype-dir \
            --with-iconv-dir \
            --with-zlib-dir \
            --with-mcrypt \
            --enable-soap \
            --enable-gd-native-ttf \
            --enable-ftp \
            --enable-mbstring \
            --enable-exif \
            --enable-zend-multibyte \
            --disable-ipv6 \
            --with-pear \
            --with-curl \
            --with-openssl
            check_ok
            make && make install
            check_ok
            [ -f /usr/local/php-fpm/etc/php.ini ] || /bin/cp php.ini-production  /usr/local/php-fpm/etc/php.ini
            if /usr/local/php-fpm/bin/php -i |grep -iq 'date.timezone => no value'
            then
                sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"'  /usr/local/php-fpm/etc/php.ini
                check_ok
            fi
            [ -f /usr/local/php-fpm/etc/php-fpm.conf ] || curl http://www.apelearn.com/study_v2/.phpfpm_conf -o /usr/local/php-fpm/etc/php-fpm.conf
            [ -f /etc/init.d/phpfpm ] || /bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm
            chmod 755 /etc/init.d/phpfpm
            chkconfig phpfpm on
            service phpfpm start
            check_ok
            break
            ;;
        5.6)
            cd /usr/local/src/
            [ -f php-5.6.6.tar.gz ] || wget http://mirrors.sohu.com/php/php-5.6.6.tar.gz
            tar zxf php-5.6.6.tar.gz &&   cd php-5.6.6
            for p in  openssl-devel bzip2-devel \
            libxml2-devel curl-devel libpng-devel \
            libjpeg-devel freetype-devel libmcrypt-devel\
            libtool-ltdl-devel perl-devel
            do
                myum $p
            done
            if ! grep -q '^php-fpm:' /etc/passwd
            then
                useradd -M -s /sbin/nologin php-fpm
            fi
            check_ok
            ./configure \
            --prefix=/usr/local/php-fpm \
            --with-config-file-path=/usr/local/php-fpm/etc \
            --enable-fpm \
            --with-fpm-user=php-fpm \
            --with-fpm-group=php-fpm \
            --with-mysql=/usr/local/mysql \
            --with-mysql-sock=/tmp/mysql.sock \
            --with-libxml-dir \
            --with-gd \
            --with-jpeg-dir \
            --with-png-dir \
            --with-freetype-dir \
            --with-iconv-dir \
            --with-zlib-dir \
            --with-mcrypt \
            --enable-soap \
            --enable-gd-native-ttf \
            --enable-ftp \
            --enable-mbstring \
            --enable-exif \
            --disable-ipv6 \
            --with-pear \
            --with-curl \
            --with-openssl
            check_ok
            make && make install
            check_ok
            [ -f /usr/local/php-fpm/etc/php.ini ] || /bin/cp php.ini-production  /usr/local/php-fpm/etc/php.ini
            if /usr/local/php-fpm/bin/php -i |grep -iq 'date.timezone => no value'
            then
                sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"'  /usr/local/php-fpm/etc/php.ini
                check_ok
            fi
            [ -f /usr/local/php-fpm/etc/php-fpm.conf ] || curl http://www.apelearn.com/study_v2/.phpfpm_conf -o /usr/local/php-fpm/etc/php-fpm.conf
            check_ok
            [ -f /etc/init.d/phpfpm ] || /bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm
            chmod 755 /etc/init.d/phpfpm
            chkconfig phpfpm on
            service phpfpm start
            check_ok
            break
            ;;
        *)
            echo 'only 1(5.4) or 2(5.6)'
            ;;
    esac
done
}
##function of install lnmp
lnmp() {
check_service mysqld
check_service nginx
check_service phpfpm
echo "The lnmp done, Please use 'http://your ip/index.php' to access."
}
#--------------------------------SELECT LAMP OR LNMP-----------------------------------#
read -p "Please chose which type env you install, (lamp|lnmp)? " t
case $t in
    lamp)
        lamp
        ;;
    lnmp)
        lnmp
        ;;
    *)
        echo "Only 'lamp' or 'lnmp' your can input."
        ;;
esac


=========================================================================================

体系结构:检查模块                                               

            |                                其实重要的就是上边几个模块不好想

       检查系统版本                         不过让每个应用的安装顺序在你脑中

            |                                多过几遍,然后直接写他们安装步骤

        关闭防火墙                           模块化,后面的几个模块是没问题,

            |                                其实过程中,关闭防火墙,安装需要

        yum安装模块                          的包,安装扩展源,你都会在经检查

            |                                模块,检查出来的

     安装备用包和扩展源

            |

  mysql.nttpd.php安装模块

            |

 结合php与http.lamp安装模块

            |

   nginx.php.lnmp安装模块                   最重点的目标在底层,我们业可以反向

            |                               的想,安装这个需要什么,一步步列出

 一键选择lamp/lnmp安装模块                  来,然后流程图似的框架就有了