陈佳浩
陈佳浩
发布于 2024-07-05 / 19 阅读
0
0

Nginx安装

#!/bin/bash
#
#********************************************************************
#Author:            chenjiahao
#QQ:                1938191576
#Date:              2022-08-18
#FileName:          install_nginx.sh
#URL:               https://www.placjh.com
#Description:       The deploy script
#Copyright (C):     2022 All rights reserved
#********************************************************************

NGINX=nginx-1.18.0
DIRE=/apps/nginx
color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "  
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}

. /etc/os-release

dependence(){
	echo "安装依赖包"
	if [ $ID = "centos" -o $ID = "rocky" ];then
		yum -y install tar wget make gcc pcre-devel openssl-devel zlib-devel &> /dev/null 
	elif [ $ID = "ubuntu" ];then
		apt update
		apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g zlib1g-dev &>/dev/null
	else
		color 不支持的OS
	fi
}

user(){
	echo 创建启动账号,创建安装目录
	useradd -s /sbin/nologin nginx &>/dev/null
	mkdir /apps/nginx -p
}

download(){
	echo "检查源码包"
	if [ ! -f $NGINX.tar.gz ];then
		echo "下载资源"
		wget http://nginx.org/download/$NGINX.tar.gz &> /dev/null
		if [ ! $? -eq 0 ];then
			color 下载失败 1
			rm -rf $NGINX.tar.gz
			exit
		fi
	fi
	echo "解压资源"
	tar xf $NGINX.tar.gz &> /dev/null
	color 完毕 0
}

install(){
	echo "编译安装"
	cd $NGINX/
	./configure --prefix=$DIRE --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module &> /dev/null
	make &> /dev/null && make install &> /dev/null
	if [ ! $? -eq 0 ];then
		color 安装失败,请排查编译安装步骤 1
		exit
	fi
}

prepare(){
	echo "准备"
	ln -s /apps/nginx/sbin/nginx /usr/local/bin/ &>/dev/null
	chown -R nginx.nginx $DIRE
	echo "创建service文件"
	cat > /lib/systemd/system/nginx.service <<-EOF
	[Unit]
	Description=The nginx HTTP and reverse proxy server
	After=network.target remote-fs.tatget nss-lookup.target

	[Service]
	Type=forking
	PPIDFile=$DIRE/logs/nginx.pid
	ExecStartPre=/bin/rm -f $DIRE/logs/nginx.pid
	ExecStartPre=$DIRE/sbin/nginx -t
	ExecStart=$DIRE/sbin/nginx
	ExecReload=/bin/kill -s HUB \$MAINPID
	KillSignal=SIGQUIT
	TimeoutStopSec=5
	KillMode=process
	PrivateTmp=true
	LimitNOFILE=100000

	[Install]
	WantedBy=multi-user.target
	EOF
	color 完毕 0
}

start_nginx(){
	echo "启动nginx"
	systemctl daemon-reload
	systemctl enable --now nginx
	if [ $? -eq 0 ];then
		color 启动成功 0
	else
		color 启动失败,请查看journalctl -xe 2
	fi
}
dependence
user
download
install
prepare
start_nginx


评论