shell基础之多功能nginx(安装、重启、停止等)
1 #!/bin/bash 2 #要求:检查本机是否已编译安装nginx,检查本机是否存在nginx源码包,编译安装nginx,实现开启、停止、查看状态等功能 3 #检查是否已编译安装nginx 4 while : 5 do 6 read -p "本机是否安装nginx?yes查看nginx状态,no跳转至安装nginx(yes/no):" wh_do 7 if [ "$wh_do" == "no" ];then 8 while : 9 do 10 read -p "是否确定编译安装nginx(yes/no)no表示退出脚本:" YON 11 if [ "$YON" == "yes" ];then 12 check_install=$(ls /usr/local |grep nginx) 13 if [ -n "$check_install" ];then 14 echo "已经编译安装nginx" 15 break 16 else 17 #配置本地yum源 18 mount_point=$(ls / |grep local_dvd) 19 if [ -z "$mount_point" ];then 20 mkdir /local_dvd 21 mount /dev/cdrom /local_dvd 22 echo "/dev/cdrom /local_dvd iso9660 defaults 0 0 " >> /etc/fstab 23 rm -rf /etc/yum.repos.d/*.repo 24 echo "[local] 25 name=local 26 baseurl=file:///local_dvd 27 enabled=1 28 gpgcheck=0 " >> /etc/yum.repos.d/local.repo 29 fi 30 check_have=$(find / -name nginx-*.tar.gz |head -n 1) 31 if [ -z "$check_have" ];then 32 yum -y install wget 33 wget http://nginx.org/download/nginx-1.16.0.tar.gz 34 fi 35 check_have=$(find / -name nginx-*.tar.gz |head -n 1) 36 #检查防火墙环境 37 se_status=$(getenforce) 38 if [ "$se_status"=="Enforcing" ];then 39 setenforce 0 40 sed -i "s/=Enforcing/=Disabled/g" /etc/selinux/config 41 fi 42 systemctl start firewalld 43 systemctl enable firewalld 44 #安装依赖包 45 yum -y install gcc-* zlib-* pcre-* 46 #解压nginx包 47 tar xzf $check_have -C /root/ 48 #编译安装nginx 49 cd /root/nginx-* 50 pwd 51 ./configure && make && make install 52 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 53 fi 54 if `firewall-cmd --list-port |grep 80/tcp` 55 then 56 break 57 else 58 firewall-cmd --add-port=80/tcp 59 firewall-cmd --add-port=80/tcp --permanent 60 fi 61 break 62 elif [ "$YON" == "no" ];then 63 echo "退出脚本" 64 exit 65 else 66 echo "请输入正确的内容" 67 continue 68 fi 69 done 70 #nginx启动、停止、查看状态 71 elif [ "$wh_do" == "yes" ];then 72 check_install=$(ls /usr/local |grep nginx) 73 if [ -z "$check_install" ];then 74 echo "您未编译安装nginx,请先安装" 75 continue 76 fi 77 while : 78 do 79 read -p "请输入你要执行操作的序号(0-退出;1-开启;2-停止;3-重启;4-查看状态)" NUM 80 case $NUM in 81 0) 82 echo "退出脚本" 83 exit 84 ;; 85 1) 86 PID=$(ps aux |grep nginx |grep master |awk '{print $2}') 87 if [ -z "$PID" ];then 88 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 89 echo "启动nginx" 90 else 91 echo "nginx已经启动" 92 fi 93 ;; 94 2) 95 PID=$(ps aux |grep nginx |grep master |awk '{print $2}') 96 if [ -z "$PID" ];then 97 echo "nginx已经停止" 98 else 99 echo "停止nginx" 100 kill $PID 101 fi 102 ;; 103 3) 104 PID=$(ps aux |grep nginx |grep master |awk '{print $2}') 105 if [ -n "$PID" ];then 106 kill $PID 107 fi 108 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 109 echo "重启nginx" 110 ;; 111 4) 112 PID=`ps aux |grep nginx |grep master |awk '{print $2}'` 113 if [ -z "$PID" ];then 114 echo "nginx已停止" 115 else 116 echo "nginx已开启" 117 fi 118 ;; 119 *) 120 echo "请输入正确的序号" 121 ;; 122 esac 123 done 124 else 125 echo "请输入正确的选项" 126 continue 127 fi 128 done