本文主要介绍打印选择菜单,按照选择项自动化一键部署lamp或lnmp环境的脚本


基本流程:

1.打印菜单脚本介绍

2.LAMP脚本介绍(带环境测试)

3.LNMP脚本介绍(带环境测试)




脚本一:打印菜单脚本介绍


[root@aliyun scripts]# cat mianshi11.sh 

#!/bin/bash

path=/server/scripts

[ ! -d "$path" ] && mkdir $path -p

cat <<END                             #cat打印终端菜单选择

   1.[install lamp]

   2.[install lnmp]

   3.[exit]

   please input the num you want:

END

read num 

expr $num + 1 &>/dev/null

[ $? -ne 0 ] &&{                     #根据返回值进行判断

   echo "the num you input must be {1|2|3}"

   exit 1

}

[ $num -eq 1 ] &&{

   echo "start installing lamp."

   sleep 2;

   [ -x "$path/lamp.sh" ]||{      #判断脚本是否可执行,若不可执行则给予提示。

      echo "$path/lamp.sh does not exist or can not be exec."

      exit 1 

   }

   $path/lamp.sh                     #绝对路径执行脚本

   #source $path/lamp.sh      #脚本中执行脚本,使用source比sh或不加解释器等更好一些

   exit $?

}

[ $num -eq 2 ] &&{

   echo "start installing LNMP."

   sleep 2;

   [ -x "$path/lnmp.sh" ]||{    #判断脚本是否可执行,若不可执行则给予提示。

      echo "$path/lnmp.sh does not exist or can not be exec."

      exit 1 

   }

   $path/lnmp.sh

   #source $path/lnmp.sh   #脚本中执行脚本,使用source比sh或不加解释器等更好一些

   exit $?

}

[ $num -eq 3 ] &&{

   echo bye

   exit 3

}

#这里有三种用户的输入不等于1,2 或 3的综合用法。

[[ ! $num =~ [1-3] ]] &&{   #[[]]的正则匹配方法。

   echo "the num you input must be {1|2|3}"

   echo "Input ERROR"

   exit 4

}


#以上脚本直接拷贝复制即可用,如有写得不好的地方欢迎点评




脚本二:搭建LAMP环境脚本介绍



[root@localhost scripts]# cat /server/scripts/lamp.sh 

#!/bin/bash

#this scripts is created by ywxi at 2018-05-11

echo LAMP is installed...............

echo -e "\033[32m "###############步骤一:安装apache###############" \033[0m"  #打印安装步骤提示

if [ -f /etc/sysconfig/selinux ]  

then  

    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux    #永久关闭SElinux

    setenforce 0                                                                                                      #临时关闭SElinux

fi 


# Check if user is root                                                

if [ "$UID" -ne 0 ]  

then  

    printf "Error: You must be root to run this script!\n"  

    exit 1  

fi 


yum -y install httpd  2>&1 >/dev/null

if [ `rpm -qa httpd|wc -l` -eq 1 ];then                                          #检查rpm包是否安装成功

    echo -e "\033[32m "httpd installed sucessed" \033[0m"    

  else

    echo -e  "\033[46;31m "httpd installed faild" \033[0m"

    exit 1

fi

for rpm in "httpd-manual" "mod_ssl" "mod_perl" "mod_auth_mysql"         #安装http相关模块包

 do

    yum -y install $rpm   2>&1 >/dev/null

    if [ `rpm -qa $rpm|wc -l` -eq 1 ];then

       echo -e "\033[32m "$rpm installed sucessed" \033[0m"

    else

       echo -e  "\033[46;31m "$rpm installed faild" \033[0m"

    fi

done

/etc/init.d/httpd start  

netstat -tnlp|grep --color 80

chkconfig httpd on

sed 10a"-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT "  /etc/sysconfig/iptables -i

/etc/init.d/iptables restart


echo " "

echo -e "\033[32m "###############步骤二:安装mysql###############" \033[0m"


wget https://repo.mysql.com//mysql57-community-release-el6-11.noarch.rpm &>/dev/null


if [ `echo $?` -eq 0 ];then

    echo -e "\033[32m "mysql57-community-release-el6-11.noarch.rpm installed sucessed" \033[0m"

  else

    echo -e  "\033[46;31m "mysql57-community-release-el6-11.noarch.rpm installed faild" \033[0m" 

fi  


rpm -Uvh mysql57-community-release-el6-11.noarch.rpm  &>/dev/null  #安装mysql的yum源


if [ `ls /etc/yum.repos.d/|grep mysql|wc -l` -ge 2 ];then                          #查看mysql源是否成功

    echo -e "\033[32m "mysql源下载成功" \033[0m"

  else

    echo -e  "\033[46;31m "mysql源下载失败" \033[0m"

fi

echo "mysql installing....."                                                                        #打印安装mysql-community-server安装包的提示,下载需要一点时间

yum -y install mysql-community-server &>/dev/null

