Shell编程之批量安装服务脚本实例

前端之家收集整理的这篇文章主要介绍了Shell编程之批量安装服务脚本实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

大家好,今天给大家带来的是一个实例脚本.... 效果如下:

1、安装过程效果如下:







2、第八选项,第九选项效果如下:



3、当用户输入0,或者输入字母等情况,效果如下:



整体的脚本内容如下:

#!/bin/bash
# Date: 4:42 2018-2-10
# Mail: ywyankerp@163.com
# Founder: <YanKai>
# Describe: This is a one - button installation service script
# 提示:使用此脚本时,尽量先选择第一项配置Yum源!
red_col="\e[1;31m"
reset_col="\e[0m"
LOG_DIR=/usr/local/src
. /etc/init.d/functions
MMM=`rpm -qa | grep mariadb`
#node 1.
function caidan(){ 
cat <<YanKaI
+------------------------------------------------+
|                       1. 配置Yum源             |
|        _o0o_          2. 安装Nginx	         |
|        08880          3. 安装Apache            |
|       88"."88         4. 安装MysqL             |
|       (|-_-|)         5. 安装PHP               |
|        0\=/0          6. 部署LNMP环境          |
|      __/   \__        7. 安装zabbix监控        |
|     ‘\   ///‘         8. 退出此管理程序        |
|    / Linux一键 \      9. 关闭计算机            |
|  ||    Server   ||    ======================   |  
|  \        ////          一键安装服务           |
|   |||  i i i    |||               by YanKaI    |
|   ___        ___      ======================   |
|___‘.  /--.--\ .‘___                            |
+------------------------------------------------+
YanKaI
}
caidan
#node 2.
function panduan (){
	read -p "请您输入{1-9}任意选项:" NUM
		expr $NUM + 1 &> /dev/null
	if [ "$?" -ne 0 ];then
		action "请您输入数值" /bin/false
	elif [[ "$NUM" == 0 ]];then
		action "请您输入比0大的数值" /bin/false
	fi
}
#node 3.Yum
function Yum_check(){
	echo -en "${red_col}正在配置Yum源,请稍等....\n${reset_col}"
	if [ -f /etc/yum.repos.d/CentOS-Base.repo ]
		then
		  mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back && \
		  wget http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null  && \
		  mv Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo && yum clean all &>/dev/null && yum makecache &>/dev/null
	fi
		if [ "$?" -eq 0 ];then
	           action "Yum源配置成功!!!" /bin/true
		else
		   action "Yum源配置失败,请您检查网络" /bin/false
		   exit 1
		fi
}
#node 4.Nginx
function Nginx_server(){
	echo -en "${red_col}开始安装Nginx服务,请稍后.....\n${reset_col}"
		yum install gcc gcc-c++ pcre-devel zlib-devel openssl-devel -y &>/dev/null && useradd -M -s /sbin/nologin Nginx && \
		cd $LOG_DIR && wget http://Nginx.org/download/Nginx-1.12.2.tar.gz &>/dev/null && tar zxf Nginx-1.12.2.tar.gz && \
		cd $LOG_DIR/Nginx-1.12.2 && ./configure --prefix=/usr/local/Nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --user=Nginx --group=Nginx &>/dev/null && make &>/dev/null && make install &>/dev/null
if [ -f /usr/local/Nginx/sbin/Nginx ]
   then
     ln -s /usr/local/Nginx/sbin/Nginx /usr/local/sbin 
fi
  if [ "$?" -eq 0 ]
     then
	action "Nginx安装成功" /bin/true && /usr/local/Nginx/sbin/Nginx
     else 
	action "Nginx安装失败,请检查" /bin/false
	exit 1
  fi
}
#node 2. Apache
function Apache_server(){
	echo -en "${red_col}开始安装Apache服务,请稍后.....\n${reset_col}"
	  yum install httpd -y &>/dev/null
		if [ "$?" -eq 0 ]
		   then
	             action "Apache安装成功!" /bin/true
		   else
		     action "Apache安装失败,请检查环境" /bin/false
		     exit 1
   		fi
}
#node 3. MysqL
function MysqL_server(){
	echo -en "${red_col}开始安装MysqL数据库,请稍后.....\n${reset_col}"
		if [ -n $MMM ]
		   then
		     rpm -e mariadb-libs --nodeps 2&>/dev/null
		   else
		     action "mariadb卸载失败,请重试..." /bin/false
		     exit 1
		fi
cd $LOG_DIR && {
   wget http://mirrors.sohu.com/MysqL/MysqL-5.7/MysqL-5.7.18-linux-glibc2.5-x86_64.tar.gz &>/dev/null  && tar zxf MysqL-5.7.18-linux-glibc2.5-x86_64.tar.gz && \
 mv MysqL-5.7.18-linux-glibc2.5-x86_64 /usr/local/MysqL	&& \
cd /usr/local/MysqL/ && mkdir data && mkdir log && echo "export PATH=$PATH:/usr/local/MysqL/bin" >> /etc/profile && \
	source /etc/profile && groupadd MysqL && useradd -r -g MysqL -s /bin/false MysqL
}
cat << EOF > /etc/my.cnf
[client]    
socket=/usr/local/MysqL/MysqL.sock    
[MysqLd]    
basedir=/usr/local/MysqL    
datadir=/usr/local/MysqL/data    
pid-file=/usr/local/MysqL/data/MysqLd.pid    
socket=/usr/local/MysqL/MysqL.sock    
log_error=/usr/local/MysqL/log/MysqL.err  
EOF
	if [ -f /etc/my.cnf ]
	   then
	     chmod 750 data/ && chown -R MysqL . && chgrp -R MysqL . && bin/MysqLd --initialize --user=MysqL && cp /usr/local/MysqL/support-files/MysqL.server /etc/init.d/MysqLd && service MysqLd start &> /dev/null
	   else
	     echo "MysqL安装失败!!!"
	     exit 1
	fi
	ln -s /usr/local/MysqL/bin/MysqL /usr/local/sbin
	ln -s /usr/local/MysqL/bin/MysqLadmin /usr/local/sbin
#oo=`cat /usr/local/MysqL/log/MysqL.err | grep root@localhost | awk -F ":"  '{print $4}' | cut -d " " -f 2`
oo=`cat /usr/local/MysqL/log/MysqL.err | grep password | awk '{print $11}'`
MysqLadmin -uroot -p${oo} password pwd123 &>/dev/null
	if [ "$?" -eq 0 ]
	   then
		action "MysqL数据库安装成功,密码为:pwd123" /bin/true
	   else
		action "MysqL数据库安装失败密码初始化失败!" /bin/false
	fi
} 
#node 4. PHP	
function PHP_server(){
	echo -en "${red_col}开始安装依赖包,请稍后....\n${reset_col}"
	yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel &> /dev/null
if [ "$?" -eq 0 ];then
   action "依赖包安装完成" /bin/true
   else
   action "依赖包安装失败请检查环境!!!" /bin/false
   exit 1
fi

yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel libjpeg-devel libpng libpng-devel freetype-devel libmcrypt libmcrypt-devel &>/dev/null

cd $LOG_DIR && {
	echo -en "${red_col}开始安装PHP请稍后,过程稍微有点长...\n${reset_col}"
	    wget http://cn2.PHP.net/distributions/PHP-5.6.27.tar.gz &> /dev/null && \
   	    tar zxf PHP-5.6.27.tar.gz && cd PHP-5.6.27/ && \
	./configure --prefix=/usr/local/PHP5.6 --with-config-file-path=/etc  --with-MysqL=/usr/local/MysqL --with-MysqLi=/usr/local/MysqL/bin/MysqL_config --with-MysqL-sock=/usr/local/MysqL/MysqL.sock --with-gd --with-iconv --with-libxml-dir=/usr --with-mhash --with-mcrypt-dir  --with-config-file-scan-dir=/etc/PHP.d --with-bz2 --with-zlib --with-freetype-dir --with-png-dir --with-jpeg-dir --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl &>/dev/null
}
if [ "$?" -eq 0 ]
   then
     make &>/dev/null && make install &>/dev/null
   else
     action "PHP安装失败,请检查环境..." /bin/false
     exit 1
fi
echo -en "${red_col}正在配置PHP,请稍等....\n${reset_col}"
cd /usr/local/src/PHP-5.6.27/ && {
   cp PHP.ini-production /etc/PHP.ini && cp sapi/fpm/init.d.PHP-fpm /etc/init.d/PHP-fpm && chmod +x /etc/init.d/PHP-fpm && chkconfig --add PHP-fpm && chkconfig PHP-fpm on
}
	if [ "$?" -eq 0 ];then
	   cp /usr/local/PHP5.6/etc/PHP-fpm.conf.default /usr/local/PHP5.6/etc/PHP-fpm.conf && echo -en "${red_col}复制主配置成功${reset_col}"
	   else
	     action "配置PHP环境出错,请检查..."  /bin/false
	     exit 1
	fi
if [ ! -d /usr/local/PHP5.6/etc/PHP-fpm.conf ]  
  then  
   sed -i 's#;pid = run/PHP-fpm.pid#pid = run/PHP-fpm.pid#g' /usr/local/PHP5.6/etc/PHP-fpm.conf  
   sed -i 's/listen = 127.0.0.1:9000/listen = 127.0.0.1:9000/g' /usr/local/PHP5.6/etc/PHP-fpm.conf  
   sed -i 's/pm.max_children = 5/pm.max_children = 300/g' /usr/local/PHP5.6/etc/PHP-fpm.conf  
   sed -i 's/pm.start_servers = 2/pm.start_servers = 20/g' /usr/local/PHP5.6/etc/PHP-fpm.conf  
   sed -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 20/g' /usr/local/PHP5.6/etc/PHP-fpm.conf  
   sed -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 100/g' /usr/local/PHP5.6/etc/PHP-fpm.conf  
   sed -i 's/user = nobody/user = Nginx/g' /usr/local/PHP5.6/etc/PHP-fpm.conf
   sed -i 's/group = nobody/group = Nginx/g' /usr/local/PHP5.6/etc/PHP-fpm.conf
   else  
   action "配置PHP环境出错,请检查..."  /bin/false
   exit 1
fi  
if [ $? -eq 0 ]  
   then  
     systemctl start PHP-fpm && action "PHP启动成功" /bin/true
   else  
     echo "启动PHP失败"  
     exit 1
fi   
}

