shell指令碼企業實戰系列-nginx原始碼包安裝

一個杯子_one發表於2020-12-10

1、以下為shell指令碼程式碼

#!/bin/bash
#檢測是否可以上外網
#nginx需要的常用軟體包
nginx_setup=1.16.1
yum_pack="gcc  curl vim iotop  make rsync lrzsz tree ntpdate  pcre pcre-devel zlib zlib-devel openssl openssl-devel"
OS=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
check_network(){
	echo "檢查網路是否可以訪問外網,不能訪問外網將停止指令碼執行"
	check_net=`curl -I -m 10 -o /dev/null -s -w %{http_code} www.baidu.com`
	if [ ${check_net} -ne 200 ];then
            echo "請檢查網路是否正常,能否訪問外網"
	    exit 1
	fi
}

#安裝wegt用於替換阿里源
install_wget(){
	yum install -y wget
}

#替換阿里源
change_aliyuan(){
	echo "--------------------------------修改映象源為阿里映象源------------------------------------"
	if [ "$OS" == "7" ];then
		echo "--------------------------------修改映象源為阿里映象源------------------------------------"
		cp  -r /etc/yum.repos.d  /etc/yum.repos.d.bak
		rm -rf  /etc/yum.repos.d/* 
		wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
		wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
		sed -i '/aliyuncs.com/d' /etc/yum.repos.d/*.repo
		#清理快取
		yum clean all && yum makecache
	
	elif [ "$OS" == "6" ];then
		echo "--------------------------------修改映象源為阿里映象源------------------------------------"
		cp  -r /etc/yum.repos.d  /etc/yum.repos.d.bak
		rm -rf  /etc/yum.repos.d/* 
		wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
		wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
		sed -i '/aliyuncs.com/d' /etc/yum.repos.d/*.repo
		#清理快取
		yum clean all && yum makecache
	fi
}

#下載常用的軟體包
yum_install_packges(){
	echo "yum安裝nginx需要的軟體"
	yum  install -y -q ${yum_pack} > /dev/null 2>&1
}

#下載nginx1.16.1版本的軟體包
download_nginx(){
	if [ -f nginx-${nginx_setup}.tar.gz ];then
		echo "已經存在相應的版本包"
		cp nginx-${nginx_setup}.tar.gz /usr/local/src/
	else
		wget -P /usr/local/src http://nginx.org/download/nginx-${nginx_setup}.tar.gz
 		wait
	fi
}

install_nginx(){
	cd /usr/local/src/ && tar xvfz nginx-${nginx_setup}.tar.gz
	cd nginx-${nginx_setup}
	./configure  --prefix=/usr/local/nginx \
        --sbin-path=/usr/local/nginx/sbin/nginx \
        --with-http_stub_status_module \
        --with-http_ssl_module \
        --with-http_realip_module
        wait
	make && make install  > /dev/null 2>&1
}

backup_nginxconf(){
	mv /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.conf.bak
        cp /home/nginx.conf.template /usr/local/nginx/conf/nginx.conf
}

#啟動nginx
start_nginx(){
	/usr/local/nginx/sbin/nginx 
	WebPort=`netstat -nlt |grep 8000|awk '{print $4}'| cut -d : -f 2`
	if [ "$WebPort" = "8000" ];then 
		echo "Nginx service has started"
	else
		echo "Please start nginx manually, starting up as: cd /usr/local/nginx/sbin && ./nginx "
	fi
}

main(){
	check_network;
	install_wget;
	change_aliyuan;
	yum_install_packges;
    download_nginx;
	install_nginx;
        backup_nginxconf;
	start_nginx;
}
main
	

相關文章