将来的你
一定会感谢现在拼命努力的自己

Nginx 一键安装shell脚本

#!/bin/bash

LANG=en_US.UTF-8
name=Nginx
ver=1.0 
LockFile=/tmp/.$(basename $0).lock
LogFile=/tmp/.$(basename $0).log
BaseDir=/usr/local/src

# declare -A  定义关联数组 类似字典 键值对
# 所需文件及版本号
declare -A Files
Files=([nginx]="nginx-1.14.2" [pcre]="pcre-8.42" [openssl]="openssl-1.1.1a" [zlib]="zlib-1.2.11")
# 安装所需目录
declare -A Dirs
Dirs=([NginxInstallDir]="/usr/local/nginx" [WwwrootDir]="/home/wwwroot" [WwwlogsDir]="/home/wwwlogs" [DefaultDir]="/home/wwwroot/default" [VhostDir]="/home/panel/vhost")

# 扩展模块及功能配置
NginxModulesOptions=""

RunUser=www
thread=$(grep 'processor' /proc/cpuinfo | sort -u | wc -l)
MirrorLink=http://xxx.com/panel/lnmp

declare -A Colors
Colors=([success]="32m" [failure]="31m" [warning]="33m" [msg]="34m")

Echo() {
	[ ! $1 ] && flag="34m" || flag=$1
	echo -e "\033[1;${Colors[${flag}]}${2}\033[0m"
	#写日志
	#echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S"):${name}:[$1] $2" >> $LogFile
}

ShellLock(){
    touch $LockFile
}

ShellUnlock(){
    rm -f $LockFile
}

Exit(){
	ShellUnlock
	exit
}

CheckOS() {
	# 获取系统及版本
	if [ -e /etc/redhat-release ]; then
		OS=CentOS
		[ -n "$(grep ' 7\.' /etc/redhat-release 2> /dev/null)" ] && CentOSVer=7
		[ -n "$(grep ' 6\.' /etc/redhat-release 2> /dev/null)" ] && CentOSVer=6
	elif [ -n "$(grep -i 'Debian' /etc/issue 2> /dev/null)" ]; then
		OS=Debian
	elif [ -n "$(grep -i 'Ubuntu' /etc/issue 2> /dev/null)" ]; then
		OS=Ubuntu
	else
		OS=UnknownOS
	fi
}

Download() {
	url=$1
	[ -s "${url##*/}" ] && Echo "msg" "[${url##*/}] 已存在" || { wget --tries=6 -c --no-check-certificate $url; sleep 1; } #判断下载是否已存在
	if [ ! -e "${url##*/}" ]; then
		Echo "failure" "${url##*/}下载失败"
		Exit
	fi
}

InstallNginx() {
	pushd $BaseDir
	# 批量下载并解压所需包
	for key in ${!Files[*]};do
		[ ! -f ${Files[$key]}.tar.gz ] && Download(${MirrorLink}/${Files[$key]}.tar.gz)
		[ $? -eq 0 ] && tar xzf ${Files[$key]}.tar.gz
    done
	# 批量检测并创建所需目录
	for key in ${!Dirs[*]};do
		[ ! -d "${Dirs[$key]}" ] && mkdir -p ${Dirs[$key]}
    done

	id -u $RunUser >/dev/null 2>&1
	[ $? -ne 0 ] && useradd -M -s /sbin/nologin $RunUser
	pushd ${Files[nginx]}
	./configure --prefix=${Dirs[NginxInstallDir]} --user=$run_user --group=$run_user --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module \
	--with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-pcre=../${Files[pcre]} \
	--with-openssl=../${Files[openssl]} --with-zlib=../${Files[zlib]} --with-pcre-jit $NginxModulesOptions 
	make -j ${THREAD} && make install
	
	if [ -f "${Dirs[NginxInstallDir]}/conf/nginx.conf" ]; then
		popd
		# 批量清理下载文件及解压后文件夹
		for key in ${!Files[*]};do
			rm -rf ${Files[$key]}.tar.gz ${Files[$key]}
		done
		Echo "success" "Nginx${Files[nginx]}安装成功"	
	else
		rm -rf ${Dirs[NginxInstallDir]}
		Echo "failure" "${Files[nginx]}安装失败" && Exit
	fi
	# 判断加path
	[ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=${Dirs[NginxInstallDir]}/sbin:\$PATH" >> /etc/profile
	[ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep ${Dirs[NginxInstallDir]} /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=${Dirs[NginxInstallDir]}/sbin:\1@" /etc/profile
	sleep 1
	. /etc/profile
	
	[ "$OS" == "CentOS" ] && { wget -O /etc/init.d/nginxd http://xxx.com/shell/lnmp/init.d/nginxd.txt; chkconfig --add nginxd; chkconfig nginxd on; chmod +x /etc/init.d/nginxd; }
	
	sed -i "s@/usr/local/nginx@${Dirs[NginxInstallDir]}@g" /etc/init.d/nginxd
	cp ${Dirs[NginxInstallDir]}/conf/nginx.conf{,_bk}
	wget -O ${Dirs[NginxInstallDir]}/conf/nginx.conf http://xxx.com/shell/lnmp/config/nginx.txt
	sed -i "s@/data/wwwroot/default@${Dirs[DefaultDir]}@" ${Dirs[NginxInstallDir]}/conf/nginx.conf
	sed -i "s@/data/wwwlogs@${Dirs[WwwlogsDir]}@g" ${Dirs[NginxInstallDir]}/conf/nginx.conf
	sed -i "s@^user www www@user $RunUser $RunUser@" ${Dirs[NginxInstallDir]}/conf/nginx.conf
	ln -sf ${Dirs[VhostDir]} ${Dirs[NginxInstallDir]}/conf/vhost
	wget -O ${Dirs[DefaultDir]}/index.html http://xxx.com/shell/lnmp/config/index.txt
	sleep 1
	wget -O ${Dirs[DefaultDir]}/phpinfo.php http://xxx.com/shell/lnmp/config/phpinfo.txt
	chown $RunUser $RunUser -R ${Dirs[WwwrootDir]}
	chmod 744 -R ${Dirs[WwwrootDir]}
	popd
	ldconfig
	service nginxd start
}

Main() {
	[ -f "$LockFile" ] && Echo "warning" "请勿重复运行或删除锁文件$LockFile" && Exit
	ShellLock
	CheckOS
	yum install -y gcc-c++ perl pcre-devel openssl openssl-devel
	InstallNginx
	ShellUnlock
}

Main
赞(48) 打赏
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除,联系邮箱:contact@3yyy.top。文章观点不代表本站立场。本站原创内容未经允许不得转载:三叶运维 » Nginx 一键安装shell脚本
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

https://beian.miit.gov.cn/