Identify Slow Pages
# Enable slow query log in PHP-FPM pool
request_slowlog_timeout = 5s
slowlog = /var/log/php8.2-fpm-slow.log
sudo systemctl restart php8.2-fpm
tail -f /var/log/php8.2-fpm-slow.log
Quick Timing in PHP
$start = microtime(true);
// ... code to profile ...
$elapsed = microtime(true) - $start;
error_log("Section took: " . round($elapsed * 1000) . "ms");
Database Query Profiling
# Enable slow query log in MySQL
SET GLOBAL slow_query_log = 1;
SET GLOBAL long_query_time = 1;
SET GLOBAL log_queries_not_using_indexes = 1;
# Analyze slow queries
mysqldumpslow -s t /var/log/mysql/slow.log | head -20
Common Bottlenecks
| Issue | Symptom | Fix |
|---|
| Missing DB index | Queries taking seconds | Add index, use EXPLAIN |
| N+1 queries | Hundreds of identical queries | Use JOINs or eager loading |
| No OPcache | High CPU, slow TTFB | Enable OPcache |
| External API calls | Waiting on remote services | Cache responses, use async |
| Large file operations | High I/O wait | Stream instead of loading into memory |
Xdebug Profiling
# php.ini
xdebug.mode=profile
xdebug.output_dir=/tmp/xdebug
xdebug.start_with_request=trigger
# Trigger profiling
curl "http://example.com/slow-page?XDEBUG_PROFILE=1"
# Open the cachegrind file with KCacheGrind or QCacheGrind