#!/bin/bash
#Author Mr.Wu
#Nginx安装脚本
#2023-2-23 21:53:56
########################################
#定义变量
read -p "请输入自定义Nginx版本号:" name1
read -p "请输入自定义Nginx名称:" name2
dev_dir="/dev/null"
nginx_tar="$(pwd)/nginx-1.22.0.tar.gz"
nginx_log="$(pwd)/nginx-install.log"
src_dir="/usr/local/src"
nginx_dir="/usr/local/nginx"
aaa="/usr/local/src/nginx-1.22.0/src/core/nginx.h"
bbb="/usr/local/src/nginx-1.22.0/src/http/ngx_http_header_filter_module.c"
ccc="/etc/systemd/system/nginx.service"
ddd="/usr/local/nginx/conf/nginx.conf"
eee="/etc/security/limits.conf"
packages="gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel"
ip="$(ip a | awk '/scope global/{print $2}' | cut -d "/" -f 1 | sed -n "1p")"
messages=("清理并重新加载缓存" "检查Nginx安装包是否存在" "下载Nginx安装包" "解压Nginx安装包" "设置Nginx版本号及名称" "安装Nginx依赖包" "Nginx预编译" "Nginx编译" "Nginx编译安装" "设置Nginx软链接" "Nginx启动管理" "设置Nginx运行账号" "设置Nginx进程数" "设置Nginx运行CPU亲和力" "设置Nginx事件处理模型" "设置Nginx最大可打开文件数")
colour=(31 34)
result=("失败!!!" "成功!!!")
########################################
#定义函数
delimiter(){#定义分隔符
echo -e "\033[32;1m※※※※※※※※※※※※※※※※※※※※※※※※※※\033[0m"
}
echoinfo(){#定义输入选项
echo -e "\033[35;1m#$1\033[0m"
}
echoresult(){#定义输出选项
echo -e "\033[$1;1m$2$3\033[0m"
}
if_test(){#if语句判断
if [ $? -eq 0 ];then
echoresult ${colour[1]} $1 ${result[1]}
else
echoresult ${colour[0]} $1 ${result[0]}
exit
fi
}
clean(){#清理并重新加载缓存
delimiter | tee -a $nginx_log
echoinfo ${messages[0]} | tee -a $nginx_log
yum clean all &>> $nginx_log
yum makecache &>> $nginx_log
sleep 3
if_test ${messages[0]} | tee -a $nginx_log
}
check_tar(){#检查Nginx安装包是否存在
delimiter | tee -a $nginx_log
echoinfo ${messages[1]} | tee -a $nginx_log
if [ ! -f $nginx_tar ];then
echo -e "\033[31;1mNginx安装包不存在,请下载!!!\033[0m" | tee -a $nginx_log
else
echo -e "\033[34;1mNginx安装包已经存在,请解压安装!!!\033[0m" | tee -a $nginx_log
fi
}
get_tar(){#下载Nginx安装包
delimiter | tee -a $nginx_log
echoinfo ${messages[2]} | tee -a $nginx_log
wget http://nginx.org/download/nginx-1.22.0.tar.gz &> $dev_dir
sleep 3
if_test ${messages[2]} | tee -a $nginx_log
}
dec_tar(){#解压Nginx安装包
delimiter | tee -a $nginx_log
echoinfo ${messages[3]} | tee -a $nginx_log
tar -zxvf $nginx_tar -C $src_dir/ &>> $nginx_log
sleep 3
if_test ${messages[3]} | tee -a $nginx_log
}
optimize1(){#初步优化Nginx
#隐藏Nginx版本号及自定义名称
delimiter | tee -a $nginx_log
echoinfo ${messages[4]} | tee -a $nginx_log
#sed -i '/#define NGINX_VERSION "1.22.0"/c \#define NGINX_VERSION "$name1"' $aaa &>> $nginx_log
sed -i "13s/1.22.0/$name1/" $aaa &>> $nginx_log
sed -i "14s/nginx/$name2/" $aaa &>> $nginx_log
#sed -i "/#define NGINX_VER "nginx\/" NGINX_VERSION/c \#define NGINX_VER "$name2\/" NGINX_VERSION" $aaa &>> $nginx_log
sed -i "s/"Server: nginx" CRLF/"Server: $name2" CRLF/g" $bbb &>> $nginx_log
sleep 3
if_test ${messages[4]} | tee -a $nginx_log
}
depend(){#安装Nginx依赖包
delimiter | tee -a $nginx_log
echoinfo ${messages[5]} | tee -a $nginx_log
yum install -y $packages &> $dev_dir
sleep 3
if_test ${messages[5]} | tee -a $nginx_log
}
precom(){#Nginx预编译
delimiter | tee -a $nginx_log
echoinfo ${messages[6]} | tee -a $nginx_log
cd $src_dir/nginx-1.22.0/
./configure --prefix=$nginx_dir --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module &>> $nginx_log
sleep 3
if_test ${messages[6]} | tee -a $nginx_log
}
compile(){#Nginx编译
delimiter | tee -a $nginx_log
echoinfo ${messages[7]} | tee -a $nginx_log
cd $src_dir/nginx-1.22.0/
make -j `grep ^processor /proc/cpuinfo | wc -l` &>> $nginx_log
sleep 3
if_test ${messages[7]} | tee -a $nginx_log
}
com_inst(){#Nginx编译安装
delimiter | tee -a $nginx_log
echoinfo ${messages[8]} | tee -a $nginx_log
cd $src_dir/nginx-1.22.0/
make install &>> $nginx_log
sleep 3
if_test ${messages[8]} | tee -a $nginx_log
}
set_lns(){#设置Nginx软链接
delimiter | tee -a $nginx_log
echoinfo ${messages[9]} | tee -a $nginx_log
ln -s /usr/local/nginx/sbin/* /usr/local/bin/ &>> $nginx_log
sleep 3
if_test ${messages[9]} | tee -a $nginx_log
}
set_star(){#Nginx启动管理
delimiter | tee -a $nginx_log
echoinfo ${messages[10]} | tee -a $nginx_log
echo "[Unit]
Description=Nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target" > $ccc | tee -a $nginx_log
systemctl daemon-reload &> $dev_dir
systemctl start nginx &> $dev_dir
systemctl enable nginx &>> $nginx_log
sleep 3
systemctl status nginx &>> $nginx_log
netstat -antup | grep nginx &>> $nginx_log
ps -ef | grep nginx | grep -v grep | grep -v "nginx-install.sh" &>> $nginx_log
num1="$(ps -ef | grep nginx | grep -v grep | grep -v "nginx-install.sh" | wc -l)"
if [ "$num1" -gt 0 ];then
echo -e "\033[34;1mNginx服务启动成功!!!\033[0m" | tee -a $nginx_log
else
echo -e "\033[31;1mNginx服务启动失败!!!\033[0m" | tee -a $nginx_log
fi
curl -I $ip &>> $nginx_log
}
optimize2(){#进一步优化Nginx
#设置Nginx运行账号
delimiter | tee -a $nginx_log
echoinfo ${messages[11]} | tee -a $nginx_log
#abb="$(ps -ef | grep nginx | grep -v grep | grep -v "nginx-install.sh" | grep -v "root" | awk '{print $1}' | sed -n "1p")"
useradd -M -s /sbin/nologin nginx &>> $nginx_log
sed -i "/\#user nobody;/c \user nginx;" $ddd &>> $nginx_log
nginx -t &>> $nginx_log
nginx -s reload &>> $nginx_log
systemctl daemon-reload &> $dev_dir
sleep 3
ps -ef | grep nginx | grep -v grep | grep -v "nginx-install.sh" &>> $nginx_log
acc="$(ps -ef | grep nginx | grep -v grep | grep -v "nginx-install.sh" | grep -v "root" | awk '{print $1}' | sed -n "1p")"
if [ "$acc"x = "nginx"x ];then
echo -e "\033[34;1m运行账号优化成功!!!\033[0m" | tee -a $nginx_log
else
echo -e "\033[31;1m运行账号优化失败!!!\033[0m" | tee -a $nginx_log
fi
#设置Nginx进程数
delimiter | tee -a $nginx_log
echoinfo ${messages[12]} | tee -a $nginx_log
fig="$(grep "^worker_processes" $ddd | awk -F " " '{print $2}' | awk -F ";" '{print $1}')"
num2="$(grep ^processor /proc/cpuinfo | wc -l)"
sed -i "/worker_processes $fig;/c \worker_processes $num2;" $ddd &>> $nginx_log
nginx -t &>> $nginx_log
nginx -s reload &>> $nginx_log
systemctl daemon-reload &> $dev_dir
sleep 3
ps -ef | grep nginx | grep -v grep | grep -v "nginx-install.sh" &>> $nginx_log
num3="$(ps -ef | grep nginx | grep -v grep | grep -v "nginx-install.sh" | wc -l)"
if [ "$num3" -gt 2 ];then
echo -e "\033[34;1m设置进程数成功!!!\033[0m" | tee -a $nginx_log
else
echo -e "\033[31;1m设置进程数失败!!!\033[0m" | tee -a $nginx_log
fi
#设置Nginx运行CPU亲和力
delimiter | tee -a $nginx_log
echoinfo ${messages[13]} | tee -a $nginx_log
num4="$(grep "^worker_processes" $ddd | awk -F " " '{print $2}' | awk -F ";" '{print $1}')"
if [ "$num4" -eq 1 ];then
echo "CPU个数为$num4,不做任何修改!!!" &>> $nginx_log
nginx -t &>> $nginx_log
nginx -s reload &>> $nginx_log
systemctl daemon-reload &> $dev_dir
elif [ "$num4" -eq 2 ];then
sed -i "/worker_processes $num4;/a\worker_cpu_affinity 01 10;" $ddd &>> $nginx_log
nginx -t &>> $nginx_log
nginx -s reload &>> $nginx_log
systemctl daemon-reload &> $dev_dir
elif [ "$num4" -eq 4 ];then
sed -i "/worker_processes $num4;/a\worker_cpu_affinity 0001 0010 0100 1000;" $ddd &>> $nginx_log
nginx -t &>> $nginx_log
nginx -s reload &>> $nginx_log
systemctl daemon-reload &> $dev_dir
elif [ "$num"4 -eq 8 ];then
sed -i "/worker_processes $num4;/a\worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;" $ddd &>> $nginx_log
nginx -t &>> $nginx_log
nginx -s reload &>> $nginx_log
systemctl daemon-reload &> $dev_dir
else
echo "ERROR!!!ERROR!!!ERROR!!!" &>> $nginx_log
fi
sleep 3
if_test ${messages[13]} | tee -a $nginx_log
#设置Nginx事件处理模型
delimiter | tee -a $nginx_log
echoinfo ${messages[14]} | tee -a $nginx_log
num5="$(cat /proc/sys/fs/file-max)"
num6="102400"
num7="$(grep "worker_connections" $ddd | awk -F " " '{print $2}' | awk -F ";" '{print $1}')"
if [ "$num5" -ge "$num6" ];then
sed -i '/worker_connections/i\ use epoll;' $ddd &>> $nginx_log
sed -i "/ worker_connections $num7;/c \ worker_connections $num6;" $ddd &>> $nginx_log
nginx -t &>> $nginx_log
nginx -s reload &>> $nginx_log
systemctl daemon-reload &> $dev_dir
elif [ "$num5" -lt "$num6" ];then
sed -i '/worker_connections/i\ use epoll;' $ddd &>> $nginx_log
sed -i "/ worker_connections $num7;/c \ worker_connections $num7;" $ddd &>> $nginx_log
nginx -t &>> $nginx_log
nginx -s reload &>> $nginx_log
systemctl daemon-reload &> $dev_dir
else
echo "请更换服务器!!!" &>> $nginx_log
fi
sleep 3
if_test ${messages[14]} | tee -a $nginx_log
#设置Nginx最大可打开文件数
delimiter | tee -a $nginx_log
echoinfo ${messages[15]} | tee -a $nginx_log
num8="$(ulimit -n)"
if [ "$num5" -ge "$num6" -o "$num6" -ge "$num8" ];then
sed -i "\$a \* soft nofile $num6" $eee &>> $nginx_log
sed -i "\$a \* hard nofile $num6" $eee &>> $nginx_log
elif [ "$num5" -lt "$num6" -o "$num6" -lt "$num8" ];then
sed -i "\$a \* soft nofile $num8" $eee &>> $nginx_log
sed -i "\$a \* hard nofile $num6" $eee &>> $nginx_log
else
echo "请更换服务器!!!" &>> $nginx_log
fi
sleep 3
nginx -t &>> $nginx_log
nginx -s reload &>> $nginx_log
systemctl daemon-reload &> $dev_dir
if_test ${messages[15]} | tee -a $nginx_log
}
########################################
#调用函数
echo "################BEGIN!!!$(date)################" | tee -a $nginx_log
clean
wait
check_tar
wait
get_tar
wait
dec_tar
wait
optimize1
wait
depend
wait
precom
wait
compile
wait
com_inst
wait
set_lns
wait
set_star
wait
optimize2
wait
echo "################END!!!$(date)################" | tee -a $nginx_log
########################################