if [ `rpm -qa mysql-community-server|wc -l` -eq 1 ];then                      #检查安装包是否安装成功

    echo -e "\033[32m "mysql安装包 installed sucessed" \033[0m"       

  else

    echo -e  "\033[46;31m "mysql安装包 installed faild" \033[0m"

    exit 1

fi

mysql -V                                                                                                #查看版本,版本一般都是中等偏高

/etc/init.d/mysqld start 

netstat -tnlp |grep 3306

mysqlpasswd=`grep 'temporary password' /var/log/mysqld.log |cut -d: -f4`

mysql -uroot -p"$mysqlpasswd"  -e "set global validate_password_policy=0;" &>/dev/null              #修改mysql密码的限制

mysql -uroot -p"$mysqlpasswd"  -e "set global validate_password_length=1;" &>/dev/null

mysql -uroot -p"$mysqlpasswd"  -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'ywxi123';" &>/dev/null   #修改mysql登录密码,为下面测试做铺垫


chkconfig mysqld on

sed 10a"-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT "  /etc/sysconfig/iptables -i     

/etc/init.d/iptables restart


echo -e "\033[32m "###############步骤三:安装php###############" \033[0m"


rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm  &>/dev/null   #更新yum源

if [ `ls /etc/yum.repos.d/|grep mysql|wc -l` -ge 2 ];then

    echo -e "\033[32m "epel源下载成功" \033[0m"

  else

    echo -e  "\033[46;31m "epel源下载失败" \033[0m"

fi

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm &>/dev/null    #更新yum源

if [ `ls /etc/yum.repos.d/|grep remi|wc -l` -ge 6 ];then    

    echo -e "\033[32m "remi-php源下载成功" \033[0m"

  else

    echo -e  "\033[46;31m "remi-php源下载失败" \033[0m"

fi


yum -y install --enablerepo=remi --enablerepo=remi-php56 php php-bcmath php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-gd php-xml php-memcache php-redis php-fpm php-mysql php-common php-mssql &>/dev/null


echo $?

echo "###############上面检查命令是否执行成功###############"

sed 's#expose_php = On#expose_php = Off#g' /etc/php.ini -i

php -v

/etc/init.d/php-fpm start

chkconfig php-fpm on 


echo -e "\033[32m "###############步骤四:LAMP环境测试###############" \033[0m"

for fw in "httpd" "mysqld" "php-fpm"    #检查服务状态

do

  if [ `netstat -tnlpu|grep $fw|wc -l` -ge 1 ];then

    echo -e "\033[32m "$fw正常运行" \033[0m"

  else

    /etc/init.d/$fw start

  fi

done

netstat -tnlpu|egrep "3306|80|9000"

echo '<?php                                           #准备测试文件放在http的html目录下

$conn=mysql_connect("'127.0.0.1'","'root'","'ywxi123'");

if ($conn){

echo "LAMP platform connect to mysql is successful!";

}else{

echo "LAMP platform connect to mysql is failed!";

}

phpinfo();

?>' >>/var/www/html/index.php 

chown -R apache.apache /var/www/html   #授予目录权限

/etc/init.d/httpd restart

/etc/init.d/php-fpm restart

httpnum=`curl -I -m 10 -o /dev/null -s -w %{http_code}"\n" 127.0.0.1/index.php`  #检测返回状态,来判断访问是否成功

if [ $httpnum -eq 200 ];then

  echo -e "\033[32m "恭喜你,LAMP环境搭建成功" \033[0m"

else

  echo -e  "\033[46;31m "糟糕了,LAMP环境有点问题" \033[0m"

fi


#以上脚本直接拷贝复制即可用,如有写得不好的地方欢迎点评





脚本三:搭建LNMP环境脚本介绍


[root@localhost scripts]# cat /server/scripts/lamp.sh 

#!/bin/bash

#this scripts is created by ywxi at 2018-05-11

echo LNMP is installed...............

echo -e "\033[32m "###############步骤一:安装Nginx###############" \033[0m"

if [ -f /etc/sysconfig/selinux ]

then

    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

    setenforce 0

fi


# Check if user is root  

if [ "$UID" -ne 0 ]

then

    printf "Error: You must be root to run this script!\n"

    exit 1

fi

rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm   &>/dev/null   #使用yum安装nginx,安装nginx库

if [ `ls /etc/yum.repos.d/|grep nginx|wc -l` -ge 1 ];then

    echo -e "\033[32m "nginx源下载成功" \033[0m"

  else

    echo -e  "\033[46;31m "nginx源下载失败" \033[0m"

fi

yum -y install nginx  &>/dev/null

if [ `rpm -qa nginx|wc -l` -eq 1 ];then       #检查nginx的rpm包

    echo -e "\033[32m "nginx安装包 installed sucessed" \033[0m"

  else

    echo -e  "\033[46;31m "nginx安装包 installed faild" \033[0m"

    exit 1

fi

nginx -v

/etc/init.d/nginx start

netstat -tnlp|grep --color 80

chkconfig httpd on

