1. 程式人生 > >一鍵式菜單自動化部署LA(N)MP腳本

一鍵式菜單自動化部署LA(N)MP腳本

linux 運維 linux運維工程師

本文主要介紹打印選擇菜單,按照選擇項自動化一鍵部署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


#上面三個腳本放在對應路徑下,給執行權限即可使用。


一鍵式菜單自動化部署LA(N)MP腳本