#node 5. LNMP_server
function LNMP(){
	Nginx_server
        MysqL_server
	PHP_server
	echo -en "${red_col}开始安装LNMP环境,请稍后..可以喝杯茶~\n${reset_col}"
if [ "$?" -eq 0 ];then
   echo -en "${red_col}LNMP安装完成,正在配置Nginx解析PHP请稍后....\n${reset_col}"
   else
     action "LNMP安装失败,请您检查环境..." /bin/false
     exit 1
fi
  if [ -f /usr/local/Nginx/conf/Nginx.conf ]
     then
       sed -i 's/index  index.html index.htm;/index  index.PHP index.html index.htm;/g' /usr/local/Nginx/conf/Nginx.conf  
       sed -i 's/#    root           html;/    root           html;/g' /usr/local/Nginx/conf/Nginx.conf  
       sed -i 's/#    fastcgi_pass   127.0.0.1:9000;/    fastcgi_pass   127.0.0.1:9000;/g' /usr/local/Nginx/conf/Nginx.conf  
       sed -i 's/#    fastcgi_index  index.PHP;/    fastcgi_index  index.PHP;/g' /usr/local/Nginx/conf/Nginx.conf  
       sed -i 's*#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;*    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;*g' /usr/local/Nginx/conf/Nginx.conf  
       sed -i 's/#    include        fastcgi_params;/    include        fastcgi.conf;/g' /usr/local/Nginx/conf/Nginx.conf  
       sed -i '71d' /usr/local/Nginx/conf/Nginx.conf && sed -i 'N;70a}' /usr/local/Nginx/conf/Nginx.conf  
       sed -i '65d' /usr/local/Nginx/conf/Nginx.conf && sed -i 'N;64alocation ~ \\.PHP$ {' /usr/local/Nginx/conf/Nginx.conf  	
	else
	   action "Nginx解析PHP失败,请您检查环境..." /bin/false
	   exit 1
fi
	touch /usr/local/Nginx/html/2018.PHP
cat << EOF >/usr/local/Nginx/html/2018.PHP
<?PHP
PHPinfo();
?>
EOF
/usr/local/Nginx/sbin/Nginx -s reload
	if [ "$?" -eq 0 ]
	   then
	     action "LNMP环境正式搭建成功,请您访问http://IP/2018.PHP" /bin/true
	   else
	     action "LNMP搭建失败啦,哈哈哈请您检查一下环境吧..."
	     exit 1
	fi
}