sed 10a"-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT "  /etc/sysconfig/iptables -i

/etc/init.d/iptables restart


echo " "

echo -e "\033[32m "###############步骤二:安装mysql###############" \033[0m"


wget https://repo.mysql.com//mysql57-community-release-el6-11.noarch.rpm &>/dev/null


if [ `echo $?` -eq 0 ];then

    echo -e "\033[32m "mysql57-community-release-el6-11.noarch.rpm installed sucessed" \033[0m"

  else

    echo -e  "\033[46;31m "mysql57-community-release-el6-11.noarch.rpm installed faild" \033[0m" 

fi  


rpm -Uvh mysql57-community-release-el6-11.noarch.rpm  &>/dev/null


if [ `ls /etc/yum.repos.d/|grep mysql|wc -l` -ge 2 ];then

    echo -e "\033[32m "mysql源下载成功" \033[0m"

  else

    echo -e  "\033[46;31m "mysql源下载失败" \033[0m"

fi

echo "mysql installing....."

yum -y install mysql-community-server &>/dev/null

if [ `rpm -qa mysql-community-server|wc -l` -eq 1 ];then

    echo -e "\033[32m "mysql安装包 installed sucessed" \033[0m"

  else

    echo -e  "\033[46;31m "mysql安装包 installed faild" \033[0m"

    exit 1

fi

mysql -V

/etc/init.d/mysqld start 

netstat -tnlp |grep 3306

mysqlpasswd=`grep 'temporary password' /var/log/mysqld.log |cut -d: -f4`

mysql -uroot -p"$mysqlpasswd"  -e "set global validate_password_policy=0;" &>/dev/null

mysql -uroot -p"$mysqlpasswd"  -e "set global validate_password_length=1;" &>/dev/null

mysql -uroot -p"$mysqlpasswd"  -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'ywxi123';" &>/dev/null


chkconfig mysqld on

sed 10a"-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT "  /etc/sysconfig/iptables -i

/etc/init.d/iptables restart


echo " "

echo -e "\033[32m "###############步骤三:安装php###############" \033[0m"


rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm  &>/dev/null

if [ `ls /etc/yum.repos.d/|grep mysql|wc -l` -ge 2 ];then

    echo -e "\033[32m "epel源下载成功" \033[0m"

  else

    echo -e  "\033[46;31m "epel源下载失败" \033[0m"

fi

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm &>/dev/null

if [ `ls /etc/yum.repos.d/|grep remi|wc -l` -ge 6 ];then

    echo -e "\033[32m "remi-php源下载成功" \033[0m"

  else

    echo -e  "\033[46;31m "remi-php源下载失败" \033[0m"

fi


yum -y install --enablerepo=remi --enablerepo=remi-php56 php php-bcmath php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-gd php-xml php-memcache php-redis php-fpm php-mysql php-common php-mssql &>/dev/null


echo $?

echo "###############上面检查命令是否执行成功###############"

sed 's#expose_php = On#expose_php = Off#g' /etc/php.ini -i

php -v

/etc/init.d/php-fpm start

chkconfig php-fpm on 


echo " "

echo -e "\033[32m "###############步骤四:LNMP环境测试###############" \033[0m"

for fw in "nginx" "mysqld" "php-fpm"

do

  if [ `netstat -tnlpu|grep $fw|wc -l` -ge 1 ];then

    echo -e "\033[32m "$fw正常运行" \033[0m"

  else

    /etc/init.d/$fw start

  fi

done

netstat -tnlpu|egrep "3306|80|9000"

echo '<?php

$conn=mysql_connect("'127.0.0.1'","'root'","'ywxi123'");

if ($conn){

echo "LNMP platform connect to mysql is successful!";

}else{

echo "LNMP platform connect to mysql is failed!";

}

phpinfo();

?>' >>/usr/share/nginx/html/index.php       #测试文件放在nginx的html下,用来测试环境

mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.$(date +%F)      #养成备份的好习惯

echo " " > /etc/nginx/conf.d/default.conf

echo 'server {

    listen       80;

    server_name  localhost;

    location / {

        root   /usr/share/nginx/html;

        index index.php index.html index.htm;

    }

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

    location ~ \.php$ {

        root           /usr/share/nginx/html;

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;

        include        fastcgi_params;

    }

} ' >> /etc/nginx/conf.d/default.conf       #配置好nginx配置与php服务响应

chown -R nginx.nginx /usr/share/nginx/html/

/etc/init.d/ngxin restart  &>/dev/null

/etc/init.d/php-fpm restart &>/dev/null

httpnum=`curl -I -m 10 -o /dev/null -s -w %{http_code}"\n" 127.0.0.1/index.php`

if [ $httpnum -eq 200 ];then

  echo -e "\033[32m "恭喜你,LNMP环境搭建成功" \033[0m"

else

  echo -e  "\033[46;31m "糟糕了,LNMP环境有点问题" \033[0m"

fi


#上面三个脚本放在对应路径下,给执行权限即可使用。