CentOS一键安装nginx-1.19.6

54 篇文章 2 订阅
17 篇文章 0 订阅

nginx-1.19.6.sh

[root@ script]# cat /opt/script/nginx-1.19.6.sh
#!/bin/sh
# 从源码安装nginx-1.19.6
# 解压后包名
App=nginx-1.19.6
# 安装包名称
AppTar=nginx-1.19.6.tar.gz
# 安装目录
AppInstallBase=/opt
# 安装目录下包名
AppName=nginx
# 脚本目录
ScriptDir=$AppInstallBase/script
# 安装包保存目录
AppTarDir=$AppInstallBase/soft
# build目录
AppBuildBase=$AppInstallBase/soft/build
# PCRE
PCRE=pcre-8.35


# 安装依赖
fdepend(){
	yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
}

# 判断是否已经下载了tar包
fdownload(){
	echo "第一步"
	echo "--------------开始下载$AppTar --------------"
	# 先判断是否已下载
	if [[ ! -f $AppTarDir/$AppTar ]]; then
		echo "$AppTar 不存在"
		echo "Start download $AppTar"
		# 判断soft目录是否存在
		if [[ ! -d $AppTarDir ]]; then
			echo "$AppTarDir 不存在"
			mkdir -p $AppTarDir
			echo "$AppTarDir 目录新建完成"
		fi
		wget -O $AppTarDir/$AppTar http://nginx.org/download/$AppTar
	fi
	echo "--------------解压处理--------------"
    # 解压
	tar -zxf $AppTarDir/$AppTar
	# 重命名并拷贝到build目录
	if [[ ! -d $AppBuildBase ]]; then
		# 如果文件夹不存在,新建目录
		echo "mkdir build"
		mkdir -p $AppBuildBase/$AppName 		  
	fi 
	rm -rf $AppBuildBase/$AppName
	mv $ScriptDir/$App $AppBuildBase/$AppName

	echo "--------------完成下载$AppTar --------------"

	echo "--------------安装 PCRE --------------"
	# PCRE 作用是让 Nginx 支持 Rewrite 功能
	if [[ ! -f $AppTarDir/$PCRE.tar.gz ]]; then
		wget -O $AppTarDir/$PCRE.tar.gz http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
		# 解压到当前文件
		tar -zxf $AppTarDir/$PCRE.tar.gz
		rm -rf $AppBuildBase/$PCRE
		mv $ScriptDir/$PCRE $AppBuildBase/$PCRE
		# 编译
		cd $AppBuildBase/$PCRE
		./configure

		# 成功后执行
		[ $? -eq 0 ] && make && make install
		# 查看版本
		pcre-config --version
		cd $ScriptDir
	fi
	echo "--------------完成安装 PCRE --------------"


	echo "--------------安装 Nginx --------------"
	cd $AppBuildBase/$AppName

	./configure \
	"--prefix=$AppInstallBase/$AppName" \
	"--with-http_stub_status_module" \
	"--with-http_ssl_module" \
	"--with-http_gzip_static_module" \
	"--with-pcre=$AppBuildBase/$PCRE"

	# 成功后执行
	[ $? -eq 0 ] && make && make install

	if [ $? -eq 0 ]; then
        echo "$AppName 成功"
    else
        echo "$AppName 失败"
    fi
	echo "--------------完成安装 Nginx --------------"
}

# 增加配置
fconfig(){
	echo "第二步"
	echo "--------------开始配置/etc/profile--------------"
	# 判断/etc/profile是否已有配置
	grep "export NGINX_HOME=$AppInstallBase/$AppName" /etc/profile 
	# $? 最后运行的命令的结束代码(返回值)即执行上一个指令的返回值  0表示没有错误
	if [[ ! $? -eq 0 ]]; then
		echo "新增配置信息"
		echo "export NGINX_HOME=$AppInstallBase/$AppName" >> /etc/profile
    	echo "export PATH=\$PATH:$NGINX_HOME/sbin" >> /etc/profile
    	source /etc/profile
	else
		echo "/etc/profile中 $App 配置已存在"
	fi

	echo "--------------开始配置~/.bashrc, 增加全局变量--------------"
    # 增加全局变量
    # 判断/etc/profile是否已有配置
	grep "export NGINX_HOME=$AppInstallBase/$AppName" ~/.bashrc 
	# $? 最后运行的命令的结束代码(返回值)即执行上一个指令的返回值  0表示没有错误
	if [[ ! $? -eq 0 ]]; then
		echo "新增配置信息"
		echo "export NGINX_HOME=$AppInstallBase/$AppName" >> ~/.bashrc
    	echo "export PATH=\$PATH:$NGINX_HOME/sbin" >> ~/.bashrc
    	source ~/.bashrc
	else
		echo "~/.bashrc中 $App 配置已存在"
	fi
    echo "--------------完成配置~/.bashrc--------------"
}

# 版本
fversion(){
	echo "第三步"
	echo "--------------查看状态--------------"
	nginx -version
	nginx -V
}

# 安装
finstall(){
	fdepend
	fdownload
	fconfig
	fversion
}

# 执行
finstall

执行安装

[root@ script]# sh /opt/script/nginx-1.19.6.sh
第一步
....
nginx 成功
--------------完成安装 Nginx --------------
第二步
--------------开始配置/etc/profile--------------
新增配置信息
--------------开始配置~/.bashrc, 增加全局变量--------------
export NGINX_HOME=/opt/nginx
~/.bashrc中 nginx-1.19.6 配置已存在
--------------完成配置~/.bashrc--------------
第三步
--------------查看状态--------------
nginx version: nginx/1.19.6
nginx version: nginx/1.19.6
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
built with OpenSSL 1.1.1g FIPS  21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre=/opt/soft/build/pcre-8.35
[root@AlexWong script]# nginx -v
nginx version: nginx/1.19.6

[root@AlexWong nginx]# nginx -v
nginx version: nginx/1.19.6
[root@AlexWong nginx]# nginx -V
nginx version: nginx/1.19.6
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
built with OpenSSL 1.1.1g FIPS  21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre=/opt/soft/build/pcre-8.35
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值