nginx编译安装脚本 | 端口检测
Posted gwj_Cloud
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx编译安装脚本 | 端口检测相关的知识,希望对你有一定的参考价值。
nginx_install.sh
#!/bin/bash
#Author:
#Created time:2022/3/2
#Description:install nginx
#variables
nginx_pkg="nginx-1.16.1.tar.gz"
nginx_rel="nginx-1.16.1"
nginx_install_doc=/usr/local/nginx
nginx_start_doc=$nginx_install_doc/sbin/nginx
nginx_user="www"
nginx_group="www"
check ()
#检测用户
if [ "$USER" != "root" ];then
echo "need to be root so that"
exit 1
fi
#检测wget
if [ ! -x /usr/bin/wget ] ;then
yum install -y wget >/dev/null
fi
install_pkg ()
#安装依赖
if !(yum install -y gcc gcc-c++ pcre-devel zlib-delvel openssl openssl-devel 1>/dev/null);then
echo "ERROR:yum install error"
exit 1
fi
#下载源码包
if (wget -O /usr/local/src/$nginx_pkg https://nginx.org/download/$nginx_pkg 1>/dev/null);then
mkdir $nginx_install_doc
tar -xf /usr/local/src/$nginx_pkg -C /usr/local/src/
if [ ! -d /usr/local/src/$nginx_rel ];then
echo "ERROR:not found $nginx_rel"
exit 1
fi
else
echo "ERROR:wget file fail"
exit 1
fi
nginx_install ()
#创建用户
useradd -r -s /sbin/nologin www
#编译安装
cd /usr/local/src/$nginx_rel
if ./configure --prefix=$nginx_install_doc --user=$nginx_user --group=$nginx_group 1>/dev/null;then
if make 1>/dev/null;then
if make install 1>/dev/null;then
echo "make install successful"
else
echo "make install fail"
exit 1
fi
else
echo "make fail"
exit 1
fi
else
echo "./configure fail"
exit 1
fi
#启动,测试
nginx_start ()
if $nginx_start_doc;then
echo "nginx start SUCCESS!"
curl http://localhost:80
else
echo "nginx start FAIL"
fi
echo "this is nginx install script"
read -p "press "Y" install;press "N" cancel: " ch
if [ $ch == Y ];then
check;install_pkg;nginx_install;nginx_start
if [ $ch == N ];then
exit 1
fi
fi
nginx_port.sh
#!/bin/bash
#检测nginx脚本,一旦nginx端口不存在,重新启动nginx
nginx_status()
#启动目录
nginx_start=/usr/local/nginx/sbin/nginx
#临时文件
temp_file=`mktemp nginx_status.XXX`
#判断方法:检测服务端口是否有相应,使用telnet协议
(telnet $1 $2 <<EOF
quit
EOF
) &> $temp_file
if (grep -E "\\^]" $temp_file)&>/dev/null;then
echo "nginx is running ..."
else
$nginx_start >/dev/null
fi
rm -f $temp_file
nginx_status $1 $2
以上是关于nginx编译安装脚本 | 端口检测的主要内容,如果未能解决你的问题,请参考以下文章
使用本脚本可以自动批量完成中间节点环境的部署工作,包括:Nginx编译安装添加程序管理脚本设置开机启动反向代理配置证书分发添加iptables规则等