1 Star 2 Fork 1

hxsaj/tools_shell

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
nginx_server_0.0.2.sh 7.34 KB
一键复制 编辑 原始数据 按行查看 历史
hxsaj 提交于 2024-03-18 01:19 . 新增部署mysql8
#!/usr/bin/env bash
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :CentOS7.X nginx 服务编译安装
# Platform :RedHatEL7.x Based Platform
# Version :1.2
# Date :2021-07-06
# Author :mugoLH
# Contact :houxiaoshuai@baidu.com & hxsaj@126.com
# Company :
# depend on:
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
set -e
# 变量列表 List of common variables
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# 1.1 定义变量
# nginx 部署路径
NginxDir="/usr/local/nginx"
# 安装日志
Install_Log="nginx_install.log"
# nginx 的启动用户
NGuser=www
# nginx 的启动用户组
NGgroup=www
# 初始支持软件
SoftBase=(vim lrzsz wget)
# nginx 编译安装依赖软件
SoftMake=(pcre-devel pcre gcc gcc-c++ openssl openssl-devel zlib-devel net-tools)
# 1.2 定义编译模块
ConfigureMoudule="\
--prefix=${NginxDir} \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_mp4_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-stream \
--with-pcre"
# 公共函数列表 List of common functions
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# [ ---- ---- 色彩函数定义
# 指示 # echo -e "\n\033[1m 色彩测试-提示 \033[0m\n"
function show_tips() {
echo -e "\033[1m $@ \033[0m" >&1;
}
# 提示 # echo -e "\n\033[1;38;46m 色彩测试-提示 \033[0m\n"
function show_notice() {
echo -e "\033[1;38;46m $@ \033[0m" >&1;
}
# 警告 # echo -e "\n\033[1;35;46m 色彩测试-警告 \033[0m\n"
function show_warning(){
echo -e "\033[1;35;46m $@ \033[0m" >&2;
}
# 报错 # echo -e "\n\033[1;31;46m 色彩测试-错误 \033[0m\n"
function show_error(){
echo -e "\033[1;31;46m $@ \033[0m" >&2;
}
# 应用函数列表 Application function list
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# 列出防火墙开放的端口
function firewalld_open_port() {
# 检查firewalld是否运行
if [ $(systemctl status firewalld| grep -w Active|awk -F "[(,)]" '{print$2}') == "running" ];then
# 检查是否目标端口已开
for i in $@ ;do
if ! $(firewall-cmd --zone=public --list-ports | grep -qw ${i}); then
firewall-cmd --zone=public --add-port=${i}/tcp --permanent >/dev/null 2>&1
firewall-cmd --reload >/dev/null 2>&1
fi
done
# 展示所有已开端口
# firewall-cmd --zone=public --list-ports
else
show_notice "[ info ] :firewalld 未运行 "
fi
}
# 关闭SELinux
function selinux_off() {
if [ $(grep -w ^SELINUX /etc/selinux/config|awk -F "=" '{print$2}') == "enforcing" ];then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
fi
if [[ $(getenforce) == "Enforcing" ]];then
setenforce 0 >/dev/null 2>&1
fi
}
# 安装软件
function yum_install() {
yum install -y $@ >/dev/null 2>&1 || show_error "[ ERR ] :软件安装失败 !!! "
}
# 获取nginx版本列表函数
function nginx_version_list() {
curl -s http://nginx.org/en/download.html | grep -e "tar.gz\"" |awk 'BEGIN{i=1}{gsub("href","\n",$0);i++;print}' | grep tar.gz | grep -v asc | awk -F ">|<" '{print$2}' > /tmp/.nginx.list
cat -b /tmp/.nginx.list |awk 'BEGIN{
print " ---- ---- ---- ---- ---- ";
print "| 序号 | Nginx版本 |" ;
print " ---- ---- ---- ---- ---- ";
}
{
printf " %-6s %-10s \n",$1,$2;
}
END{
print" ";
}' |tee /tmp/.nginx-nu.list
}
function nginx_download_source() {
# 如果函数带有指定安装的版本就静默安装
if [ $# = 0 ]; then
# 展示 nginx 版本列表
nginx_version_list
# 选择版本
read -p " [ 选择 ] 源代码版本(输入序号即可):" NgNU
export NginxVersion=$(cat /tmp/.nginx-nu.list|awk '{print$1" "$2}' |grep "^${NgNU}" |awk '{print$2}')
# 下载源码包
wget -c http://nginx.org/download/${NginxVersion}.tar.gz >/dev/null 2>&1 || show_error "[ ERR ] :下载 nginx 源码失败 !!! "
elif [ $# = 1 ]; then
# 下载指定的版本
NginxVersion="${1}"
wget -c http://nginx.org/download/${NginxVersion}.tar.gz >/dev/null 2>&1 || show_error "[ ERR ] :下载 nginx 源码失败 !!! "
fi
}
function nginx_compile() {
# 解压压缩包
tar -xf ${1}.tar.gz && cd ${1}
# 编译安装
./configure ${ConfigureMoudule} >./${Install_Log} 2>&1 || show_error "[ ERR ] : 预编译失败 !!! "
make -j $(cat /proc/cpuinfo | grep "processor" | wc -l) >./${Install_Log} 2>&1 && make install >./${Install_Log} 2>&1 || show_error " make编译失败 !!! "
}
function nginx_start() {
# 默认配置启动 Nginx 服务
${NginxDir}/sbin/nginx || show_error "[ ERR ] : nginx启动失败 !!! "
}
function nginx_stop() {
# 关闭 Nginx 服务
kill -INT $(ps -ef | grep nginx | grep master | awk '{print$2}')
}
# 卸载nginx
function nginx_uninstall(){
${NginxDir}/sbin/nginx -s stop
#for i in $(ps -ef |grep $(ps -ef |grep nginx|grep "${NginxDir}"|grep -v grep|awk '{print$2}')|awk '{print$2}');do
# kill -9 $i;
#done
sleep 2
rm -rf ${NginxDir} && show_tips "[ Done ] nginx卸载完成!"
}
# 编译安装 nginx
function nginx_install_compile() {
# 1,编译安装nginx
show_notice " ------ 编译安装nginx ------"
# 1.1,安装依赖的基础软件
show_tips " [ 1 ],安装基础软件 " && yum_install ${SoftBase[@]}
# 1.4,安装编译支持
show_tips " [ 2 ],安装编译依赖 " && yum_install ${SoftMake[@]}
# 2.1,下载 nginx 版本
if [ $# = 0 ]; then
show_tips " [ 3 ],选择部署版本...... " && nginx_download_source
elif [ $# = 1 ]; then
show_tips " [ 3 ],下载nginx...... " && nginx_download_source ${1}
fi
# 2.2 编译安装nginx
show_tips " [ 4 ],编译安装 " && nginx_compile ${NginxVersion}
# 2.3 关闭SeLinux
show_tips " [ 5 ],关闭Selinux " && selinux_off
# 2.4 防火墙开放端口
show_tips " [ 6 ],防火墙放行80和443 " && firewalld_open_port 80 443
# 2.6 运行nginx 并查看状态
show_tips " [ 7 ],启动nginx...... " && nginx_start
echo ""
# 2.7 检测安装完成的版本的和运行状态
Installed_Nginx_Version=$(${NginxDir}/sbin/nginx -v 2>&1 | awk -F "/" '{print$2}')
show_notice "[ info ] : NGINX 部署位置:${NginxDir}"
show_notice "[ info ] : NGINX 已安装版本:${Installed_Nginx_Version}"
show_notice "[ info ] : NGINX 运行状态:" && ps -ef | grep nginx | egrep -wv "grep|sh"
show_notice "[ info ] : NGINX 监听状态:" && netstat -atnlp | grep nginx
}
##############################################################################################
if [ $# = 0 ]; then
if [ $(echo $LANG) == "zh_CN.UTF-8" ];then
show_tips "应用选项:sh $0 [ start|stop|install|install {Nginx_version}|uninstall ]"
show_tips "[ eg ]:(启动nginx):sh $0 start "
show_tips "[ eg ]:(交互安装nginx版本):sh $0 install "
show_tips "[ eg ]:(安装指定版本nginx):sh $0 install nginx-1.18.0 "
else
show_tips "[ eg ]: sh $0 [ start|stop|install|install {Nginx_version}|uninstall ]"
show_tips "[ eg ]: sh $0 install "
show_tips "[ eg ]: sh $0 install nginx-1.18.0 "
fi
elif [ $# = 1 ]; then
# 只有一个位置参数,就判断是关闭、启动、、安装还是卸载
case ${1} in
"start" ) nginx_start ;;
"stop" ) nginx_stop ;;
"install" ) nginx_install_compile ;;
"uninstall") nginx_uninstall ;;
esac
elif [ $# = 2 ]; then
# 安装脚本指定版本
Nginx_V=${2}
nginx_install_compile ${Nginx_V}
fi
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Shell
1
https://gitee.com/hxsaj/tools_shell.git
git@gitee.com:hxsaj/tools_shell.git
hxsaj
tools_shell
tools_shell
master

搜索帮助