Sh3ll



Directory :  /scripts2/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

Current File : //scripts2/adjust_fpm_domain.sh
#!/bin/bash
# This script adjusts one domain in the server to use its correct PHP-FPM resources.

# Config file routes
TABLE_FILE="/etc/sitioshispanos/match_fpm_conf"

# Routes and file extensions for file handling
TEMPLATE_PREFIX="/etc/sitioshispanos/fpm_templates"

CPANEL_PREFIX="/var/cpanel/userdata"
CPANEL_POSTFIX="php-fpm.yaml"

# Best in memory
mapfile -t PACK_TABLE < "$TABLE_FILE"

# Expected LiteSpeed service name
LITESPEED_SERV="litespeed"

# LiteSpeed servers don't use PHP-FPM
if pidof "$LITESPEED_SERV" > /dev/null; then
  echo "This is a LiteSpeed server, ignored"
  exit 0
fi

# Input domain
DOMAIN=$1

# Optionally override package settings
CUSTOM_FPM=$2

if [ ! "$DOMAIN" ]; then
  echo "Please enter a domain."
  exit 1
fi

# Try to match to an account
MATCHED_ACCOUNT=$(/usr/sbin/whmapi1 --output=json get_domain_info | jq -r --arg DOMAIN "$DOMAIN" '.data.domains[] | select( .domain == $DOMAIN) | .user')

if [ ! "$MATCHED_ACCOUNT" ]; then
  echo "This domain does not seem to correspond to any accounts."
  exit 2
fi

# Use custom plan, otherwise the one for account
if [ "$CUSTOM_FPM" ]; then  
  MATCHED_PLAN=$CUSTOM_FPM
else
  MATCHED_PLAN=$(/usr/sbin/whmapi1 --output=json accountsummary user="$MATCHED_ACCOUNT" | jq -r '.data.acct[].plan')
fi

# Template name of the plan to write to this account.
write_plan=""

for pack_line in "${PACK_TABLE[@]}"; do
  # If the name of the account's pack is in columns 2nd and further...
  if echo $pack_line | awk '{$1=""; print $0}' | grep -qw "$MATCHED_PLAN"; then
         
    #Then the template to write is the first column of the current line
    write_plan=$(echo $pack_line | awk '{print $1}')
    break

  # Otherwise no template
  else
    write_plan=""
  fi
done

if [ "$write_plan" ]; then
  # Variables for template and destination config file
  fpm_template="$TEMPLATE_PREFIX/$write_plan"
  fpm_yaml="$CPANEL_PREFIX/$MATCHED_ACCOUNT/$DOMAIN.$CPANEL_POSTFIX"

  echo -n "Template copy: "
  cp -v "$fpm_template" "$fpm_yaml"

# If no match for default plan of override plan
else
  echo "Could not find matching FPM template for package name: $MATCHED_PLAN"
  exit 3
fi

# Recompile all PHP-FPM domains
echo -e "\n** /scripts/php_fpm_config --check **"

if /scripts/php_fpm_config --check; then
  
  echo -e "\n** /scripts/php_fpm_config --rebuild **"
  /scripts/php_fpm_config --rebuild

else
  echo "Not rebuilding, check errors in output of check command above"
fi

Sh3LL