Ubuntu PHP Performance Monitoring Tools
Monitoring PHP performance on Ubuntu is crucial for maintaining application stability, identifying bottlenecks, and ensuring optimal resource utilization. Below are categorized tools—command-line, code-level analyzers, web servers, APM solutions, and custom scripts—to help you track and optimize PHP performance effectively.
These tools provide real-time insights into system resources and PHP process behavior, ideal for quick checks or scripting.
top
is a built-in command that displays real-time CPU, memory, and process usage. htop
(install via sudo apt install htop
) offers a more interactive interface with color-coded metrics and sortable columns. Use them to identify high-resource PHP processes (e.g., filter by php
in the command column).vmstat 1
to refresh stats every second—useful for detecting memory bottlenecks.sudo apt install sysstat
). Use iostat -x 1
to view detailed disk metrics (e.g., read/write speeds, await times) for PHP-driven disk-heavy applications.sysstat
package, sar
collects and reports system activity (e.g., CPU usage, memory, network). Run sar -u 1 5
to get CPU stats every second for 5 intervals.sudo apt install glances
) that aggregates system metrics (CPU, memory, disk, network, processes) in a single view. Supports remote monitoring and alerts for threshold breaches.sudo apt install dstat
) that combines system stats (CPU, memory, disk, network) in real time. Use dstat -ta 6
to track all metrics every 6 seconds.These tools provide granular insights into PHP code execution, helping you pinpoint slow functions, excessive memory usage, or inefficient loops.
sudo pecl install xdebug
) for code debugging and profiling. Configure it in php.ini
to generate profiling files (e.g., xdebug.mode=profile
, xdebug.output_dir=/tmp
). Use tools like KCacheGrind or Webgrind to visualize the data—identify which functions consume the most time/memory. Note: Xdebug has high overhead and should not be enabled in production.composer require blackfire/php-sdk
), then use the blackfire run
command to profile scripts. Provides a visual call graph, performance metrics (execution time, memory), and comparison between profiles. Ideal for production environments.composer require tideways/php-xhprof-extension
), then enable profiling in your code. Offers real-time dashboards and historical data for tracking performance trends.Since PHP often runs with Apache/Nginx, these tools help monitor web server performance and its impact on PHP applications.
server { location /nginx_status { stub_status; } }
to your config) and access it via a browser (e.g., http://localhost/nginx_status
). View active connections, requests per second, and worker process status—helpful for identifying Nginx-related bottlenecks.sudo apt install goaccess
) that parses Nginx/Apache logs in real time. Generate HTML reports showing request URIs, response times, HTTP status codes, and referrers. Use it to identify slow endpoints or frequent errors.ab -n 1000 -c 100 http://localhost/your_script.php
to simulate 1000 requests with 100 concurrent users. Measures request rate, response time, and failure rate—useful for load testing.APMs provide end-to-end monitoring of PHP applications, from server metrics to code-level details, with alerting and reporting.
composer require newrelic/php-agent
) that tracks PHP application performance in real time. Provides dashboards for response time, throughput, error rates, and database queries. Offers alerts for anomalies (e.g., high CPU usage) and distributed tracing for microservices.composer require datadog/php-dogstatsd
) that integrates with PHP, servers, and databases. Tracks metrics like request latency, memory usage, and external API calls. Provides anomaly detection, log management, and team collaboration features.For tailored monitoring, write PHP scripts to track specific metrics (e.g., script execution time, memory usage) and log them to a file or database.
<?php
$start_time = microtime(true);
$start_memory = memory_get_usage(true);
// Your PHP code here (e.g., database query, API call)
$execution_time = (microtime(true) - $start_time) * 1000; // in milliseconds
$memory_usage = memory_get_usage(true); // in bytes
// Log to a file
file_put_contents('/var/log/php_performance.log',
date('Y-m-d H:i:s') . " - Execution Time: {$execution_time}ms, Memory Usage: {$memory_usage} bytes\n",
FILE_APPEND
);
?>
Use this to monitor specific parts of your application (e.g., a slow API endpoint) and analyze logs to identify trends.These tools cater to different needs—from quick system checks to in-depth code analysis and enterprise-grade monitoring. Choose a combination based on your application size, budget, and performance requirements. For example, use htop
and vmstat
for real-time system checks, Blackfire for code-level profiling, and New Relic for end-to-end APM.