#!/usr/bin/env bash # Save this file as revert-apache.sh and run: bash revert-apache.sh HTTP_PORT=80 HTTPS_PORT=443 if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then echo "Usage: $0 [force]" echo "Disable xVarnish and revert to the standard Apache configuration." echo -e "\n force Do not prompt for confirmation if passed. (Optional)" exit 3 elif [ ! -f /var/cpanel/cpanel.config ]; then echo "$0 is for use with cPanel, which does not appear to be installed." exit 4 fi [[ $# -eq 1 && "$1" = "force" ]] && is_force=true || is_force=false if [ "$is_force" = false ]; then echo "Disable xVarnish and revert to the standard Apache configuration." echo echo "Steps performed:" echo " 1. Disable and kill non-critical xVarnish services" echo " 2. Remove Apache mod_xvarnish" echo " 3. Set HTTP and HTTPS to standard ports $HTTP_PORT, $HTTPS_PORT" echo " 4. Rebuild httpd.conf configuration with new ports" echo " 5. Stop Varnish Cache and Apache web services" echo " 6. Start Apache on ports $HTTP_PORT, $HTTPS_PORT" echo read -r -p "Do you wish to proceed? [y/N] " answer [[ ! "${answer,,}" =~ ^(yes|y)$ ]] && exit 6 || echo fi [ "$is_force" = true ] && echo "Starting $(basename "$0")" # quick functions for reuse el7_q() { type systemctl &>/dev/null; } ea4_q() { [ -f /etc/cpanel/ea4/is_ea4 ]; } disable() { el7_q && systemctl -q disable "$1" || /sbin/chkconfig "$1" on; } killproc() { pgrep -x "$1" >/dev/null && pgrep -x "$1" | xargs -r kill -9; } cp_set_option() { [[ $# -eq 2 && -n "$1" && -n "$2" ]] || return 3 grep -qF "${1}=" /var/cpanel/cpanel.config || return 5 sed -i "s/^${1}=.*/${1}=${2}/" /var/cpanel/cpanel.config } xv_set_option() { [[ $# -eq 2 && -n "$1" && -n "$2" ]] || return 3 [ -f /usr/local/xvarnish/settings ] || return 4 tmpfn=$(mktemp /usr/local/xvarnish/.settings.XXXXXX) sed "s/^${1} =.*/${1} = ${2}/" /usr/local/xvarnish/settings > "$tmpfn" if ! grep -qF "${1} = ${2}" "$tmpfn"; then echo "${1} = ${2}" >> "$tmpfn" fi /bin/mv -f "$tmpfn" /usr/local/xvarnish/settings } echo "[1] Disable and kill non-critical xVarnish services" xv_set_option enable_varnish off xv_set_option enable_healthcheck off for name in xvhealth xvstats xvbeat; do disable "$name" killproc "$name" done echo "[2] Remove Apache mod_xvarnish" modxv="/usr/local/apache/conf/conf.d/mod_xvarnish.conf" \ && ea4_q && modxv="/etc/apache2/conf.modules.d/999_mod_xvarnish.conf" [ -d "$(dirname "$modxv")" ] && : > "$modxv" sopath="/usr/local/apache/modules/mod_xvarnish.so" \ && ea4_q && sopath="/usr/lib64/apache2/modules/mod_xvarnish.so" [ -f "$sopath" ] && /bin/rm -f "$sopath" echo "[3] Set HTTP and HTTPS to standard ports $HTTP_PORT, $HTTPS_PORT" cp_set_option apache_port "0.0.0.0:$HTTP_PORT" cp_set_option apache_ssl_port "0.0.0.0:$HTTPS_PORT" xv_chkconf="/usr/local/xvarnish/etc/conf.d/default/httpd.chksrv80" chkconf="/usr/local/cpanel/src/chkservd/chkserv.d/httpd" [ -f "$xv_chkconf" ] && /bin/cp -f "$xv_chkconf" "$chkconf" echo "[4] Rebuild httpd.conf configuration with new ports" out=$(/scripts/rebuildhttpdconf 2>&1) [ $? -eq 0 ] || { echo "warning: /scripts/rebuildhttpdconf failed: ${out:-"(no output)"}" } echo "[5] Stop Varnish Cache and Apache web services" /usr/local/cpanel/libexec/tailwatchd --stop &>/dev/null disable varnish killproc varnishd killproc hitch killproc httpd killproc xvssl echo "[6] Start Apache on ports $HTTP_PORT, $HTTPS_PORT" el7_q && systemctl start httpd || service httpd start sleep 1 && /usr/local/cpanel/libexec/tailwatchd --start &>/dev/null echo -e "\nCompleted $(basename "$0")"