简单的Nginx自动化安装啊脚本

#!/bin/bash

#description:Nginx installation script automatically
#user:baomanji
#date:2017-03-25
#version:0.1

###############
ng_i=$(rpm -qa nginx | wc -l)
nfs_i=$(rpm -qa nfs-utils | wc -l)
ng_up='/etc/nginx/upstream'
ng_vh='/etc/nginx/vhost'
ng_d='/etc/nginx'
system_version=$(awk -F'[ .]+' '{print$4}' /etc/redhat-release)

if [ $system_version -eq 7 ];then
Ip=$(ifconfig | awk 'NR2{print$2}')
else
Ip=$(ifconfig | awk -F'[ :]+' 'NR
2{print$4}')

fi
Net=$(echo $Ip | awk -F. '{print$1"."$2"."$3}')

######################################
######################################
function nfs_install() {
if [ $nfs_i -eq 0 ];then
yum -y install rpcbind nfs-utils
fi
test -d /share || mkdir /share
echo "/share $Net.0/24(rw,sync,fsid=0)" >> /etc/exports
chmod o+w /share
systemctl restart rpcbind.service
systemctl restart nfs-server.service
}

function nginx_install() {
if [ $ng_i -eq 0 ];then
if [ -f /etc/yum.repos.d/epel.repo ];then
yum -y install nginx rpcbind nfs-utils
sed -i 's#80#9000#g' /etc/nginx/nginx.conf
else
yum -y install epel-release
yum -y install nginx rpcbind nfs-utils
sed -i 's#80#9000#g' /etc/nginx/nginx.conf
fi
else
echo "nginx already install"
fi

ls -d /www/8080 || mkdir -p /www/8080
ls -d $ng_up || mkdir $ng_up
ls -d $ng_vh || mkdir $ng_vh
systemctl restart rpcbind.service
mount -t nfs nfs-server:/share /www/8080

}

function load () {
(
cat << EOF
server {
listen 80;
server_name $Ip;
location / {
proxy_pass http://pythonweb;
}
}
EOF
) > /etc/nginx/vhost/80.conf

(
cat << EOF
upstream pythonweb {
server python-web1:8080;
server python-web2:8080;
server python-web3:8080;
}
EOF
)> /etc/nginx/upstream/pythonweb.conf

}

function vhost() {

(
cat << EOF
server {
listen 8080;
server_name $Ip;
access_log /var/log/nginx/8080-log;
root /www/8080;
index index.html;
}
EOF
) > /etc/nginx/vhost/8080.conf

}

function boot_nginx() {
grep "/etc/nginx/upstream/" $ng_d/nginx.conf || sed -i '/default_type/ a\ include /etc/nginx/upstream/;' /etc/nginx/nginx.conf
grep "/etc/nginx/vhost/" $ng_d/nginx.conf || sed -i '/default_type/ a\ include /etc/nginx/vhost/;' /etc/nginx/nginx.conf
/usr/sbin/nginx -t && systemctl restart nginx.service && netstat -anpt | grep 80*
}

function options(){
while :
do
cat <<EOF
请选择========
install--安装Nginx
nfs------安装nfs
vhost----配置虚拟主机
load-----配置负载均衡
boot-----启动Nginx
exit-----退出

EOF

read -p "请输入选择:" input
case $input in
nfs)
nfs_install
;;
install)
nginx_install
;;
vhost)
vhost
;;
load)
load
;;
boot)
boot_nginx
;;
*)
exit
esac
done
}

options

posted @ 2017-03-27 08:21  f_manito  阅读(304)  评论(0编辑  收藏  举报