How to Diagnose and Fix PHP Performance Issues Without Restarting
Learn practical techniques to monitor OPcache, adjust PHP‑FPM settings, reset caches, employ preload, and use shared memory caching, enabling you to identify and mitigate PHP performance degradation in production without restarting services, ensuring continuous user experience.
In production environments, PHP applications may experience performance degradation, and restarting PHP‑FPM or the web server often impacts user experience. This article explores several methods to diagnose and alleviate performance drops without restarting PHP.
1. Real‑time Monitoring and Diagnosis
Use OPcache status monitoring
OPcache improves PHP performance by caching pre‑compiled bytecode. Low cache hit rates can cause slowdown. The following script displays OPcache status in real time:
<?php
print_r(opcache_get_status());
?>Pay attention to opcache_hit_rate, memory_usage and interned_strings_usage. If the hit rate falls below 90%, OPcache configuration may need adjustment.
Check PHP‑FPM status
PHP‑FPM provides a status page to monitor each pool’s activity. Enable the status page in php-fpm.conf: pm.status_path = /status Access this URL via the web server to view idle processes, active requests, and other metrics.
2. Dynamic Configuration Adjustment
Adjust OPcache settings
Based on monitoring results, modify OPcache parameters in php.ini:
; Increase OPcache memory
opcache.memory_consumption=256
; Increase max cached files
opcache.max_accelerated_files=20000
; Optimize revalidation frequency
opcache.revalidate_freq=60After changes, restart PHP‑FPM to apply them (preferably during low‑traffic periods).
Optimize PHP‑FPM process management
Adjust process settings according to server load:
; Dynamic process management
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 30
; Per‑request memory limit
php_value[memory_limit] = 256M3. Cleanup and Optimization
Reset OPcache
To clear all cached bytecode, run:
<?php
opcache_reset();
?>Note: this invalidates all caches, causing subsequent requests to re‑compile.
Manage large file uploads
Temporarily disable uploads or adjust limits:
file_uploads = Off
; or adjust size limits
upload_max_filesize = 2M
post_max_size = 8M4. Application‑level Optimizations
Database connection pooling
Although PHP lacks native pooling, you can use persistent connections or external middleware:
Use persistent PDO connections: new PDO($dsn, $user, $pass, [PDO::ATTR_PERSISTENT => true]) Employ ProxySQL or PgBouncer as a proxy pool.
Code hot‑reloading
Implement hot‑reload for custom autoloaders or route caches:
<?php
// Check cache file modification time
if (filemtime($cache_file) < filemtime($source_file)) {
unlink($cache_file); // delete old cache
// regenerate cache
}
?>5. Advanced Techniques
Preloading (PHP 7.4+)
Preload frequently used classes into memory:
<?php
// preload.php
opcache_compile_file('vendor/autoload.php');
opcache_compile_file('src/Framework/Kernel.php');
?>Add to php.ini:
opcache.preload=/path/to/preload.phpShared memory caching
Store hot data in APCu or Redis:
<?php
// Store data
apcu_store('cache_key', $data, 3600);
// Retrieve data
$data = apcu_fetch('cache_key');
?>6. Emergency Measures
If performance drops sharply and the cause is unclear, consider:
Temporarily increase OPcache memory.
Raise pm.max_children in PHP‑FPM.
Disable non‑essential extensions.
Enable more detailed error logging for diagnosis.
Conclusion
By combining monitoring tools, dynamic configuration tweaks, cache management, and code optimizations, you can effectively address PHP performance degradation without restarting services. A robust monitoring system is essential for early detection of bottlenecks, and regular performance audits and code reviews further ensure a smooth user experience.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
