From 28f11c5a99841e0af0347b7c435feccf0b002099 Mon Sep 17 00:00:00 2001 From: bohung Date: Thu, 4 Aug 2022 15:32:48 +0800 Subject: [PATCH] Update install nginx script. --- install_nginx.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/install_nginx.sh b/install_nginx.sh index fd26594..c239ca4 100644 --- a/install_nginx.sh +++ b/install_nginx.sh @@ -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 \ No newline at end of file