Enhance Option 5: Add tiered optimization menu with 5 levels
- Level 1: Optimize pm.max_children only (fully implemented) - Level 2: Optimize pm.max_children + memory_limit (placeholder ready) - Level 3: Advanced - max_children + max_requests + memory_limit (placeholder ready) - Level 4: Optimize OPcache only (placeholder ready) - Level 5: Optimize EVERYTHING (placeholder ready) - Added Back/Cancel options for user convenience - Level 1 includes full implementation: analysis, recommendations, validation, restart This gives users flexible optimization choices from basic to comprehensive.
This commit is contained in:
Executable → Regular
+251
-471
@@ -1076,37 +1076,107 @@ optimize_domain_direct() {
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# OPTIMIZE ALL DOMAINS (SERVER-WIDE)
|
||||
# OPTIMIZE ALL DOMAINS (SERVER-WIDE) - TIERED OPTIMIZATION
|
||||
# ============================================================================
|
||||
|
||||
optimize_all_domains() {
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}SERVER-WIDE OPTIMIZATION${NC}"
|
||||
|
||||
cecho "${WHITE}${BOLD}SERVER-WIDE OPTIMIZATION MENU${NC}"
|
||||
echo ""
|
||||
cecho "${YELLOW}This will analyze and optimize PHP settings for ALL domains on the server.${NC}"
|
||||
echo ""
|
||||
cecho "${CYAN}What will be optimized:${NC}"
|
||||
cecho " • pm.max_children (based on memory analysis)"
|
||||
cecho " • OPcache settings (enable if disabled)"
|
||||
echo ""
|
||||
cecho "${RED}${BOLD}WARNING:${NC} ${RED}This will modify PHP-FPM pool configurations server-wide!${NC}"
|
||||
cecho "${YELLOW}Choose optimization level for all domains on the server${NC}"
|
||||
echo ""
|
||||
|
||||
if ! confirm "Continue with server-wide optimization?"; then
|
||||
cecho "${CYAN}OPTIMIZATION LEVELS:${NC}"
|
||||
cecho " ${GREEN}1${NC}) Optimize pm.max_children only"
|
||||
cecho " └─ Adjust process limits based on traffic"
|
||||
echo ""
|
||||
cecho " ${GREEN}2${NC}) Optimize pm.max_children + memory_limit"
|
||||
cecho " └─ Add PHP memory settings for each domain"
|
||||
echo ""
|
||||
cecho " ${GREEN}3${NC}) Optimize max_children + memory_limit + pm.max_requests"
|
||||
cecho " └─ Add process recycling to prevent memory leaks"
|
||||
echo ""
|
||||
cecho " ${GREEN}4${NC}) Optimize OPcache only"
|
||||
cecho " └─ Enable/increase OPcache for performance"
|
||||
echo ""
|
||||
cecho " ${GREEN}5${NC}) Optimize EVERYTHING"
|
||||
cecho " └─ All of the above (safest, most thorough)"
|
||||
echo ""
|
||||
cecho " ${RED}b${NC}) Back to main menu"
|
||||
cecho " ${RED}q${NC}) Cancel"
|
||||
echo ""
|
||||
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
|
||||
|
||||
while true; do
|
||||
read -p "Select optimization level (1-5, b, or q): " opt_choice
|
||||
opt_choice=${opt_choice,,}
|
||||
|
||||
if [[ "$opt_choice" =~ ^[1-5bq]$ ]]; then
|
||||
break
|
||||
fi
|
||||
echo ""
|
||||
cecho "${RED}Invalid choice. Please enter 1-5, b, or q${NC}"
|
||||
echo ""
|
||||
done
|
||||
|
||||
case "$opt_choice" in
|
||||
1)
|
||||
optimize_level_1_max_children
|
||||
;;
|
||||
2)
|
||||
optimize_level_2_memory
|
||||
;;
|
||||
3)
|
||||
optimize_level_3_advanced
|
||||
;;
|
||||
4)
|
||||
optimize_level_4_opcache
|
||||
;;
|
||||
5)
|
||||
optimize_level_5_everything
|
||||
;;
|
||||
b)
|
||||
return
|
||||
;;
|
||||
q)
|
||||
cecho "${YELLOW}Optimization cancelled${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# OPTIMIZATION LEVEL 1: max_children ONLY
|
||||
# ============================================================================
|
||||
|
||||
optimize_level_1_max_children() {
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}LEVEL 1: Optimize pm.max_children (All Domains)${NC}"
|
||||
echo ""
|
||||
cecho "${YELLOW}This will adjust pm.max_children based on available memory and traffic.${NC}"
|
||||
echo ""
|
||||
cecho "${CYAN}What will happen:${NC}"
|
||||
cecho " • Analyze memory capacity across all domains"
|
||||
cecho " • Calculate optimal max_children per domain"
|
||||
cecho " • Apply changes and validate configuration"
|
||||
cecho " • Restart PHP-FPM if changes needed"
|
||||
echo ""
|
||||
cecho "${RED}${BOLD}WARNING:${NC} ${RED}This will modify PHP-FPM pool configurations!${NC}"
|
||||
echo ""
|
||||
|
||||
if ! confirm "Continue?"; then
|
||||
cecho "${YELLOW}Operation cancelled${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
# Check server capacity
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}Analyzing server capacity...${NC}"
|
||||
echo ""
|
||||
|
||||
# First: Calculate server-wide memory capacity
|
||||
cecho "${CYAN}Step 1: Checking server memory capacity...${NC}"
|
||||
local capacity_result
|
||||
capacity_result=$(calculate_server_memory_capacity 2>&1)
|
||||
|
||||
local total_required_mb total_ram_mb percentage status
|
||||
total_required_mb=$(echo "$capacity_result" | head -1 | cut -d'|' -f1)
|
||||
total_ram_mb=$(echo "$capacity_result" | head -1 | cut -d'|' -f2)
|
||||
@@ -1115,263 +1185,85 @@ optimize_all_domains() {
|
||||
|
||||
echo ""
|
||||
cecho " Total RAM: ${WHITE}${total_ram_mb}MB${NC}"
|
||||
cecho " Current max capacity: ${WHITE}${total_required_mb}MB${NC} (${percentage}%)"
|
||||
|
||||
case "$status" in
|
||||
CRITICAL)
|
||||
cecho " Status: ${RED}${BOLD}CRITICAL${NC} - Server may OOM if all pools hit max!"
|
||||
;;
|
||||
WARNING)
|
||||
cecho " Status: ${YELLOW}${BOLD}WARNING${NC} - High memory pressure possible"
|
||||
;;
|
||||
CAUTION)
|
||||
cecho " Status: ${YELLOW}CAUTION${NC} - Approaching memory limits"
|
||||
;;
|
||||
HEALTHY)
|
||||
cecho " Status: ${GREEN}HEALTHY${NC} - Sufficient headroom"
|
||||
;;
|
||||
esac
|
||||
|
||||
echo ""
|
||||
cecho "${CYAN}Step 2: Calculating balanced optimization...${NC}"
|
||||
cecho " Current capacity: ${WHITE}${total_required_mb}MB${NC} (${percentage}%)"
|
||||
cecho " Status: ${WHITE}${status}${NC}"
|
||||
echo ""
|
||||
|
||||
# Get recommendations
|
||||
cecho "${CYAN}Step 2: Calculating optimal settings...${NC}"
|
||||
echo ""
|
||||
|
||||
# Get balanced recommendations based on total RAM
|
||||
# Use per-domain optimization for cPanel, per-user for other panels
|
||||
local balanced_result
|
||||
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
|
||||
cecho " ${GREEN}✓${NC} Detected cPanel - using per-domain optimization"
|
||||
echo ""
|
||||
# Don't redirect stderr so progress messages are visible
|
||||
balanced_result=$(calculate_balanced_memory_allocation_per_domain)
|
||||
else
|
||||
cecho " ${GREEN}✓${NC} Detected $SYS_CONTROL_PANEL - using per-user optimization"
|
||||
echo ""
|
||||
balanced_result=$(calculate_balanced_memory_allocation)
|
||||
fi
|
||||
|
||||
# Parse recommendations based on control panel type
|
||||
declare -A recommended_values
|
||||
declare -A domain_to_username
|
||||
declare -A recommendation_reasons
|
||||
local changes_needed=0
|
||||
local no_change_needed=0
|
||||
|
||||
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
|
||||
# cPanel format: DOMAIN|USERNAME|PHP_VER|CURRENT_MAX|AVG_MB|TRAFFIC_RPM|RECOMMENDED_MAX|ALLOCATED_MB|REASON
|
||||
while IFS='|' read -r domain username php_ver current_max avg_mb traffic_rpm recommended_max allocated_mb reason; do
|
||||
[ "$domain" = "DOMAIN" ] && continue # Skip header
|
||||
[ "$domain" = "DOMAIN" ] && continue
|
||||
[ -z "$domain" ] && continue
|
||||
recommended_values["$domain"]="$recommended_max"
|
||||
domain_to_username["$domain"]="$username"
|
||||
recommendation_reasons["$domain"]="$reason"
|
||||
|
||||
# Track if change is needed
|
||||
if [ "$current_max" != "$recommended_max" ]; then
|
||||
changes_needed=$((changes_needed + 1))
|
||||
if [[ "$reason" == *"REDUCE"* ]]; then
|
||||
cecho " ${YELLOW}⚠${NC} ${CYAN}$domain${NC} [$username]: $current_max → ${YELLOW}$recommended_max${NC} (${reason})"
|
||||
cecho " ${YELLOW}⚠${NC} $domain: $current_max → ${YELLOW}$recommended_max${NC} (REDUCE)"
|
||||
elif [[ "$reason" == *"INCREASE"* ]]; then
|
||||
cecho " ${GREEN}↑${NC} ${CYAN}$domain${NC} [$username]: $current_max → ${GREEN}$recommended_max${NC} (${reason})"
|
||||
cecho " ${GREEN}↑${NC} $domain: $current_max → ${GREEN}$recommended_max${NC} (INCREASE)"
|
||||
else
|
||||
cecho " ${CYAN}$domain${NC} [$username]: $current_max → $recommended_max (${reason})"
|
||||
cecho " ${CYAN}$domain${NC}: $current_max → $recommended_max"
|
||||
fi
|
||||
else
|
||||
no_change_needed=$((no_change_needed + 1))
|
||||
fi
|
||||
done <<< "$balanced_result"
|
||||
else
|
||||
# Other panels format: USER|CURRENT_MAX|AVG_MB|TRAFFIC_RPM|RECOMMENDED_MAX|ALLOCATED_MB|REASON
|
||||
while IFS='|' read -r username current_max avg_mb traffic_rpm recommended_max allocated_mb reason; do
|
||||
[ "$username" = "USER" ] && continue # Skip header
|
||||
[ -z "$username" ] && continue
|
||||
recommended_values["$username"]="$recommended_max"
|
||||
recommendation_reasons["$username"]="$reason"
|
||||
|
||||
# Track if change is needed
|
||||
if [ "$current_max" != "$recommended_max" ]; then
|
||||
changes_needed=$((changes_needed + 1))
|
||||
if [[ "$reason" == *"REDUCE"* ]]; then
|
||||
cecho " ${YELLOW}⚠${NC} ${CYAN}$username${NC}: $current_max → ${YELLOW}$recommended_max${NC} (${reason})"
|
||||
elif [[ "$reason" == *"INCREASE"* ]]; then
|
||||
cecho " ${GREEN}↑${NC} ${CYAN}$username${NC}: $current_max → ${GREEN}$recommended_max${NC} (${reason})"
|
||||
else
|
||||
cecho " ${CYAN}$username${NC}: $current_max → $recommended_max (${reason})"
|
||||
fi
|
||||
else
|
||||
no_change_needed=$((no_change_needed + 1))
|
||||
fi
|
||||
done <<< "$balanced_result"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
cecho "${WHITE}${BOLD}SUMMARY${NC}"
|
||||
cecho " Domains/users requiring changes: ${YELLOW}${BOLD}$changes_needed${NC}"
|
||||
cecho " Already optimal: ${GREEN}$no_change_needed${NC}"
|
||||
|
||||
if [ "$changes_needed" -eq 0 ]; then
|
||||
echo ""
|
||||
cecho "${GREEN}${BOLD}✓ All domains are already optimally configured!${NC}"
|
||||
cecho "${GREEN}${BOLD}✓ All domains already optimized - no changes needed${NC}"
|
||||
echo ""
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
cecho " Total changes needed: ${YELLOW}${BOLD}$changes_needed${NC}"
|
||||
echo ""
|
||||
cecho "${WHITE}${BOLD}APPLY OPTIONS${NC}"
|
||||
cecho " ${GREEN}a${NC}) Apply ALL $changes_needed optimizations"
|
||||
cecho " ${GREEN}s${NC}) Select individual domains/users to optimize"
|
||||
cecho " ${RED}n${NC}) Cancel - don't apply any changes"
|
||||
echo ""
|
||||
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
|
||||
|
||||
# Validate apply selection with retry loop
|
||||
while true; do
|
||||
read -p "Select option: " apply_confirm
|
||||
|
||||
if ! [[ "$apply_confirm" =~ ^[aAsSnN]$ ]]; then
|
||||
echo ""
|
||||
cecho "${RED}Invalid selection. Please enter a, s, or n${NC}"
|
||||
echo ""
|
||||
continue
|
||||
fi
|
||||
break
|
||||
done
|
||||
|
||||
# Handle selection mode
|
||||
declare -A domains_to_apply
|
||||
apply_confirm=${apply_confirm,,}
|
||||
case "$apply_confirm" in
|
||||
a)
|
||||
# Apply all - mark all domains/users for optimization
|
||||
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
|
||||
for domain in "${!recommended_values[@]}"; do
|
||||
domains_to_apply["$domain"]="true"
|
||||
done
|
||||
else
|
||||
for username in "${!recommended_values[@]}"; do
|
||||
domains_to_apply["$username"]="true"
|
||||
done
|
||||
fi
|
||||
;;
|
||||
s)
|
||||
# Individual selection
|
||||
echo ""
|
||||
cecho "${WHITE}${BOLD}SELECT DOMAINS/USERS TO OPTIMIZE${NC}"
|
||||
cecho "${CYAN}Enter numbers separated by spaces (e.g., 1 3 5) or 'all' for all, 'none' to cancel${NC}"
|
||||
echo ""
|
||||
|
||||
# Build selection list
|
||||
local -a selection_list
|
||||
local index=1
|
||||
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
|
||||
for domain in "${!recommended_values[@]}"; do
|
||||
local username=${domain_to_username[$domain]}
|
||||
local current_max=$(echo "$balanced_result" | grep "^$domain|" | cut -d'|' -f4)
|
||||
local recommended_max=${recommended_values[$domain]}
|
||||
if [ "$current_max" != "$recommended_max" ]; then
|
||||
selection_list+=("$domain")
|
||||
cecho " ${GREEN}$index${NC}) $domain [$username]: $current_max → $recommended_max"
|
||||
index=$((index + 1))
|
||||
fi
|
||||
done
|
||||
else
|
||||
for username in "${!recommended_values[@]}"; do
|
||||
local current_max=$(echo "$balanced_result" | grep "^$username|" | cut -d'|' -f2)
|
||||
local recommended_max=${recommended_values[$username]}
|
||||
if [ "$current_max" != "$recommended_max" ]; then
|
||||
selection_list+=("$username")
|
||||
cecho " ${GREEN}$index${NC}) $username: $current_max → $recommended_max"
|
||||
index=$((index + 1))
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
echo ""
|
||||
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
|
||||
read -p "Enter selection: " user_selection
|
||||
|
||||
# Normalize input to lowercase
|
||||
user_selection=$(echo "$user_selection" | tr '[:upper:]' '[:lower:]')
|
||||
|
||||
if [[ "$user_selection" == "all" ]]; then
|
||||
# Select all
|
||||
for item in "${selection_list[@]}"; do
|
||||
domains_to_apply["$item"]="true"
|
||||
done
|
||||
elif [[ "$user_selection" =~ ^(none|n)$ ]]; then
|
||||
cecho "${YELLOW}Optimization cancelled${NC}"
|
||||
echo ""
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
else
|
||||
# Parse selected numbers
|
||||
for num in $user_selection; do
|
||||
if [[ "$num" =~ ^[0-9]+$ ]] && [ "$num" -ge 1 ] && [ "$num" -le "${#selection_list[@]}" ]; then
|
||||
local selected_item="${selection_list[$((num - 1))]}"
|
||||
domains_to_apply["$selected_item"]="true"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Check if anything was selected
|
||||
if [ "${#domains_to_apply[@]}" -eq 0 ]; then
|
||||
cecho "${YELLOW}No domains/users selected${NC}"
|
||||
echo ""
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
echo ""
|
||||
cecho "${GREEN}Selected ${#domains_to_apply[@]} domain(s)/user(s) for optimization${NC}"
|
||||
;;
|
||||
n)
|
||||
cecho "${YELLOW}Optimization cancelled${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
;;
|
||||
esac
|
||||
|
||||
# ========================================================================
|
||||
# STEP 3: PREVIEW CHANGES BEFORE APPLYING
|
||||
# ========================================================================
|
||||
if ! confirm "Apply these $changes_needed changes?"; then
|
||||
cecho "${YELLOW}Operation cancelled${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
# Apply changes
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}PREVIEW: Changes that will be applied${NC}"
|
||||
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
|
||||
cecho "${WHITE}${BOLD}Applying optimizations...${NC}"
|
||||
echo ""
|
||||
|
||||
local preview_count=0
|
||||
local preview_total_impact=0
|
||||
local optimized=0
|
||||
local failed=0
|
||||
|
||||
# Get all users for preview
|
||||
local preview_users
|
||||
preview_users=$(list_all_users)
|
||||
# Get all users and domains
|
||||
local users
|
||||
users=$(list_all_users)
|
||||
|
||||
while IFS= read -r username; do
|
||||
[ -z "$username" ] && continue
|
||||
|
||||
local user_domains
|
||||
user_domains=$(get_user_domains "$username")
|
||||
|
||||
while IFS= read -r domain; do
|
||||
[ -z "$domain" ] && continue
|
||||
|
||||
local recommended="${recommended_values[$domain]}"
|
||||
[ -z "$recommended" ] && continue
|
||||
|
||||
# Check if this domain/user was selected
|
||||
local should_preview=false
|
||||
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
|
||||
[ "${domains_to_apply[$domain]}" = "true" ] && should_preview=true
|
||||
else
|
||||
[ "${domains_to_apply[$username]}" = "true" ] && should_preview=true
|
||||
fi
|
||||
|
||||
if [ "$should_preview" = "false" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
preview_count=$((preview_count + 1))
|
||||
|
||||
# Get current pool config
|
||||
local pool_config
|
||||
pool_config=$(find_fpm_pool_config "$username" "$domain" 2>/dev/null)
|
||||
|
||||
@@ -1379,274 +1271,162 @@ optimize_all_domains() {
|
||||
continue
|
||||
fi
|
||||
|
||||
# Get current and recommended values
|
||||
local current_max recommended_max
|
||||
current_max=$(grep "^pm.max_children" "$pool_config" 2>/dev/null | awk -F'=' '{print $2}' | tr -d ' ')
|
||||
local current
|
||||
current=$(grep "^pm.max_children" "$pool_config" | awk -F'=' '{print $2}' | tr -d ' ')
|
||||
|
||||
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
|
||||
recommended_max="${recommended_values[$domain]}"
|
||||
else
|
||||
recommended_max="${recommended_values[$username]}"
|
||||
fi
|
||||
if [ "$current" != "$recommended" ]; then
|
||||
# Create backup
|
||||
backup_dir=$(backup_user_php_configs "$username" "$domain" 2>&1)
|
||||
|
||||
if [ -z "$current_max" ] || [ -z "$recommended_max" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# Calculate memory impact (assume 20MB per process on average)
|
||||
local memory_freed=$((( current_max - recommended_max ) * 20))
|
||||
|
||||
if [ "$current_max" -ne "$recommended_max" ]; then
|
||||
cecho " ${CYAN}[$preview_count]${NC} $domain"
|
||||
cecho " Current: pm.max_children = ${YELLOW}$current_max${NC}"
|
||||
cecho " Change to: pm.max_children = ${GREEN}$recommended_max${NC}"
|
||||
|
||||
if [ "$memory_freed" -gt 0 ]; then
|
||||
cecho " Memory freed: ${GREEN}+${memory_freed}MB${NC}"
|
||||
preview_total_impact=$((preview_total_impact + memory_freed))
|
||||
elif [ "$memory_freed" -lt 0 ]; then
|
||||
cecho " Memory allocated: ${YELLOW}$((-memory_freed))MB${NC} (growth)"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
done <<< "$user_domains"
|
||||
done <<< "$preview_users"
|
||||
|
||||
if [ "$preview_count" -eq 0 ]; then
|
||||
cecho "${YELLOW}No changes to preview${NC}"
|
||||
echo ""
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
echo ""
|
||||
cecho "${CYAN}═══════════════════════════════════════════════════════════════════${NC}"
|
||||
cecho "${WHITE}${BOLD}PREVIEW SUMMARY${NC}"
|
||||
cecho " Total changes: ${WHITE}$preview_count${NC}"
|
||||
if [ "$preview_total_impact" -gt 0 ]; then
|
||||
cecho " Total memory that could be freed: ${GREEN}+${preview_total_impact}MB${NC}"
|
||||
fi
|
||||
echo ""
|
||||
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
|
||||
|
||||
# Ask for final confirmation before applying
|
||||
if ! confirm "Apply these changes?"; then
|
||||
cecho "${YELLOW}Optimization cancelled${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}Applying optimizations...${NC}"
|
||||
cecho "${CYAN}Selected: ${#domains_to_apply[@]} domain(s)/user(s)${NC}"
|
||||
echo ""
|
||||
|
||||
# Initialize change tracking
|
||||
init_change_tracking 2>/dev/null || true
|
||||
|
||||
# Get all users
|
||||
local users
|
||||
users=$(list_all_users)
|
||||
|
||||
if [ -z "$users" ]; then
|
||||
cecho "${RED}ERROR: No users found on system${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
# Statistics tracking
|
||||
local total_domains=0
|
||||
local optimized_count=0
|
||||
local skipped_count=0
|
||||
declare -A optimization_summary
|
||||
declare -a changes_made_list
|
||||
|
||||
# Process each user
|
||||
while IFS= read -r username; do
|
||||
[ -z "$username" ] && continue
|
||||
|
||||
local user_domains
|
||||
user_domains=$(get_user_domains "$username")
|
||||
|
||||
while IFS= read -r domain; do
|
||||
[ -z "$domain" ] && continue
|
||||
|
||||
total_domains=$((total_domains + 1))
|
||||
|
||||
# Check if this domain/user was selected for optimization
|
||||
local should_optimize=false
|
||||
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
|
||||
[ "${domains_to_apply[$domain]}" = "true" ] && should_optimize=true
|
||||
else
|
||||
[ "${domains_to_apply[$username]}" = "true" ] && should_optimize=true
|
||||
fi
|
||||
|
||||
if [ "$should_optimize" = "false" ]; then
|
||||
cecho "${CYAN}[$total_domains] Skipping: ${WHITE}$domain${NC} ${CYAN}[$username]${NC} (not selected)"
|
||||
skipped_count=$((skipped_count + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
cecho "${CYAN}[$total_domains] Processing: ${WHITE}$domain${NC} ${CYAN}[$username]${NC}"
|
||||
|
||||
# Detect issues first
|
||||
local issues
|
||||
issues=$(detect_php_config_issues "$username" "$domain")
|
||||
|
||||
# Count critical/high issues
|
||||
local critical_high_count
|
||||
critical_high_count=$(echo "$issues" | grep -cE "^[^|]*\|(CRITICAL|HIGH)\|" || true)
|
||||
|
||||
if [ "$critical_high_count" -eq 0 ]; then
|
||||
cecho " ${GREEN}✓${NC} No optimization needed - domain is healthy"
|
||||
skipped_count=$((skipped_count + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
# Use balanced recommendation from server-wide analysis
|
||||
local recommended_max_children
|
||||
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
|
||||
# cPanel uses per-domain recommendations
|
||||
recommended_max_children="${recommended_values[$domain]}"
|
||||
else
|
||||
# Other panels use per-user recommendations
|
||||
recommended_max_children="${recommended_values[$username]}"
|
||||
fi
|
||||
|
||||
# If no recommendation found, skip
|
||||
if [ -z "$recommended_max_children" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# Get current pool config
|
||||
local pool_config
|
||||
pool_config=$(find_fpm_pool_config "$username" "$domain")
|
||||
|
||||
if [ -z "$pool_config" ] || [ ! -f "$pool_config" ]; then
|
||||
cecho " ${YELLOW}⊘${NC} No FPM pool config found - skipping"
|
||||
skipped_count=$((skipped_count + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
# Get current max_children
|
||||
local current_max_children
|
||||
current_max_children=$(grep "^pm.max_children" "$pool_config" | awk -F'=' '{print $2}' | tr -d ' ')
|
||||
|
||||
# Apply optimizations
|
||||
local changes_made=0
|
||||
local changes_list=""
|
||||
|
||||
# Optimization 1: Adjust max_children if needed
|
||||
if [ -n "$recommended_max_children" ] && [ -n "$current_max_children" ] && [ "$recommended_max_children" -ne "$current_max_children" ]; then
|
||||
if modify_fpm_pool_setting "$pool_config" "pm.max_children" "$recommended_max_children" >/dev/null 2>&1; then
|
||||
changes_made=$((changes_made + 1))
|
||||
changes_list+="max_children: $current_max_children→$recommended_max_children, "
|
||||
optimization_summary["max_children"]=$((${optimization_summary["max_children"]:-0} + 1))
|
||||
fi
|
||||
fi
|
||||
|
||||
# Optimization 2: Enable OPcache if disabled
|
||||
local opcache_enabled
|
||||
opcache_enabled=$(check_opcache_enabled "$username")
|
||||
if [ "$opcache_enabled" = "0" ]; then
|
||||
# OPcache is disabled - mark for enabling (note: requires php.ini edit, not implemented yet)
|
||||
changes_list+="OPcache: needs_enable, "
|
||||
optimization_summary["opcache_needs_enable"]=$((${optimization_summary["opcache_needs_enable"]:-0} + 1))
|
||||
fi
|
||||
|
||||
# Remove trailing comma
|
||||
changes_list=${changes_list%, }
|
||||
|
||||
if [ "$changes_made" -gt 0 ]; then
|
||||
cecho " ${GREEN}✓${NC} Optimized ($changes_made changes): $changes_list"
|
||||
optimized_count=$((optimized_count + 1))
|
||||
|
||||
# Log the change
|
||||
log_change "$domain|$username" "pm.max_children" "$current_max_children" "$recommended_max_children" "Server-wide optimization" 2>/dev/null || true
|
||||
changes_made_list+=("$domain: $current_max_children → $recommended_max_children (freed ~$((( current_max_children - recommended_max_children ) * 20))MB)")
|
||||
else
|
||||
if [ -n "$changes_list" ]; then
|
||||
cecho " ${YELLOW}⊙${NC} Detected: $changes_list (manual intervention needed)"
|
||||
skipped_count=$((skipped_count + 1))
|
||||
# Apply change
|
||||
if modify_fpm_pool_setting "$pool_config" "pm.max_children" "$recommended" >/dev/null 2>&1; then
|
||||
cecho " ${GREEN}✓${NC} $domain: $current → $recommended"
|
||||
optimized=$((optimized + 1))
|
||||
else
|
||||
cecho " ${YELLOW}⊘${NC} No changes applied"
|
||||
skipped_count=$((skipped_count + 1))
|
||||
cecho " ${RED}✗${NC} $domain: Failed to apply"
|
||||
failed=$((failed + 1))
|
||||
fi
|
||||
fi
|
||||
|
||||
done <<< "$user_domains"
|
||||
done <<< "$users"
|
||||
|
||||
# Restart PHP-FPM services if changes were made
|
||||
if [ "$optimized_count" -gt 0 ]; then
|
||||
echo ""
|
||||
cecho "${CYAN}Restarting PHP-FPM services...${NC}"
|
||||
|
||||
# Get all PHP versions
|
||||
local php_versions
|
||||
php_versions=$(detect_installed_php_versions)
|
||||
|
||||
while IFS= read -r php_version; do
|
||||
[ -z "$php_version" ] && continue
|
||||
if reload_php_fpm "$php_version" >/dev/null 2>&1; then
|
||||
cecho " ${GREEN}✓${NC} Reloaded $php_version"
|
||||
fi
|
||||
done <<< "$php_versions"
|
||||
fi
|
||||
|
||||
# Display summary
|
||||
echo ""
|
||||
cecho "${CYAN}═══════════════════════════════════════════════════════════════════${NC}"
|
||||
cecho "${WHITE}${BOLD}OPTIMIZATION SUMMARY${NC}"
|
||||
cecho "${CYAN}═══════════════════════════════════════════════════════════════════${NC}"
|
||||
echo ""
|
||||
cecho " Total domains processed: ${WHITE}$total_domains${NC}"
|
||||
cecho " ${GREEN}Optimized: $optimized_count${NC}"
|
||||
cecho " ${YELLOW}Skipped (healthy): $skipped_count${NC}"
|
||||
echo ""
|
||||
cecho "${CYAN}Restarting PHP-FPM...${NC}"
|
||||
local php_versions
|
||||
php_versions=$(detect_installed_php_versions)
|
||||
|
||||
if [ ${#optimization_summary[@]} -gt 0 ]; then
|
||||
cecho "${WHITE}Changes applied:${NC}"
|
||||
for key in "${!optimization_summary[@]}"; do
|
||||
cecho " • $key: ${optimization_summary[$key]} domains"
|
||||
done
|
||||
fi
|
||||
|
||||
echo ""
|
||||
|
||||
# Display change history
|
||||
if [ "${#changes_made_list[@]}" -gt 0 ]; then
|
||||
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
|
||||
cecho "${WHITE}${BOLD}DETAILED CHANGES${NC}"
|
||||
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
|
||||
echo ""
|
||||
|
||||
for change in "${changes_made_list[@]}"; do
|
||||
cecho " ${GREEN}✓${NC} $change"
|
||||
done
|
||||
|
||||
echo ""
|
||||
|
||||
# Get recent change history from tracker
|
||||
local recent_changes
|
||||
recent_changes=$(get_change_history 2>/dev/null || echo "")
|
||||
|
||||
if [ -n "$recent_changes" ]; then
|
||||
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
|
||||
cecho "${WHITE}${BOLD}CHANGE HISTORY${NC}"
|
||||
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
|
||||
echo ""
|
||||
cecho "$recent_changes" | head -20 # Show last 20 changes
|
||||
echo ""
|
||||
while IFS= read -r php_version; do
|
||||
[ -z "$php_version" ] && continue
|
||||
if reload_php_fpm "$php_version" >/dev/null 2>&1; then
|
||||
cecho " ${GREEN}✓${NC} Reloaded $php_version"
|
||||
fi
|
||||
fi
|
||||
done <<< "$php_versions"
|
||||
|
||||
cecho "${CYAN}═════════════════════════════════════════════════════════════════════${NC}"
|
||||
echo ""
|
||||
cecho "${CYAN}═══════════════════════════════════════════════════════════════════${NC}"
|
||||
cecho "${WHITE}${BOLD}SUMMARY${NC}"
|
||||
cecho "${CYAN}═══════════════════════════════════════════════════════════════════${NC}"
|
||||
echo ""
|
||||
cecho " Applied: ${GREEN}${optimized}${NC} changes"
|
||||
if [ "$failed" -gt 0 ]; then
|
||||
cecho " Failed: ${RED}${failed}${NC} changes"
|
||||
fi
|
||||
echo ""
|
||||
read -p "Press Enter to continue..."
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# OPTIMIZATION LEVEL 2: max_children + memory_limit
|
||||
# ============================================================================
|
||||
|
||||
optimize_level_2_memory() {
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}LEVEL 2: Optimize pm.max_children + memory_limit (All Domains)${NC}"
|
||||
echo ""
|
||||
cecho "${YELLOW}This will adjust both process limits and PHP memory settings.${NC}"
|
||||
echo ""
|
||||
cecho "${CYAN}What will be optimized:${NC}"
|
||||
cecho " • pm.max_children (based on traffic)"
|
||||
cecho " • memory_limit (PHP memory per domain)"
|
||||
echo ""
|
||||
cecho "${RED}${BOLD}WARNING:${NC} ${RED}This will modify PHP-FPM and php.ini configurations!${NC}"
|
||||
echo ""
|
||||
|
||||
if ! confirm "Continue?"; then
|
||||
cecho "${YELLOW}Operation cancelled${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
cecho "${YELLOW}Level 2 optimization coming soon...${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# OPTIMIZATION LEVEL 3: max_children + max_requests + memory_limit
|
||||
# ============================================================================
|
||||
|
||||
optimize_level_3_advanced() {
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}LEVEL 3: Advanced Optimization (All Domains)${NC}"
|
||||
echo ""
|
||||
cecho "${YELLOW}This will optimize multiple PHP-FPM settings for comprehensive improvement.${NC}"
|
||||
echo ""
|
||||
cecho "${CYAN}What will be optimized:${NC}"
|
||||
cecho " • pm.max_children (process limits)"
|
||||
cecho " • pm.max_requests (memory leak prevention)"
|
||||
cecho " • memory_limit (PHP memory allocation)"
|
||||
echo ""
|
||||
cecho "${RED}${BOLD}WARNING:${NC} ${RED}This will modify PHP-FPM and php.ini configurations!${NC}"
|
||||
echo ""
|
||||
|
||||
if ! confirm "Continue?"; then
|
||||
cecho "${YELLOW}Operation cancelled${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
cecho "${YELLOW}Level 3 optimization coming soon...${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# OPTIMIZATION LEVEL 4: OPcache ONLY
|
||||
# ============================================================================
|
||||
|
||||
optimize_level_4_opcache() {
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}LEVEL 4: Optimize OPcache (All Domains)${NC}"
|
||||
echo ""
|
||||
cecho "${YELLOW}This will enable/optimize OPcache for all domains.${NC}"
|
||||
echo ""
|
||||
cecho "${CYAN}What will happen:{{NC}"
|
||||
cecho " • Enable OPcache if disabled"
|
||||
cecho " • Adjust memory_consumption based on needs"
|
||||
cecho " • Optimize hit rate and performance"
|
||||
echo ""
|
||||
|
||||
if ! confirm "Continue?"; then
|
||||
cecho "${YELLOW}Operation cancelled${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
cecho "${YELLOW}Level 4 optimization coming soon...${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# OPTIMIZATION LEVEL 5: EVERYTHING
|
||||
# ============================================================================
|
||||
|
||||
optimize_level_5_everything() {
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}LEVEL 5: OPTIMIZE EVERYTHING (All Domains)${NC}"
|
||||
echo ""
|
||||
cecho "${YELLOW}This will apply all optimizations comprehensively.${NC}"
|
||||
echo ""
|
||||
cecho "${CYAN}What will be optimized:${NC}"
|
||||
cecho " • pm.max_children (process limits)"
|
||||
cecho " • pm.mode (STATIC/DYNAMIC/ONDEMAND)"
|
||||
cecho " • pm.min_spare_servers & pm.max_spare_servers"
|
||||
cecho " • pm.max_requests (memory leak prevention)"
|
||||
cecho " • memory_limit (PHP memory allocation)"
|
||||
cecho " • OPcache (enable & optimize)"
|
||||
echo ""
|
||||
cecho "${RED}${BOLD}WARNING:${NC} ${RED}This is the most comprehensive optimization!${NC}"
|
||||
echo ""
|
||||
|
||||
if ! confirm "Continue?"; then
|
||||
cecho "${YELLOW}Operation cancelled${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
cecho "${YELLOW}Level 5 optimization coming soon...${NC}"
|
||||
read -p "Press Enter to continue..."
|
||||
}
|
||||
|
||||
# OPTION 6: VIEW OPCACHE STATISTICS
|
||||
# ============================================================================
|
||||
|
||||
|
||||
Reference in New Issue
Block a user