栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

脚本安装编译安装nginx服务(适用于所有版本)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

脚本安装编译安装nginx服务(适用于所有版本)

[root@50 ~]# cat nginx_install.sh 
#!/bin/bash

clear
echo "Preparations before installation..."
##variabled
NGINX_VERSION="nginx-1.16.0"
NGINX_INSTALL_DOC="/usr/local/nginx"
NGINX_USER="nginx"
NGINX_GROUP="nginx"
NGINX_CONFIGURE="--prefix=${NGINX_INSTALL_DOC} --user=${NGINX_USER} --group=${NGINX_GROUP} --with-http_ssl_module --with-http_stub_status_module"

##function
nginx_check(){
# 1、监测当前用户 要求为root 
if [  "$USER" != 'root' ];then
   echo "need to be root so that"
   exit 5
fi

# 2、检查wget命令
WGET_CHECK=$(rpm -q wget)
if [ $? -ne 0 ];then
yum -y install wget &> /dev/null
fi
}

nginx_install_pre(){
# 1、安装依赖
if ! (yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel openssl openssl-devel elinks 1>/dev/null);then
	echo "ERROR:YUM install error"
	exit 5
fi
# 2、下载nginx源码包
if (wget http://nginx.org/download/${NGINX_VERSION}.tar.gz &>/dev/null);then
	tar zxf ${NGINX_VERSION}.tar.gz
	if [ ! -d ${NGINX_VERSION} ];then
		echo "ERROR:not found ${NGINX_VERSION}";exit 5 
	fi
else
	echo "ERROR:wget download file ${NGINX_VERSION}.tar.gz fail"
fi
}

nginx_install_make(){
(groupadd ${NGINX_GROUP} ;useradd -s /sbin/nologin -r -M -g ${NGINX_GROUP} ${NGINX_USER}) &>/dev/null
cd ${NGINX_VERSION}
echo "nginx configure..."
if ./configure ${NGINX_CONFIGURE} 1>/dev/null;then
	echo "nginx make ..."
    if make 1>/dev/null;then
		echo "nginx make install ..."
		if make install 1>/dev/null;then
			echo "nginx install success"
        else
			echo "ERROR: nginx install tail!";exit 5
		fi
	else
		echo "ERROR: nginx make tail!";exit 5
	fi
else
	echo"ERROR: nginx configure tail!";exit 5
fi
}

# 配置nginx开机自启,使用systemctl 管理nginx服务
nginx_enable(){
cat > /usr/lib/systemd/system/nginx.service </dev/null
}

nginx_start(){
TEMP_NGINX=$(mktemp nginx.XXX)
if systemctl start nginx.service;then
    echo "nginx start SUCCESS!"
    clear
    elinks http://localhost -dump >${TEMP_NGINX}
    head -n +11 ${TEMP_NGINX}
    rm -f ${TEMP_NGINX}
    echo -e "e[1;36m Manager Nginx:e[0m e[0;32m systemctl start|stop|status|restart| nginx.service e[0m"
else
    echo "nginx stop FAIL"
fi

}

nginx_check
nginx_install_pre
nginx_install_make
nginx_enable
nginx_start

执行脚本

bash nginx_install.sh

脚本中定义了变量 需要安装其他nginx环境 只需要改变量就可以了!

  6 NGINX_VERSION="nginx-1.16.0"           #定义安装nginx版本
  7 NGINX_INSTALL_DOC="/usr/local/nginx"   #定义安装目录
  8 NGINX_USER="nginx"                     #定义nginx用户
  9 NGINX_GROUP="nginx"                    #定义nginx组
 10 NGINX_CONFIGURE="...省略"               #定义nginx编译需要模块及参数!
 # 注意:在定义 NGINX_CONFIGURE变量模块时  需要安装此模块需要的依赖(如:with-http_ssl_module模块就需要依赖openssl 否则就会报错!)

脚本执行成功后效果图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/308497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号