diff --git a/modules/performance/php-optimizer.sh b/modules/performance/php-optimizer.sh index 42789e2..35dc71d 100755 --- a/modules/performance/php-optimizer.sh +++ b/modules/performance/php-optimizer.sh @@ -429,8 +429,106 @@ optimize_domain() { echo "" cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" - cecho "${YELLOW}${BOLD}WARNING:${NC} ${YELLOW}Automatic optimization not yet implemented${NC}" - cecho "${YELLOW}Please apply the above recommendations manually${NC}" + echo "" + + # Ask if user wants to apply changes + read -p "Apply these recommendations? (y/n): " apply_choice + + if [[ ! "$apply_choice" =~ ^[Yy]$ ]]; then + cecho "${YELLOW}Optimization cancelled - no changes made${NC}" + echo "" + read -p "Press Enter to continue..." + return + fi + + # Create backup first + cecho "${CYAN}Creating backup before making changes...${NC}" + local backup_dir + backup_dir=$(backup_user_php_configs "$username" "$domain" 2>&1) + + if [ $? -ne 0 ]; then + cecho "${RED}${BOLD}✗ Backup failed - aborting changes${NC}" + echo "" + read -p "Press Enter to continue..." + return + fi + + cecho "${GREEN}✓ Backup created: $(basename "$backup_dir")${NC}" + echo "" + + # Apply changes + cecho "${CYAN}Applying optimizations...${NC}" + echo "" + + local changes_made=0 + local changes_failed=0 + + # Find pool config + local pool_config + pool_config=$(find_fpm_pool_config "$username") + + if [ -n "$pool_config" ] && [ -f "$pool_config" ]; then + # Apply max_children change if recommended + if [ -n "$recommended_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 + cecho " ${GREEN}✓${NC} Set pm.max_children = $recommended_max_children" + changes_made=$((changes_made + 1)) + else + cecho " ${RED}✗${NC} Failed to set pm.max_children" + changes_failed=$((changes_failed + 1)) + fi + fi + fi + + echo "" + cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}" + + if [ "$changes_made" -gt 0 ]; then + cecho "${GREEN}${BOLD}✓ Applied $changes_made optimization(s)${NC}" + + if [ "$changes_failed" -gt 0 ]; then + cecho "${YELLOW}⚠ Failed to apply $changes_failed optimization(s)${NC}" + fi + + echo "" + cecho "${YELLOW}Changes have been applied. Restart PHP-FPM for changes to take effect.${NC}" + echo "" + read -p "Restart PHP-FPM now? (y/n): " restart_choice + + if [[ "$restart_choice" =~ ^[Yy]$ ]]; then + # Detect PHP version + local php_version + php_version=$(detect_php_version_for_domain "$domain") + + cecho "${CYAN}Restarting PHP-FPM ($php_version)...${NC}" + + if reload_php_fpm "$php_version" >/dev/null 2>&1; then + cecho "${GREEN}✓ PHP-FPM reloaded successfully${NC}" + + # Verify it's running + sleep 1 + if verify_php_fpm_running "$php_version" >/dev/null 2>&1; then + cecho "${GREEN}✓ PHP-FPM is running${NC}" + else + cecho "${RED}✗ WARNING: PHP-FPM may not be running!${NC}" + cecho "${YELLOW}Run: systemctl status ea-php${php_version#ea-php}-php-fpm${NC}" + fi + else + cecho "${RED}✗ Failed to restart PHP-FPM${NC}" + cecho "${YELLOW}You may need to restart manually:${NC}" + cecho "${YELLOW}systemctl restart ea-php${php_version#ea-php}-php-fpm${NC}" + fi + else + cecho "${YELLOW}Skipping restart - remember to restart PHP-FPM manually!${NC}" + fi + + echo "" + cecho "${CYAN}Backup location: ${WHITE}$(basename "$backup_dir")${NC}" + cecho "${CYAN}To rollback: Use Option 'r' (Restore from Backup)${NC}" + else + cecho "${YELLOW}No changes were applied${NC}" + fi + echo "" read -p "Press Enter to continue..." }