diff --git a/add_proxy_parameter_to_vhost_configuration.sh b/add_proxy_parameter_to_vhost_configuration.sh new file mode 100755 index 0000000..f3b8bd6 --- /dev/null +++ b/add_proxy_parameter_to_vhost_configuration.sh @@ -0,0 +1,465 @@ +#!/usr/bin/env bash + +script_name="$(basename $(realpath $0))" +working_dir="$(dirname $(realpath $0))" + +conf_file="${working_dir}/conf/${script_name%%.*}.conf" + +tmp_log_file="$(mktemp)" + +backup_date="$(date +%Y-%m-%d-%H%M)" + + +# ------------- +# - Variable settings +# ------------- + +declare -a vhost_php_fpm_file_arr=() +apache_needs_restart=false + +if [[ -f "/usr/local/apache2/conf/httpd.conf" ]]; then + _HTTPD_CONF_FILE="/usr/local/apache2/conf/httpd.conf" +elif [[ -f "/etc/apache2/apache2.conf" ]]; then + _HTTPD_CONF_FILE="/etc/apache2/apache2.conf" +else + _HTTPD_CONF_FILE='' +fi + +if [[ -f "${_HTTPD_CONF_FILE}" ]] && $(grep -q -i -E "^\s*Timeout\s+" "${_HTTPD_CONF_FILE}") ; then + _TIMEOUT="$(grep -i -E "^\s*timeout\s+" "${_HTTPD_CONF_FILE}"| awk '{print$2}' | head -1)" +else + _TIMEOUT=60 +fi +if [[ -f "${_HTTPD_CONF_FILE}" ]] && $(grep -q -i -E "^\s*ProxyTimeout\s+" "${_HTTPD_CONF_FILE}") ; then + PROXY_TIMEOUT="$(grep -i -E "^\s*ProxyTimeout\s+" "${_HTTPD_CONF_FILE}"| awk '{print$2}' | head -1)" +else + PROXY_TIMEOUT=${_TIMEOUT} +fi + +# ------------- +# --- Some functions +# ------------- + +clean_up() { + + # Perform program exit housekeeping + rm -f $tmp_log_file + exit $1 +} + +# - Check if a given array (parameter 2) contains a given string (parameter 1) +# - +containsElement () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} + +stringContain() { + [ -z "${2##*$1*}" ] && [ -z "$1" -o -n "$2" ] +} + +# - Remove leading/trailling whitespaces +# - +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +echononl(){ + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e -n "$*\\c" 1>&2 + else + echo -e -n "$*" 1>&2 + fi + rm /tmp/shprompt$$ +} + +fatal(){ + echo "" + if [[ -n "$*" ]] ; then + echo -e " [ \033[31m\033[1mFatal\033[m ]: $*" + echo "" + fi + echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m" + echo "" + clean_up 1 +} + +error(){ + echo "" + echo -e " [ \033[31m\033[1mFehler\033[m ]: $*" + echo "" +} + +warn (){ + echo "" + echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info (){ + echo "" + echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" + echo "" +} + +echo_done() { + echo -e "\033[80G[ \033[32mdone\033[m ]" +} +echo_ok() { + echo -e "\033[80G[ \033[32mok\033[m ]" +} +echo_warning() { + echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]" +} +echo_failed(){ + echo -e "\033[80G[ \033[1;31mfailed\033[m ]" +} +echo_skipped() { + echo -e "\033[80G[ \033[37mskipped\033[m ]" +} + +trap clean_up SIGHUP SIGINT SIGTERM + + +# - Try to find apache vhost configuration directory +# - +if [[ -d "/usr/local/apache2/conf/vhosts" ]]; then + if [[ -d "/usr/local/apache2/conf/vhosts/0" ]]; then + _apache_vhost_dir="/usr/local/apache2/conf/vhosts/0" + else + _apache_vhost_dir="/usr/local/apache2/conf/vhosts" + fi +elif [[ -d "/etc/apache2/sites-enabled" ]]; then + _apache_vhost_dir="/etc/apache2/sites-enabled" +fi + +clear +echo "" +echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .." +echo "" + + + + +echo "" +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo "Insert Apache VHost directory.." +echo "" +echo "" +apache_vhost_dir= +if [ -z "$_apache_vhost_dir" ]; then + echononl "Apache VHost directory: " + read apache_vhost_dir + while [[ "X$apache_vhost_dir" = "X" ]] ; do + echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n" + echononl "Apache VHost directory: " + read apache_vhost_dir + done +else + echononl "Apache VHost directory [${_apache_vhost_dir}]: " + read apache_vhost_dir + if [[ "X$apache_vhost_dir" = "X" ]] ; then + apache_vhost_dir="$_apache_vhost_dir" + fi +fi + +index_subdir_apache_vhost_dir="$(expr ${#apache_vhost_dir} + 1)" + +if [[ "$(basename $apache_vhost_dir)" = 'sites-enabled' ]]; then + apache_vhost_base_dir="$(dirname $apache_vhost_dir)/sites-available" +elif [[ "$(basename $apache_vhost_dir)" =~ vhost ]]; then + apache_vhost_base_dir="$apache_vhost_dir" +elif [[ "$(basename $(dirname $apache_vhost_dir))" =~ vhost ]]; then + apache_vhost_base_dir="$(dirname $apache_vhost_dir)" +fi + + +declare -i _num=1 +while IFS='' read -r -d '' fq_name ; do + dirname="$(basename "$fq_name")" + [[ "$dirname" = "DELETED" ]] && continue + [[ "$dirname" = "BAK" ]] && continue + [[ "$dirname" = "MAINTENANCE" ]] && continue + [[ "$dirname" =~ ^Moved ]] && continue + if [[ $_num -eq 1 ]]; then + _apache_additional_vhost_dirs="$(basename "$dirname")" + else + _apache_additional_vhost_dirs="$_apache_additional_vhost_dirs $(basename "$dirname")" + fi + ((_num++)) +done < <(find $apache_vhost_dir -mindepth 1 -maxdepth 1 -type d -print0) + + +echo "" +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo "If there are additional apache vhost directories, enter them here .." +echo "" +echo -e "\033[33m\033[1mNotice\033[m:" +echo -e " only first level subdiirectories of \033[1m$apache_vhost_dir\033[m" +echo -e " are posible values." +echo "" +echo "Blank seperated list of directories" +echo "" +echo -e "Type:" +echo -e " \033[33m\033[1mNone\033[m\033[15Gfor no further vhost directoties" +echo -e " \033[33m\033[1m\033[m\033[15Gto accept the default inside the square brackets" +echo "" +apache_additional_vhost_dirs= +if [ -z "$_apache_additional_vhost_dirs" ]; then + echononl "Additional Apache VHost directory [None]: " + read apache_additional_vhost_dirs + if [[ -z "trim($apache_additional_vhost_dirs)" ]] ; then + apache_additional_vhost_dirs="None" + elif [[ "${apache_additional_vhost_dirs,,}" = 'none' ]] ; then + apache_additional_vhost_dirs="None" + fi +else + echononl "Additional Apache VHost directories [${_apache_additional_vhost_dirs}]: " + read apache_additional_vhost_dirs + if [[ "${apache_additional_vhost_dirs,,}" = 'none' ]] ; then + apache_additional_vhost_dirs="None" + elif [[ -z "$(trim $apache_additional_vhost_dirs)" ]] ; then + apache_additional_vhost_dirs="$_apache_additional_vhost_dirs" + fi +fi + +if [[ -n "$apache_additional_vhost_dirs" ]] && [[ "$apache_additional_vhost_dirs" != "None" ]]; then + declare -a apache_additional_vhost_dir_arr=() + for _dir in $apache_additional_vhost_dirs ; do + if [[ -d "${apache_vhost_dir}/$_dir" ]] ; then + apache_additional_vhost_dir_arr+=("$_dir") + else + warn "$_dir is not a subdirectory of ${apache_vhost_dir}.\n\n \033[33m\033[1mIgnoring directory $_dir\033[m.." + fi + done +fi + + +echo "" +echo "apache_additional_vhost_dirs: $apache_additional_vhost_dirs" +echo "" + +echo "" +echo -e " Apache Vhosts Directory................: \033[1m$apache_vhost_dir\033[m" +if [[ ${#apache_additional_vhost_dir_arr[@]} -gt 0 ]] ; then + declare -i counter=1 + echo -en " Additional VHost Directories...........:\033[1m" + for _dir in ${apache_additional_vhost_dir_arr[@]} ; do + if [[ $counter -eq 1 ]] ; then + echo -e " \033[1m$_dir\033[m" + else + echo -e " \033[1m$_dir\033[m" + fi + counter=$((counter+1)) + done + echo "" +fi + +echo "" +echo "" +echo -e -n "\033[1mContinue with above settings? [y/n]:\033[m " +read OK +while [[ "X${OK}X" = "XX" ]] ; do + echo "" + echo -e -n "\033[1mContinue with above settings? [y/n]:\033[m " + read OK +done + +if [[ "${OK,,}" != 'yes' ]] && [[ "${OK,,}" != 'y' ]]; then + fatal "Abort by user request." +else + echo "" +fi + + +echononl " Backup existing VHost Configuration directory" +if [[ -d "$apache_vhost_base_dir" ]]; then + + cp -a "$apache_vhost_base_dir" "${apache_vhost_base_dir}.$backup_date" > $tmp_log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $tmp_log_file)" + fi +else + echo_skipped +fi + + +echo "" + +echononl " Create vhost file array.." +for _pgp_fpm_file in $(ls ${apache_vhost_dir}/*.php-fpm) ; do + vhost_php_fpm_file_arr+=("${_pgp_fpm_file}") +done + +if [[ ${#apache_additional_vhost_dir_arr[@]} -gt 0 ]] ; then + for _dir in ${apache_additional_vhost_dir_arr[@]} ; do + for _pgp_fpm_file in $(ls ${apache_vhost_dir}/${_dir}/*.php-fpm) ; do + vhost_php_fpm_file_arr+=("${_pgp_fpm_file}") + done + done +fi + +if [[ ${#vhost_php_fpm_file_arr[@]} -gt 0 ]] ; then + echo_ok +else + echo_skipped + warn "No vhost configuration file found!" + clean_up 1 +fi + + +_found_unchanged_part_follows_pipe=false +echononl " Replace SetHandler part that follows the pipe.." +for vhost_php_fpm_file in ${vhost_php_fpm_file_arr[@]} ; do + $(grep -q -E "^\s*SetHandler\s+.*fcgi://php/" "${vhost_php_fpm_file}" 2> /dev/null) && continue + _found_unchanged_part_follows_pipe=true + apache_needs_restart=true + perl -i -n -p -e "s#fcgi://127.0.0.1#fcgi://php/#" ${vhost_php_fpm_file} +done +if $_found_unchanged_part_follows_pipe ; then + echo_ok +else + echo_skipped +fi + + +echo "" + +regex_file_match_end="[[:space:]]*" + +if [[ ${#vhost_php_fpm_file_arr[@]} -gt 0 ]] ; then + for vhost_php_fpm_file in ${vhost_php_fpm_file_arr[@]} ; do + + echononl " Change vhost file \033[1m${vhost_php_fpm_file}\033[m" + + if $(grep -q -E "^\s*" "${vhost_php_fpm_file}" 2> /dev/null) ; then + echo_skipped + continue + fi + + apache_needs_restart=true + + mv "${vhost_php_fpm_file}" "/tmp" + + > "${vhost_php_fpm_file}" + + + _found=false + while IFS= read -r line || [[ -n "$line" ]]; do + + echo "${line}" >> "${vhost_php_fpm_file}" + + if ! $_found && [[ $line =~ $regex_file_match_end ]] ; then + cat <> "${vhost_php_fpm_file}" + + # Define a matching worker. + # The part that is matched to the SetHandler is the part that + # follows the pipe. If you need to distinguish, "localhost; can + # be anything unique. + # + + + # Recycle connections to the fastcgi dispatcher (PHP FPM). + # + # Use persistent connections to reduce the constant overhead of setting + # up new connections + # + ProxySet enablereuse=on + + # max - the most proxied request per server + # + # max = pm.max_children / max number of servers + # = pm.max_children / (MaxRequestWorkers / ThreadsPerChild) + # + ProxySet max=16 + + # Forces the module to flush every chunk of data received from the FCGI backend + # as soon as it receives it, without buffering. + # + ProxySet flushpackets=on + + # connectiontimeout + # + # Connect timeout in seconds. The number of seconds Apache httpd waits for the + # creation of a connection to the backend to complete. By adding a postfix of ms, + # the timeout can be also set in milliseconds. + # + ProxySet connectiontimeout=5 + + # timeout + # + # Socket timeout in seconds. The number of seconds Apache httpd waits for data + # sent by / to the backend. + # + #ProxySet timeout=${PROXY_TIMEOUT} + +fi + + +EOF + _found=true + fi + + done <"/tmp/$(basename "${vhost_php_fpm_file}")" + + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $tmp_log_file)" + fi + + done +fi + +if $apache_needs_restart ; then + + echo "" + echo -e "\033[32m------\033[m" + echo -e "Restart Apache Webservice" + echo -e "\033[32m------\033[m" + + OK="" + echo "" + echo "Type 'y' to continue 'n' or any other key to skip removal of Domains" + echo + echo -e -n "\033[1mRestart Apache Webservice [y/n]:\033[m " + read OK + while [[ "X${OK}X" = "XX" ]] ; do + echo "" + echo -e -n "\033[1mRestart Apache Webservice [y/n]:\033[m " + read OK + done + + if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then + + echo "" + echononl " Graceful restart Apache Webservice.." + apachectl graceful + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + error "$(cat $tmp_log_file)" + echo "Error: $(cat $tmp_log_file)" >> $main_log + fi + + fi # if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then + +fi # if $apache_needs_restart + + +echo "" +clean_up 0