#!/bin/bash #执行环境
#Author:Wang pang hu #作者
#Created Time:2021/8/17 16:04 #时间
#Release:1.0 #版本
#Script Description:nginx install script from Panghu #脚本说明
#安装用户 root
#安装准备 依赖包、源码包获得、
#安装
#启动测试
#Variables
nginx_pkg="nginx-1.20.1.tar.gz"
nginx_source_doc=`echo $nginx_pkg|cut -d "." -f1-3`
nginx_install_doc="usr/local/nginx"
nginx_user="www"
nginx_group="www"
#function
check () {
#检测当前用户,要求为root 用户
#检测当前用户,要求为root 用户
if [ "$USER" != 'root' ];then
echo "need to be root so that"
exit 1
fi
#检查 wget 命令
if [ ! -x /usr/bin/wget ];then
echo "not found command /usr/bin/wget"
exit 1
fi
#简写 [ ! -x /usr/bin/wget ]&& echo "not found command /usr/bin/wget"&& exit 1
}
install_pre () {
#1、安装依赖包 1 是标准输出的意思
if ! (yum -y install gcc-* pcre-devel zlib-devel elinks 1>/dev/null);then
echo "ERROR:yum install error"
exit 1
fi
#2、下载源码包
if wget https://nginx.org/download/$nginx_pkg &>/dev/null;then
tar xf $nginx_pkg
if [ ! -d $nginx_source_doc ];then
echo "ERROR:not found $nginx_source_doc "
exit 1
fi
else
echo "ERROR:download file $nginx_source_doc "
exit 1
fi
}
install () {
#1、创建管理用户
useradd -r -s /sbin/nologin www
#2、安装nginx源码
cd $nginx_source_doc
echo "nginx configure...."
if ./configure --prefix=$nginx_install_doc --user=$nginx_user --group=$nginx_group 1>/dev/null;then
echo "nginx make...."
if make 1>/dev/null;then
echo "nginx install...."
if make install 1>/dev/null;then
echo "nginx install success"
else
echo "ERROR:nginx make install fail";exit 1
fi
else
echo "ERROR:nginx configure fail";exit 1
fi
else
echo "ERROR:nginx configure fail";exit 1
fi
}
nginx_test () {
if $nginx_install_doc/sbin/nginx;then
echo "nginx start SUCCESS!"
elinks http://localhost -dump
else
echo "nginx stop FAIL"
fi
}
#调用函数
echo "this is nginx install script"
read -p "press Y install,press C cancel:" ch
if [ $ch == 'Y' ];then
check;install_pre;install;nginx_test
elif [ $ch == 'C' ];then
exit
fi
成功之后回显:
脚本引用自:https://www.zutuanxue.com
自娱自乐,仅供参考!