FIX: Automatic OPcache memory calculation within safe limits

Now OPcache memory is automatically calculated to fit within the 60%
RAM safety threshold:

1. PHP-FPM capacity validation now reserves 256MB for OPcache
   - max_safe_php_fpm = (60% RAM) - 256MB
   - Prevents PHP-FPM+OPcache from exceeding safe limits

2. OPcache memory calculation now dynamic:
   - Accepts optional available_memory parameter
   - Won't exceed available limits
   - Minimum 32MB, maximum 256MB (typical servers)

3. Level 5 (Optimize Everything):
   - Calculates available memory after PHP-FPM allocation
   - Passes available memory to OPcache calculation
   - OPcache automatically scales down on low-memory servers

Result: Option 5 now automatically balances PHP-FPM + OPcache
within safe limits without manual configuration.
This commit is contained in:
Developer
2026-04-20 21:13:40 -04:00
parent da10729635
commit c71b2ecf8e
+23 -5
View File
@@ -1223,8 +1223,9 @@ is_opcache_enabled() {
# Calculate optimal OPcache memory # Calculate optimal OPcache memory
calculate_optimal_opcache_memory() { calculate_optimal_opcache_memory() {
local avg_rpm="$1" 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" local memory="64"
if [ "$avg_rpm" -ge 100 ]; then if [ "$avg_rpm" -ge 100 ]; then
@@ -1237,6 +1238,17 @@ calculate_optimal_opcache_memory() {
memory="64" memory="64"
fi 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" echo "${memory}M"
} }
@@ -2527,11 +2539,12 @@ optimize_level_4_opcache() {
# Enable OPcache # Enable OPcache
if enable_opcache "$ini_file" >/dev/null 2>&1; then 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 local avg_rpm
avg_rpm=$(calculate_avg_requests_per_minute "$username" 24 | cut -d'|' -f1) avg_rpm=$(calculate_avg_requests_per_minute "$username" 24 | cut -d'|' -f1)
local optimal_memory 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 # Set memory_consumption
if modify_php_ini_setting "$ini_file" "opcache.memory_consumption" "$optimal_memory" >/dev/null 2>&1; then 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 done
# Determine if recommendations are safe # 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 if [ "$total_recommended_memory" -gt "$max_safe_php_fpm" ]; then
cecho "${RED}${BOLD}⚠ WARNING: Combined recommendations exceed safe limits!${NC}" 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 if enable_opcache "$ini_file" >/dev/null 2>&1; then
local avg_rpm local avg_rpm
avg_rpm=$(calculate_avg_requests_per_minute "$username" 24 | cut -d'|' -f1) 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 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 modify_php_ini_setting "$ini_file" "opcache.memory_consumption" "$optimal_opcache_mem" >/dev/null 2>&1