2025-03-19 17:38:46 +08:00

204 lines
6.1 KiB
Bash

#!/bin/bash
function install_nginx() {
###########################################
readonly install_path=/usr/local/
###########################################
[ $? -ne 0 ] && yum install wget -y
if [ $# -lt 2 ]
then
version=1.14.2
else
version=$2
fi
readonly gzip_file_name="nginx-${version}.tar.gz"
readonly file_name="nginx-${version}"
readonly workdir="./$file_name"
#################################################
readonly install_directory=${install_path}/nginx-${version}
###########################################
mkdir -p $workdir
cd $workdir
[ $? -ne 0 ] && echo -e "\033[31m\033[01mChange Directory Error: No Such File Or Path\033[0m" && exit
which wget &> /dev/null
[ $? -ne 0 ] && yum install wget -y
echo -en "install nginx-${version}."
sleep 1
echo -en "."
sleep 1
echo -en "."
sleep 2 && echo
download_url="http://nginx.org/download/nginx-${version}.tar.gz"
[ ! -f $gzip_file_name ] && wget $download_url -O $gzip_file_name
################### install packages #################
yum install gcc-c++ make pcre-devel openssl-devel libxml2-devel libxslt-devel -y
################### configure ########################
readonly backup_date=$(date +%Y-%m-%d_%H-%M-%S)
[ -d $file_name ] && mv $file_name ${file_name}_$backup_date
tar xf $gzip_file_name
cd $file_name
./configure \
--prefix=${install_directory} \
--without-select_module \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_xslt_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_v2_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-file-aio \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_auth_request_module \
--with-pcre
make && make install
ln -sf ${install_directory}/sbin/nginx /usr/local/sbin/
# /usr/bin/cp contrib/vim/* /usr/share/vim/vimfiles/ -r # 添加 vim 对 nginx 语法高亮的支持
# 使用其他方式语法高亮
cd -
echo "install success "
echo "install_directory: ${install_directory}"
}
############################################
function do_rewrite_nginx_page(){
cd ${install_directory}
cat << EOF > html/500.html
<!DOCTYPE html>
<html>
<head>
<!-- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> -->
<meta name="format-detection" content="telephone=no">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0" />
<title>訪問限制</title>
</head>
<body>
<div>
<h1>STOP!</h1>
<h2>訪問限制</h2>
<span>由於您所在的地區不在我們的服務範圍內,我們暫時無法為您提供服務,請您諒解!</span>
<p>Access restrictions</p>
<p>
Because you are not in the area of our service, we are unable to provide you with services for the time being, please forgive us!
</p>
</div>
</body>
</html>
EOF
mkdir conf.d
ln -sf `pwd`/conf.d ~/conf.d
mkdir pids
####################### dir (conf) #################
cd conf
mkdir bak
cp nginx.conf bak/
cat << EOF > ./nginx.conf
user root;
worker_processes auto;
pid pids/nginx.pid;
events {
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
log_format main-upstream '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" forword-for: "\$http_x_forwarded_for" '
'upstream_addr: \$upstream_addr upstream_status: \$upstream_status \$upstream_response_time';
access_log logs/access.log main;
error_log logs/error.log;
sendfile on;
#tcp_nopush on;
keepalive_timeout 120;
include /root/conf.d/*.conf ;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 5;
error_page 500 /500.html;
gzip_types application/x-javascript text/css application/octet-strea image/png audio/mpeg applic
ation/javascript application/json;
client_max_body_size 100M;
server {
listen 80 default_server;
server_name _;
access_log logs/default_server.log main;
location = /500.html {
root html/;
}
location / {
return 500 ;
}
}
}
EOF
echo -e "\033[32m\033[01mInstall [Nginx] Success! conf.d = [/root/conf.d/]\033[0m"
}
function do_test(){
readonly file_name="list"
file_content=$(cat $file_name)
for i in `echo $file_content`
do
echo $i | grep -v td | grep -v Windows | awk -F">" '{print $2}' | awk -F"<" '{print $1}'
done
}
function get_nginx_list(){
url="https://nginx.org/en/download.html"
x=`curl ${url} 2> /dev/null`
for i in $x
do
echo $i | grep "/download/nginx" | grep -v td | grep -v Windows | awk -F">" '{print $2}' | awk -F"<" '{print $1}'
done
}
function help1(){
echo
echo -e "\033[01m"
echo "$0 list"
echo "$0 install [version default=1.14.2]"
echo
echo "ForExample: "
echo "$0 list -> list nginx version"
echo "$0 install -> install nginx version=1.14.2"
echo "$0 install 1.16.1 -> install nginx version=1.16.1"
echo -e "\033[0m"
echo
}
case $1 in
"list")
get_nginx_list
;;
"install")
install_nginx $@
do_rewrite_nginx_page
;;
*)
help1
;;
esac