bash编写一个应用小脚本,测试机自动放开端口配置nginx

哈喽
大概是这样
学了bash苦无用武之地

最近有个测试机
开发经常联系我来新开端口
新增个测试站点,这样

LNMP环境

10多个端口了,不够用MMP
苦于重复无聊的工作
出此脚本

各位随便看看,没什么技术
但是对我的工作非常管用

注释是方便新手看,后来加的,自己看懂了备份删除下最好,大神别喷

#!/bin/bash

#建函数check判断是否出错,出错就强制退出
function check {
if [ $? != 0 ]
then
echo "error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
exit 1
fi
}

#判断centos7还是6
var=`rpm -q centos-release|cut -d- -f3`

#给备份加个时间后缀
now=`date '+%Y%m%d-%H%M%S'`

#读取输入端口号
#差点忘了,整个脚本运行后只需要开发输入端口和网站家目录这2项就可以了
echo "you want port:"
read port

#读取家目录
echo "you want rootdir:"
read root

echo "==============back nginx================"
cp  /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.conf.$new
check
echo ""

#利用sed直接写入到conf
echo "==============vi nginx================"
sed -i "/large_client_header_buffers/a\#\n    server {\n        listen "$port";\n        server_name localhost;\n\n        access_log logs/"$port".log main;\n\n        location / {\n            root "$root";\n            index index.php index.html;\n        }\n\n        location ~ \x5c.php\$ {\n            root     "$root";\n            fastcgi_pass   127.0.0.1:9000;\n            fastcgi_index  index.php;\n            fastcgi_param  SCRIPT_FILENAME  "$root"\$fastcgi_script_name;\n            include        fastcgi_params;\n        }\n    }\n\n"  /usr/local/nginx/conf/nginx.conf
check
echo ""

echo "==============nginx -t================"
/usr/local/nginx/sbin/nginx -t
check
echo ""

echo "==============restart nginx================"
/usr/local/nginx/sbin/nginx -s reload
check
echo ""

echo "==============start iptables================"
#判断端口是不是已经开了
if grep $port /etc/sysconfig/iptables
then
        echo "$port yi jing you le"
        exit 1
fi

echo "==============back config================"
cp /etc/sysconfig/iptables /etc/sysconfig/iptables.$now
check
echo ""

#sed直接在80端口下插入,非常粗暴,而且一般web服务器都开80吧,应该都行
echo "==============vi config================"
sed -i "/--dport 80 -j ACCEPT/a\-A INPUT -p tcp -m state --state NEW -m tcp --dport $port -j ACCEPT"  /etc/sysconfig/iptables
check
echo ""

echo "==============restart================"
#判断centos版本,重启
if [ $var == 7 ]
then
        systemctl restart iptables.service
        check
        echo ""
        systemctl status iptables.service
        check
        echo ""
elif [ $var == 6 ]
then
        service iptables restart
        check
        echo ""
        service iptables status
        check
        echo ""
fi

echo "==============tian jia wan cheng  http://IP:port================"

安排! TAB

猜你喜欢

转载自blog.51cto.com/kevinzhang91/2140265