Update install nginx script.

This commit is contained in:
BoHung Chiu 2022-08-04 15:32:48 +08:00
parent b18befc511
commit 28f11c5a99
1 changed files with 46 additions and 2 deletions

View File

@ -1,6 +1,9 @@
#!/bin/bash
function escape_str(){
echo $1|sed -E 's/\\+//g' |sed -E 's/[\/\.\*]/\\\0/g'
}
ubuntu_ver="$(lsb_release -rs)"
sudo echo "Starting "
sudo echo ""
org_pwd="$(pwd)"
cpu_cores="$(nproc --all)"
openssl_ver="$(openssl version|xargs| awk '{print $2}')"
@ -52,7 +55,12 @@ fi
nginx_ver="$(nginx -v 2>&1|xargs|awk '{print $3}'|cut -d '/' -f 2)"
nginx_target_ver="1.23.1"
if [[ "$nginx_ver" < $nginx_target_ver ]]; then
if [[ "$nginx_ver" < $nginx_target_ver ]] || [[ "$1" == '--force' ]]; then
if [ -f "/etc/nginx/nginx.conf" ]; then
nginx_conf_exist="1"
else
nginx_conf_exist="0"
fi
#Build nginx and install
sudo bash -l -c "
cd /root &&
@ -67,5 +75,41 @@ if [[ "$nginx_ver" < $nginx_target_ver ]]; then
ln -s /usr/share/nginx/sbin/nginx /usr/sbin/. &&
service nginx restart
"
if [[ $nginx_conf_exist == "0" ]]; then
nginx_conf_path="/etc/nginx/nginx.conf"
server_block_start=`sudo grep -E '^\s*server\s*{' $nginx_conf_path -n|cut -d : -f 1`
http_block_start=`sudo grep -E '^\s*http\s*{' $nginx_conf_path -n|cut -d : -f 1`
http_block_end_offset=`cat $nginx_conf_path | awk '{if (NR>='$http_block_start') print}'|grep -E '^}' -n|cut -d : -f 1|xargs|awk '{print $1}'`
http_block_end=$((http_block_end_offset + http_block_start - 1))
if [ -z "$server_block_start" ]; then
if [[ ! -f /etc/nginx/sites-enabled/default ]]; then
sudo mkdir -p /etc/nginx/sites-enabled
sudo wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/sites-enabled-default -O /etc/nginx/sites-enabled/default
fi
else
server_block_contents=`cat $nginx_conf_path | awk '{if (NR>='$server_block_start' && NR <'$http_block_end') print}'`
blank_text=`echo "$server_block_contents"|grep -E '^\s*' -m 1|sed 's/\w.*//g'`
server_block_contents=`echo "$server_block_contents"|sed "s/^$blank_text//g"`
sudo mkdir -p /etc/nginx/sites-enabled
echo "$server_block_contents"|sudo tee /etc/nginx/sites-enabled/default 1>/dev/null
nginx_conf_contents=`cat $nginx_conf_path | awk '{if (NR<'$server_block_start' || NR >='$http_block_end') print}'`
echo "$nginx_conf_contents"|sudo tee $nginx_conf_path 1>/dev/null
fi
http_block_end_offset=`cat $nginx_conf_path | awk '{if (NR>='$http_block_start') print}'|grep -E '^}' -n|cut -d : -f 1|xargs|awk '{print $1}'`
http_block_end=$((http_block_end_offset + http_block_start - 1))
include_list='/etc/nginx/conf.d/\*.conf /etc/nginx/sites-enabled/\*'
if [ -z "$(grep 'Virtual Host Configs' $nginx_conf_path)" ]; then
virtual_host_configs_text=`echo '\n ##\n # Virtual Host Configs\n ##'`
sudo sed -i "$((http_block_end-1)),+0s/.*/\0\\n $(echo "$virtual_host_configs_text")/g" $nginx_conf_path
http_block_end_offset=`cat $nginx_conf_path | awk '{if (NR>='$http_block_start') print}'|grep -E '^}' -n|cut -d : -f 1|xargs|awk '{print $1}'`
http_block_end=$((http_block_end_offset + http_block_start - 1))
fi
for file_list in $include_list; do
if [[ "$(cat $nginx_conf_path)" != *"$(echo $file_list|sed 's/\\//g')"* ]]; then
sudo sed -i $((http_block_end-1)),+0's/.*/\0\n include '$(escape_str $file_list)';/g' $nginx_conf_path
http_block_end=$((http_block_end + 1))
fi
done
fi
cd "$org_pwd"
fi