【shell案例】nginx编译安装脚本

前言

本脚本没有使用函数,脚本安装的是nginx-1.16.1 , 如果你要安装其他版本,可以适当修改脚本的版本号
sed -i ‘s/原字符串/新字符串/g’ /app/nginx-install.sh
注意: 此脚本名字为nginx-install.sh, 存放在/app路径下:

脚本源码

#!/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 --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
  && 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

总结

脚本重要练习,在不断练习中成长。

猜你喜欢

转载自blog.csdn.net/xinshuzhan/article/details/108186211
今日推荐