function zabbix_server(){
	if [ -f /usr/local/Nginx/sbin/Nginx -a -f /etc/my.cnf -a -f /etc/PHP.ini ];then
	   echo -en "${red_col}开始安装zabbix请稍后!!!\n${reset_col}"
	else
	   action "提示:在安装zabbix之前请先安装LNMP环境!" /bin/false
	   exit 1
	fi
	groupadd zabbix  && useradd -g zabbix zabbix  
	sed -i 's/;date.timezone =/date.timezone = PRC/g' /etc/PHP.ini
	sed -i 's/expose_PHP = On/expose_PHP = Off/g' /etc/PHP.ini
	sed -i 's/short_open_tag = Off/short_open_tag = On/g' /etc/PHP.ini
	sed -i 's/post_max_size = 8M/post_max_size = 16M/g' /etc/PHP.ini
	sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /etc/PHP.ini
	sed -i 's/max_input_time = 60/max_input_time = 300/g' /etc/PHP.ini
	sed -i 's/;always_populate_raw_post_data = -1/always_populate_raw_post_data = -1/g' /etc/PHP.ini
	sed -i 's/;mbstring.func_overload = 0/mbstring.func_overload = 0/g' /etc/PHP.ini
if [ "$?" -eq 0 ];then
   echo -en "${red_col}编辑/etc/PHP.ini文件成功!\n${reset_col}"
   else
	action "编辑/etc/PHP.ini文件失败!" /bin/false
	exit 1
fi

yum install -y net-snmp net-snmp-devel curl-devel java-1.8.0-openjdk java-1.8.0-openjdk-devel  OpenIPMI-devel  libssh2-devel &>/dev/null
if [ "$?" -eq 0 ]
  then
    action "zabbix依赖包安装成功!!!"  /bin/true
  else
    action "zabbix依赖包安装失败" /bin/false
    exit 1
fi
cd $LOG_DIR && {
	wget http://www.fping.org/dist/fping-3.10.tar.gz &>/dev/null && tar zxf fping-3.10.tar.gz &>/dev/null && \
cd fping-3.10/ && ./configure &>/dev/null && make &>/dev/null && make install &>/dev/null
}
if [ "$?" -eq 0 ]
   then
     chown root:zabbix /usr/local/sbin/fping  && chmod 4710 /usr/local/sbin/fping
   else 
     action "fping安装失败" /bin/false
   exit 1
fi
cd $LOG_DIR && {
wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.2.1/zabbix-3.2.1.tar.gz  &>/dev/null && \
tar zxf zabbix-3.2.1.tar.gz && cd  zabbix-3.2.1/ && \
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --enable-java --with-MysqL=/usr/local/MysqL/bin/MysqL_config --with-net-snmp --with-libcurl --with-openipmi &>/dev/null && make &>/dev/null && make install &>/dev/null
}
if [ "$?" -eq 0 ]
   then
     action "zabbix安装成功,正在配置请稍后...." /bin/true
   else
     action "zabbix安装失败,请您检查环境...."  /bin/false
   exit 1
fi
ln -s /usr/local/zabbix/bin/* /usr/local/bin/
ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/
if [ "$?" -eq 0 ];
   then
     /usr/local/sbin/MysqL -uroot -ppwd123 -e 'create database zabbix character set utf8;'  &>/dev/null
     /usr/local/sbin/MysqL -uroot -ppwd123 -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'" &>/dev/null
   else
     action "zabbix创建软连接失败!" /bin/false
     exit 1
fi
	if [ "$?" -eq 0 ]
 	   then
	     action "zabbix数据库创建成功!" /bin/true
	   else
             action "zabbix数据库创建失败!" /bin/false
	   exit 1
	fi
cd /usr/local/src/zabbix-3.2.1/database/MysqL  && {
/usr/local/sbin/MysqL -uzabbix -pzabbix -hlocalhost zabbix < schema.sql  &>/dev/null
/usr/local/sbin/MysqL -uzabbix -pzabbix -hlocalhost zabbix < images.sql  &>/dev/null
/usr/local/sbin/MysqL -uzabbix -pzabbix -hlocalhost zabbix < data.sql   &>/dev/null
}
if [ "$?" -ne 0 ]
   then
     action "zabbix数据库导入失败!" /bin/false
    exit 1
   else
     action "zabbix数据库导入成功!" /bin/true
fi

sed -i 's$LogFile=/tmp/zabbix_server.log$LogFile=/usr/local/zabbix/logs/zabbix_server.log$g' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's$# PidFile=/tmp/zabbix_server.pid$PidFile=/usr/local/zabbix/logs/zabbix_server.pid$g' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's/# DBHost=localhost/DBHost=localhost/g' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's/# DBPassword=/DBPassword=zabbix/g' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's/# DBPort=3306/DBPort=3306/g' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's$# FpingLocation=/usr/sbin/fping$FpingLocation=/usr/sbin/fping$g' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's$# DBSocket=/tmp/MysqL.sock$DBSocket=/usr/local/MysqL/MysqL.sock$g' /usr/local/zabbix/etc/zabbix_server.conf
mkdir -p /usr/local/zabbix/logs  &&  chown -R zabbix:zabbix /usr/local/zabbix/  

cat <<EOF >/etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/MysqL/lib
/usr/local/lib
EOF
/usr/local/Nginx/sbin/Nginx -s reload && /etc/init.d/PHP-fpm restart  &>/dev/null
if [ "$?" -eq 0  ];then
   action "zabbix主配修改成功,正在进行相关的配置~" /bin/true
   else
	action "zabbix发生了严重的错误!!!" /bin/false
	exit 1
fi
cd /usr/local/src/zabbix-3.2.1/ && {
	cp misc/init.d/fedora/core/zabbix_server /etc/rc.d/init.d/zabbix_server
	cp misc/init.d/fedora/core/zabbix_agentd /etc/rc.d/init.d/zabbix_agentd
	chmod +x /etc/rc.d/init.d/zabbix_server
	chmod +x /etc/rc.d/init.d/zabbix_agentd
	chkconfig --add zabbix_server 
	chkconfig --add zabbix_agentd
	chkconfig zabbix_server on
	chkconfig zabbix_agentd on
}

	if [ "$?" -eq 0 ];then
	   action "复制zabbix启动项成功" /bin/true
	else
	   action "复制zabbix启动项失败!!" /bin/false
	   exit 1
	fi
sed -i 's$BASEDIR=/usr/local$BASEDIR=/usr/local/zabbix/$g' /etc/rc.d/init.d/zabbix_server
sed -i 's*PIDFILE=/tmp/$BINARY_NAME.pid*PIDFILE=/usr/local/zabbix/logs/$BINARY_NAME.pid*g'  /etc/rc.d/init.d/zabbix_server
sed -i 's$BASEDIR=/usr/local$BASEDIR=/usr/local/zabbix/$g' /etc/rc.d/init.d/zabbix_agentd
sed -i 's*PIDFILE=/tmp/$BINARY_NAME.pid*PIDFILE=/usr/local/zabbix/logs/$BINARY_NAME.pid*g' /etc/rc.d/init.d/zabbix_agentd

ldconfig && systemctl daemon-reload 
/etc/init.d/zabbix_agentd start  &>/dev/null
/etc/init.d/zabbix_server start  &>/dev/null
	if [ "$?" -eq 0 ];then
	   action "zabbix监控启动成功,正在进行配置页面,请稍后!!!" /bin/true
	else
	   action "zabbix监控启动失败,请检查环境!!!" /bin/false
	   exit 1
	fi
mkdir /usr/local/Nginx/html/zabbix/ 
	if [ -d /usr/local/Nginx/html/zabbix/ ];then
           cp -r /usr/local/src/zabbix-3.2.1/frontends/PHP/* /usr/local/Nginx/html/zabbix/
	else
	   action "zabbix目录创建失败" /bin/false
	   exit 1
	fi
chown -R Nginx:Nginx /usr/local/Nginx/html/zabbix

if [ $? -eq 0  ];then
   echo "zabbix部署成功,请您访问http://IP/zabbix"
   else
     echo "zabbix部署失败,请检查环境!!!"
	exit 1
fi
}

function TC (){
        echo -e "${red_col}此管理程序已经成功退出!\n${reset_col}"
	exit 0
}

function GJ(){
	echo -e "${red_col}即将关机,请稍后....\n${reset_col}"
	shutdown -h now
}

function zong(){
while :
do
 panduan
	case $NUM in
		1)
		Yum_check
		;;
		2)
		Nginx_server
		;;
		3)
		Apache_server
		;;
		4)
		MysqL_server
		;;
		5)
		PHP_server
		;;
		6)
		LNMP
		;;
		7)
		zabbix_server
		;;
		8)
		TC
		;;
		9)
		GJ
		;;
	esac
done
}
zong

运行此脚本时,请调整成能够上网的状态,写这脚本主要为了能够结合前几章博文的一些技术点,写的不好请谅解,下一章会对此脚本所有的过程进行剖析,谢谢~~


希望对您有所帮助,祝大家2018新年快乐,再见~~~

猜你在找的Bash相关文章