diff --git a/modules/performance/php-optimizer.sh b/modules/performance/php-optimizer.sh index 7ce4a23..b53f581 100755 --- a/modules/performance/php-optimizer.sh +++ b/modules/performance/php-optimizer.sh @@ -1223,8 +1223,9 @@ is_opcache_enabled() { # Calculate optimal OPcache memory calculate_optimal_opcache_memory() { local avg_rpm="$1" + local available_memory="${2:-}" # Optional: available memory limit - # Base recommendation in MB + # Base recommendation in MB based on traffic local memory="64" if [ "$avg_rpm" -ge 100 ]; then @@ -1237,6 +1238,17 @@ calculate_optimal_opcache_memory() { memory="64" fi + # If available memory is specified, don't exceed it + if [ -n "$available_memory" ] && [ "$available_memory" -gt 0 ]; then + # Extract numeric value (remove 'M' if present) + local avail_num=${available_memory%M} + if [ "$memory" -gt "$avail_num" ]; then + memory=$avail_num + fi + # Minimum 32MB for OPcache + [ "$memory" -lt 32 ] && memory=32 + fi + echo "${memory}M" } @@ -2527,11 +2539,12 @@ optimize_level_4_opcache() { # Enable OPcache if enable_opcache "$ini_file" >/dev/null 2>&1; then - # Calculate optimal memory for OPcache + # Calculate optimal memory for OPcache (safe limit: 256MB max) local avg_rpm avg_rpm=$(calculate_avg_requests_per_minute "$username" 24 | cut -d'|' -f1) local optimal_memory - optimal_memory=$(calculate_optimal_opcache_memory "$avg_rpm") + # Pass 256MB as max available (OPcache is global and shared across all domains) + optimal_memory=$(calculate_optimal_opcache_memory "$avg_rpm" "256") # Set memory_consumption if modify_php_ini_setting "$ini_file" "opcache.memory_consumption" "$optimal_memory" >/dev/null 2>&1; then @@ -2783,7 +2796,9 @@ optimize_level_5_everything() { done # Determine if recommendations are safe - local max_safe_php_fpm=$((total_ram_mb * 60 / 100)) # 60% of RAM for PHP-FPM + # Reserve up to 256MB for OPcache (will be allocated separately) + local max_opcache_reserved=256 + local max_safe_php_fpm=$((total_ram_mb * 60 / 100 - max_opcache_reserved)) # 60% of RAM minus OPcache reserve if [ "$total_recommended_memory" -gt "$max_safe_php_fpm" ]; then cecho "${RED}${BOLD}⚠ WARNING: Combined recommendations exceed safe limits!${NC}" @@ -2892,8 +2907,11 @@ optimize_level_5_everything() { if enable_opcache "$ini_file" >/dev/null 2>&1; then local avg_rpm avg_rpm=$(calculate_avg_requests_per_minute "$username" 24 | cut -d'|' -f1) + # Calculate available memory for OPcache (remaining from 60% allocation minus PHP-FPM needs) + local available_for_opcache=$((total_ram_mb * 60 / 100 - total_recommended_memory)) + [ "$available_for_opcache" -lt 32 ] && available_for_opcache=32 local optimal_opcache_mem - optimal_opcache_mem=$(calculate_optimal_opcache_memory "$avg_rpm") + optimal_opcache_mem=$(calculate_optimal_opcache_memory "$avg_rpm" "$available_for_opcache") modify_php_ini_setting "$ini_file" "opcache.memory_consumption" "$optimal_opcache_mem" >/dev/null 2>&1