File: //bigscoots/wpo/phpfpm/create_pools.sh
#!/bin/bash
serverip=$(ip route get 1 | awk '{print $NF;exit}')
# Function to send Slack alert
send_slack_alert() {
local message="$1"
bash /bigscoots/general/slack.sh "#team-chat" ":warning: *WPO PHP-FPM*\n*Hostname:* $(hostname)\n *Server IP:* ${serverip}\n *Message:* ${message}"
}
[ -f /etc/centminmod/php.d/bsdomains.ini ] && echo > /etc/centminmod/php.d/bsdomains.ini
for ITEM in /home/nginx/domains/*; do
if [[ -d "$ITEM" && "${ITEM##*/}" != .* && "${ITEM##*/}" == *.* ]]; then
DOMAIN=$(basename "$ITEM")
# Remove existing configuration files
phpfpmdconf="/usr/local/nginx/conf/phpfpmd/$DOMAIN.conf"
domainsslconf="/usr/local/nginx/conf/conf.d/"$DOMAIN".ssl.conf"
domainwpsecureconf="/usr/local/nginx/conf/wpincludes/"$DOMAIN"/wpsecure_"$DOMAIN".conf"
[ -f "$phpfpmdconf" ] && rm -f "$phpfpmdconf"
[ -f "$domainsslconf" ] && sed -i "s=/usr/local/nginx/conf/php-wpsc-$DOMAIN.conf=/usr/local/nginx/conf/php-wpsc.conf=g" "$domainsslconf"
[ -f "$domainwpsecureconf" ] && sed -i "s=/usr/local/nginx/conf/php-wpsc-$DOMAIN.conf=/usr/local/nginx/conf/php-wpsc.conf=g" "$domainwpsecureconf"
# Create temporary directories and set permissions
mkdir -p /home/nginx/domains/"$DOMAIN"/tmp/opcache
chown -R nginx: /home/nginx/domains/"$DOMAIN"/tmp
# Initialize or unset the open_basedir variable
unset open_basedir
# Check if the options file exists and source it
OPTIONS_FILE="/root/.bigscoots/domains/$DOMAIN/bsdomainsini.options"
if [ -f "$OPTIONS_FILE" ]; then
source "$OPTIONS_FILE"
fi
# Append options to the bsdomains.ini file
touch /etc/centminmod/php.d/bsdomains.ini
cat <<EOT >> /etc/centminmod/php.d/bsdomains.ini
; $DOMAIN start
[HOST=$DOMAIN]
open_basedir = /tmp:/home/nginx/.bigscoots/cf:/home/nginx/domains/$DOMAIN:/home/fastcgicache_wp:/opt/datadog${open_basedir}
error_log = /home/nginx/domains/$DOMAIN/log/php_error.log
mail.log = /home/nginx/domains/$DOMAIN/log/php_mail.log
upload_tmp_dir = /home/nginx/domains/$DOMAIN/tmp
session.save_path = /home/nginx/domains/$DOMAIN/tmp
sys_temp_dir = /home/nginx/domains/$DOMAIN/tmp
soap.wsdl_cache_dir = /home/nginx/domains/$DOMAIN/tmp
; $DOMAIN end
EOT
fi
done
# Check if a file exists and contains a specific line
check_file() {
local file="$1"
local line="$2"
if [[ -f "$file" ]]; then
if grep -Fxq "$line" "$file"; then
return 0 # File exists and contains the line
else
return 1 # File exists but does not contain the line
fi
fi
}
# Function to test PHP-FPM configuration and restart the appropriate daemon
test_php_fpm_config() {
local daemon="$1"
local command="$2"
local error_message="$3"
if ! "$command" -t >/dev/null 2>&1
then
send_slack_alert "PHP-FPM config failed, please check $error_message"
return 1
fi
}
# Associative array mapping PHP version to conf_file, daemon_name, and executable_file
declare -A php_fpm_info=(
["php00.conf_file"]="/usr/local/etc/php-fpm.conf"
["php00.daemon_name"]="php-fpm"
["php00.executable_file"]="/usr/local/sbin/php-fpm"
["php56.conf_file"]="/etc/opt/remi/php56/php-fpm.d/www.conf"
["php56.daemon_name"]="php56-php-fpm"
["php56.executable_file"]="/opt/remi/php56/root/usr/sbin/php-fpm"
["php70.conf_file"]="/etc/opt/remi/php70/php-fpm.d/www.conf"
["php70.daemon_name"]="php70-php-fpm"
["php70.executable_file"]="/opt/remi/php70/root/usr/sbin/php-fpm"
["php71.conf_file"]="/etc/opt/remi/php71/php-fpm.d/www.conf"
["php71.daemon_name"]="php71-php-fpm"
["php71.executable_file"]="/opt/remi/php71/root/usr/sbin/php-fpm"
["php72.conf_file"]="/etc/opt/remi/php72/php-fpm.d/www.conf"
["php72.daemon_name"]="php72-php-fpm"
["php72.executable_file"]="/opt/remi/php72/root/usr/sbin/php-fpm"
["php73.conf_file"]="/etc/opt/remi/php73/php-fpm.d/www.conf"
["php73.daemon_name"]="php73-php-fpm"
["php73.executable_file"]="/opt/remi/php73/root/usr/sbin/php-fpm"
["php74.conf_file"]="/etc/opt/remi/php74/php-fpm.d/www.conf"
["php74.daemon_name"]="php74-php-fpm"
["php74.executable_file"]="/opt/remi/php74/root/usr/sbin/php-fpm"
["php80.conf_file"]="/etc/opt/remi/php80/php-fpm.d/www.conf"
["php80.daemon_name"]="php80-php-fpm"
["php80.executable_file"]="/opt/remi/php80/root/usr/sbin/php-fpm"
["php81.conf_file"]="/etc/opt/remi/php81/php-fpm.d/www.conf"
["php81.daemon_name"]="php81-php-fpm"
["php81.executable_file"]="/opt/remi/php81/root/usr/sbin/php-fpm"
["php82.conf_file"]="/etc/opt/remi/php82/php-fpm.d/www.conf"
["php82.daemon_name"]="php82-php-fpm"
["php82.executable_file"]="/opt/remi/php82/root/usr/sbin/php-fpm"
["php83.conf_file"]="/etc/opt/remi/php83/php-fpm.d/www.conf"
["php83.daemon_name"]="php83-php-fpm"
["php83.executable_file"]="/opt/remi/php83/root/usr/sbin/php-fpm"
["php84.conf_file"]="/etc/opt/remi/php84/php-fpm.d/www.conf"
["php84.daemon_name"]="php84-php-fpm"
["php84.executable_file"]="/opt/remi/php84/root/usr/sbin/php-fpm"
["php85.conf_file"]="/etc/opt/remi/php85/php-fpm.d/www.conf"
["php85.daemon_name"]="php85-php-fpm"
["php85.executable_file"]="/opt/remi/php85/root/usr/sbin/php-fpm"
)
# Define the path to the custom config file
CUSTOM_CONFIG_FILE="/root/.bigscoots/php/www.conf.custom"
# Default disable_functions value (fallback in case the custom file is missing or empty)
DEFAULT_DISABLE_FUNCTIONS="exec,system,passthru,shell_exec,proc_open,proc_close,popen,show_source,cmd"
# Read the custom disable_functions value if the file exists
if [[ -f "$CUSTOM_CONFIG_FILE" ]]; then
CUSTOM_DISABLE_FUNCTIONS=$(grep -E '^disable_functions=' "$CUSTOM_CONFIG_FILE" | cut -d'=' -f2 | tr -d ' ')
# If the file exists but is empty, fallback to default
if [[ -z "$CUSTOM_DISABLE_FUNCTIONS" ]]; then
CUSTOM_DISABLE_FUNCTIONS=$DEFAULT_DISABLE_FUNCTIONS
fi
else
CUSTOM_DISABLE_FUNCTIONS=$DEFAULT_DISABLE_FUNCTIONS
fi
# Generate the updated lines_to_check array
lines_to_check=(
"php_admin_value[disable_functions] = \"$CUSTOM_DISABLE_FUNCTIONS\" # Do not modify this line # 1684243876"
"pm.status_listen = 2 #php8only"
)
# Iterate over the PHP versions in php_fpm_info
for version in "${!php_fpm_info[@]}"
do
if [[ "$version" =~ ^php[0-9]+\.conf_file$ ]]
then
conf_file="${php_fpm_info[$version]}"
version_key="${version/.conf_file/}"
daemon_name="${php_fpm_info[${version_key}.daemon_name]}"
executable_file="${php_fpm_info[${version_key}.executable_file]}"
php_version="${version_key#php}"
# Debugging
# echo $conf_file
# echo $version_key
# echo $daemon_name
# echo $executable_file
# echo $php_version
# Iterate over the lines to check
for line_to_check in "${lines_to_check[@]}"
do
if ! check_file "$conf_file" "$line_to_check"
then
if [ -f "$conf_file" ] && [[ "$line_to_check" == *"php8only"* && "$php_version" == 8* ]]
then
# Delete existing lines that match $line_to_check
sed -i "/^$(sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$line_to_check").*/d" "$conf_file"
# Append new line at the end
sed -i "\$a$line_to_check" "$conf_file"
elif [ -f "$conf_file" ] && [[ ! "$line_to_check" == *"php8only"* ]]
then
# Delete existing lines that match $line_to_check
sed -i "/^$(sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$line_to_check").*/d" "$conf_file"
# Append new line at the end
sed -i "\$a$line_to_check" "$conf_file"
else
continue
fi
# Restart the appropriate daemon
if test_php_fpm_config "$daemon_name" "$executable_file" "PHP-FPM configuration for $daemon_name"
then
systemctl restart "$daemon_name"
else
continue
fi
fi
done
fi
done
# Create seperate PHP pools per DOMAIN
# Check to see if weve already created pools before so we know where to start off port wise else well start with port 9006
#if [ -f /root/.bigscoots/php/fpmpoolcounter ] ; then
# fpmport=$(($(cat /root/.bigscoots/php/fpmpoolcounter)+1))
#else
# fpmport=9010
#fi
# Creating configs per DOMAIN and incrementing port each DOMAIN that doesnt already have a pool.
#for DOMAIN in $(ls -1 /home/nginx/domains/); do
# if [ ! -f /usr/local/nginx/conf/phpfpmd/"$DOMAIN".conf ]; then
# fpmport=$((fpmport+1))
# cp -rf /bigscoots/wpo/phpfpm/sample_pool.confNOUSE /usr/local/nginx/conf/phpfpmd/"$DOMAIN".conf
# cp -rf /usr/local/nginx/conf/php-wpsc.conf /usr/local/nginx/conf/php-wpsc-"$DOMAIN".conf
# sed -i "s/listen = 127.0.0.1:9002/listen = 127.0.0.1:$fpmport/g" /usr/local/nginx/conf/phpfpmd/"$DOMAIN".conf # 1
# sed -i "s/samplepoolDOMAIN.com/$DOMAIN/g" /usr/local/nginx/conf/phpfpmd/"$DOMAIN".conf # 1
# sed -i 's/^fastcgi_pass dft_php;/#fastcgi_pass dft_php;/g' /usr/local/nginx/conf/php-wpsc-"$DOMAIN".conf # 2
# sed -i "s/fastcgi_pass 127.0.0.1:9000;/fastcgi_pass 127.0.0.1:$fpmport;/g ; s/#fastcgi_pass 127.0.0.1:$fpmport;/fastcgi_pass 127.0.0.1:$fpmport;/g" /usr/local/nginx/conf/php-wpsc-"$DOMAIN".conf # 2
# sed -i "s/#fastcgi_param PHP_ADMIN_VALUE open_basedir=\$document_root/fastcgi_param PHP_ADMIN_VALUE open_basedir=\/home\/nginx\/.bigscoots\/cf\/:\/home\/nginx\/domains\/$DOMAIN/g" /usr/local/nginx/conf/php-wpsc-"$DOMAIN".conf # 2
# sed -i 's/fastcgi_pass dft_php;/#fastcgi_pass dft_php;/g' /usr/local/nginx/conf/php-wpsc-"$DOMAIN".conf
# sed -i "s/\/usr\/local\/nginx\/conf\/php-wpsc.conf/\/usr\/local\/nginx\/conf\/php-wpsc-"$DOMAIN".conf/g" /usr/local/nginx/conf/conf.d/"$DOMAIN".ssl.conf
# echo $fpmport > /root/.bigscoots/php/fpmpoolcounter
# fi
#done