shell脚本一键部署nginx
一键部署nginx
#!/bin/bash systemctl stop firewalld && setenforce 0 #首先安装nginx的依赖环境 yum -y install gcc pcre-devel zlib-devel net-tools wget #解压nginx的安装包 if [ ! -d "/root/nginx-1.16.1" ]; then wget http://nginx.org/download/nginx-1.16.1.tar.gz tar -zxf nginx-1.16.1.tar.gz echo "压缩包已解压" else echo "此文件已存在" continue fi #进去文件开始检查环境 编译安装 if [ ! -d /usr/local/nginx ]; then cd /root/nginx-1.16.1 && ./configure && make && make install else continue fi #判断是否nginx的端口被占用 pid_file="/usr/local/nginx/logs/nginx.pid" if [ ! -e ${pid_file} ]; then echo "被占用的pid是:`cat ${pid_file}`" kill `cat ${pid_file}` echo "服务被占用,已删掉" else echo "服务没有被占用" continue fi nginx=/usr/local/nginx/sbin/nginx #开始启动nginx read -p "请输入你接下来要做的操作:" action check(){ netstat -anptu | grep nginx if [ $? -eq 0 ];then continue fi } case $action in start) netstat -anptu | grep nginx if [ $? -eq 0 ]; then continue else $nginx fi ;; stop) netstat -anptu | grep nginx if [ $? -eq 0 ]; then echo "nginx-server is already running nginx-server begin stop" $nginx -s stop else echo "nginx-server is not start" fi ;; reload) netstat -anptu | grep nginx if [ $? -eq 0 ]; then echo "nginx-server is already running nginx-server begin reload" $nginx -s reload else echo "nginx-server is not running now begin start nginx-server" $nginx $nginx -s reload fi ;; statue) check ;; *) echo "please enter{start|stop|reload|statue}" ;; esac ip=`/sbin/ifconfig -a|awk '{print $2}'|sed -n '2p'` code=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://${ip}` if [ $code -eq 200 ]; then echo "nginx-server is ok" else echo "nginx-server is not ok" fi