一个自动安装lnmp环境的shell脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个自动安装lnmp环境的shell脚本相关的知识,希望对你有一定的参考价值。
#/bin/bash
#定义nginx源码包版本,脚本会根据版本名称和后缀检查文件是否在当前目录如果你需要安装不同版本的nginx可以修改这两个变量
nginx_version=nginx-1.8.0
format1=tar.gz
#定义php-fpm安装包的文件名,如果你需要安装其他版本可以更改这个变量
php_fpm_package_name=php-fpm-5.4.16-36.el7_1.x86_64.rpm
#判断系统语言是不是中文是返回0不是返回1
language(){
echo $LANG |grep -q zh
if [ $? -eq 0 ];then
return 0
else
return 1
fi
}
#yum仓库的错误输出函数
error_yum(){
#根据系统默认语言输出错误提示
language
if [ $? -eq 0 ];then
clear
echo
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "错误:本机YUM不可用,请正确配置YUM后重试."
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo
exit
else
clear
echo
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "ERROR:Yum is disable,please modify yum repo file then try again."
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo
exit
fi
}
#yum仓库检查函数
yumcheck(){
#清空yum缓存
yum clean all &>/dev/null
#得到yum仓库中可用rpm包个数
repolist=$(yum repolist 2>/dev/null |awk ‘/repolist:/{print $2}‘|sed ‘s/,//‘)
if [ $repolist -le 0 ];then
#如果可用软件包数是0则调用报错函数error_yum提示yum错误
error_yum
fi
}
#解决nginx依赖关系
solve_depend(){
language
if [ $? -eq 0 ];then
echo -en "\033[1;34m正在安装依赖包,请稍后...\033[0m"
else
echo -e "\033[1;34mInstalling dependent software,please wait a moment...\033[0m"
fi
rpmlist="gcc pcre-devel openssl-devel zlib-devel make"
#循环安装nginx所有依赖包
for i in $rpmlist
do
#查看该包是否安装如果每有安装则安装否则跳过
rpm -q $i &>/dev/null || yum -y install $i
done
}
#当前目录找不到源码包提示错误函数
error_nofile(){
language
if [ $? -eq 0 ];then
clear
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo -e "\033[1;34m错误:未找到[ ${1} ]软件包,请下载软件包至当前目录.\033[0m"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
exit
else
clear
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo -e "\033[1;34mERROR:Not found [ ${1} ] package in current directory, please download it.\033[0m"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
exit
fi
}
#安装nginx函数
install_nginx(){
#调用yum仓库检查
yumcheck
#安装nginx依赖包
solve_depend
grep -q nginx /etc/passwd
#如果系统中不存在nginx这个用户则添加一个nginx用户
[ $? -ne 0 ] && useradd -s /sbin/nologin nginx
if [ -f ${nginx_version}.${format1} ];then
tar -xf ${nginx_version}.${format1}
cd $nginx_version
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make
make install
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
cd ..
else
#如果找不到nginx安装文件则报错
error_nofile Nginx
fi
}
#yum安装错误提示函数
error_yum(){
language
if [ $? -eq 0 ];then
clear
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo -e "\033[1;34m错误: ${1}安装失败.\033[0m"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
exit
else
clear
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo -e "\033[1;34merror: ${1}install error.\033[0m"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
exit
fi
}
#安装PHP Mariadb 相关组件函数
yum_install(){
yum_list="php php-mysql mariadb mariadb-server mariadb-devel"
for i in $yum_list
do
#查看该包是否安装如果每有安装则安装否则跳过
rpm -q $i &>/dev/null || yum -y install $i
[ $? -nq 0 ] && error_yum $i
done
}
#安装php后台程序函数
install_php_fpm(){
if [ -f ${php_fpm_package_name} ]
then
rpm -q $php_fpm_package_name &>/dev/null || rpm -hiv $php_fpm_package_name
else
error_yum ${php_fpm_package_name}
fi
}
#启动提示
start_message(){
language
if [ $? -eq 0 ];then
if [ $2 -eq 0 ];then
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo -e "\033[1;34m启动: ${1}启动成功.\033[0m"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
else
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo -e "\033[1;34m启动: ${1}启动失败.\033[0m"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
fi
else
if [ $2 -eq 0 ];then
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo -e "\033[1;34mstart: ${1}start is successful.\033[0m"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
else
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo -e "\033[1;34mstart: ${1}start is error.\033[0m"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
fi
fi
}
#启动lnmp环境函数
start_lnmp(){
systemctl start php-fpm.service &>/dev/null
netstat -tupan |grep php-fpm
[ $? -eq 0 ] && start_message php-fpm 0 || start_message php-fpm 1
/usr/local/nginx/sbin/nginx &>/dev/null
netstat -tupan |grep nginx &>/dev/null
[ $? -eq 0 ] && echo start_message nginx 0 || start_message nginx 1
systemctl start mariadb &>/dev/null
[ $? -eq 0 ] && start_message mariadb 0 || start_message mariadb 1
#启动环境结束后暂停5秒;退出程序
sleep 5
exit
}
#脚本菜单函数
menu(){
clear
language
if [ $? -eq 0 ];then
echo " ##############----Menu----##############"
echo "# 1. 安装lump环境"
echo "# 2. 启动lump环境"
echo "# 9. 退出程序"
echo " ########################################"
else
echo " ##############----Menu----##############"
echo "# 1. Install lnmp"
echo "# 2. Start lnmp"
echo "# 9. Exit Program"
echo " ########################################"
fi
}
#功能选择函数
choice(){
language
if [ $? -eq 0 ];then
read -p "请选择一个菜单[1 2 9]:" select
else
read -p "Please choice a menu[1 2 9]:" select
fi
}
#主程序开始
while :
do
menu
choice
case $select in
1)
install_nginx
yum_install
install_php_fpm;;
2)
start_lnmp;;
9)
exit;;
*)
echo "choice error"
esac
done
本脚本基于CentOS7.x平台编写;如有错误请留言;谢谢!
以上是关于一个自动安装lnmp环境的shell脚本的主要内容,如果未能解决你的问题,请参考以下文章
环境搭建-CentOS 7上通过Shell脚本自动化安装Harbor