1、nginx反向代理安装配置
1 #!/usr/bin/bash 2 if [ -e /etc/nginx/nginx.conf ] 3 then 4 echo 'Already installed' 5 exit 6 else 7 /usr/bin/yum -y install epel* 8 /usr/bin/yum -y install nginx* 9 fi 10 11 if [ -f /etc/nginx/nginx.conf ];then 12 msg="upstream zhanggen { server 1.1.1.1 weight=8;server 2.2.2.2 weight=6;server 3.3.3.3;}" 13 /usr/bin/sed -ri "/^http/a $msg" /etc/nginx/nginx.conf 14 /usr/bin/sed -ri "/^ *location \/ \{$/a proxy_pass http://zhanggen\;" /etc/nginx/nginx.conf 15 fi 16 17 /usr/sbin/nginx -t 18 return=`echo $?` 19 if [ $return -ne 0 ] 20 then 21 echo "config error" 22 else 23 echo "config success " 24 fi 26 /usr/bin/systemctl start nginx 27 /usr/bin/ps -ef | /usr/bin/grep nginx |/usr/bin/grep -v 'grep' 28 29 value=`echo $?` 30 if [ $value -eq 0 ] 31 then 32 echo 'Start nginx successful' 33 else 34 echo 'Start nginx faild please check again' 35 fi
2、安装配置后端nginx
1 #!/usr/bin/bash 2 if [ -e /etc/nginx/nginx.conf ] 3 then 4 echo 'Already installed' 5 exit 6 else 7 yum -y install epel*; 8 yum -y install nginx*; 9 fi 10 11 if [ -f /etc/nginx/nginx.conf ] 12 then 13 nginx -t 14 else 15 echo "Installation failed" 16 fi 17 18 return=`echo $?` 19 if [ $return -ne 0 ] 20 then 21 echo "config error" 22 else 23 echo "config success " 24 fi 25 26 systemctl start nginx; 27 28 ngxStatus=`ps aux | grep -v grep |grep -c nginx` 29 30 if [ $ngxStatus -lt 2 ] 31 then 32 echo "Start nginx successful" 33 fi
3、安装NFS服务端
1 #!/usr/bin/sh 2 while true 3 # -*- coding: UTF-8 -*- 4 do 5 test -s /etc/exports 6 return=`echo $?` 7 if [ $return -ne 0 ] 8 then 9 yum install rpcbind nfs-utils –y 10 echo "/share * (rw,sync,fsid=0)" > /etc/exports 11 mkdir -p /share 12 chmod -R o+w /share 13 systemctl enable nfs-server.service && systemctl enable rpcbind.service 14 systemctl start rpcbind.service && systemctl start nfs-server.service 15 iptables -F && iptables -X 16 share=`showmount -e` 17 access_IP=`awk '{print $2}' /etc/exports` 18 echo "NFS安装成功,您共享给大家的目录为: $share $access_IP可访问到它" 19 exit 20 21 else 22 echo "已经安装了 NFS 服务" 23 exit 24 fi 25 done
4、安装NFS客户端
1 #!/usr/bin/sh 2 while : 3 do 4 iptables -F && iptables -F 5 yum install rpcbind nfs-utils -y 6 service rpcbind start 7 service nfs start 8 showmount -e 192.168.182.141 9 re=`echo $?` 10 if [ $re -eq 0 ] 11 then 12 mount -t nfs 192.168.182.141:/share /var/www/ 13 else 14 mount fiald ! 15 fi 16 exit 17 done
5、检查nginx和NFS可用性,并邮件报警。
1 #!/usr/bin/sh 2 ps -ef |grep nfsd |grep -v 'grep' 3 return6=`echo $?` 4 if [ $return6 -ne 0 ] 5 then 6 msg="TIME:$(/usr/bin/date +%F_%T) 7 HOSTNAME:$(/usr/bin/hostname) 8 IPADDR:$(/usr/sbin/ifconfig |/usr/bin/awk 'NR==2{print $2}') 9 NFS is faild" 10 echo "$msg" 11 /usr/bin/mail $msg 12 systemctl start nfs-server.service 13 fi 14 ps -ef |grep nginx |grep -v 'grep' 15 return8=`echo $?` 16 if [ $return8 -ne 0 ] 17 then 18 msg="TIME:$(/usr/bin/date +%F_%T) 19 HOSTNAME:$(/usr/bin/hostname) 20 IPADDR:$(/usr/sbin/ifconfig |/usr/bin/awk 'NR==2{print $2}') 21 nginx is faid" 22 echo "$msg" 23 /usr/bin/mail $msg 24 systemctl start nginx 25 fi
6、Python发邮件模块 mv /usr/bin/mail
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import smtplib import email.mime.multipart import email.mime.text server = 'smtp.163.com' port = '25' def sendmail(server,port,user,pwd,msg): smtp = smtplib.SMTP() smtp.connect(server,port) smtp.login(user, pwd) smtp.sendmail(msg['from'], msg['to'], msg.as_string()) smtp.quit() print('邮件发送成功email has send out !') if __name__ == '__main__': msg = email.mime.multipart.MIMEMultipart() msg['Subject'] = '你是风儿我是沙,缠缠绵绵回我家' msg['From'] = 'python4_mail@163.com' msg['To'] = 'python4_recvmail@163.com' user = 'python4_mail' pwd = 'sbalex3714' content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:])) #格式处理,专门针对我们的邮件格式 txt = email.mime.text.MIMEText(content, _charset='utf-8') msg.attach(txt) sendmail(server,port,user,pwd,msg)
7、设置crotable 每5分钟检查一次
crontab -e
*/5 * * * * /status_check.sh