Ends in
00
days
00
hrs
00
mins
00
secs
ENROLL NOW

Get $3 OFF ALL CCP, SAA, CDA, and SysOps Video Courses

How to Enable Slow Logging in PHP-FPM

Home » Others » How to Enable Slow Logging in PHP-FPM

How to Enable Slow Logging in PHP-FPM

Last updated on May 14, 2024

In the dynamic field of online development, ensuring that PHP applications run smoothly is paramount, and performing its best is critical. In this article, we will discuss how to effectively identify and address issues that could impact system functionality and user experience, developers must excel in troubleshooting techniques. Logging plays an essential role in this process as it captures detailed information about the application’s behavior during its execution. By monitoring and analyzing these logs, developers can pinpoint the root causes of issues. Utilizing logging tools not only helps in diagnosing these problems but also serves as a preventive measure against potential disruptions. Through strategic use of logging, PHP developers can enhance the reliability of their applications and provide users with uninterrupted digital experiences and satisfaction.

Understanding Slow Logging

Slow logging in PHP is one of the methods used to pinpoint and address issues within PHP applications by capturing requests that take longer than a specified amount of time to execute. This method is particularly valuable for identifying slow-running scripts or database queries that could be contributing to bottlenecks. By focusing on these aspects, slow logging allows developers to effectively address and resolve potential disruptions, facilitating a more reliable troubleshooting process within the PHP environment.

How to Enable Slow Logging

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially suitable for larger sites.

  1. To enable PHP-FPM for diagnostic purposes on a specific website, you simply modify the PHP-FPM pool configuration. This involves ensuring that PHP-FPM manages the processing of PHP scripts, allowing for detailed logging of script execution. For example, /etc/php/7.4/fpm/pool.d/tdemo.com.conf and add directives specifying the desired location of the slow log file and the time it takes for a script to run for it to be logged: 
 slowlog = /var/log/php-slow.log
 request_slowlog_timeout = 30s

The php-slow.log directive in PHP-FPM specifies the path to a file where slow requests are logged. It’s essential that the PHP-FPM pool user has the necessary permissions to create, read, and write to this log file. While I’ve set the request_slowlog_timeout value to 30 seconds to suit our specific needs, a general starting point is 3 seconds, which is commonly used to identify a script as slow.

        2. Test Your Configuration: Before applying changes, validate your configuration using the command:

Tutorials dojo strip

sudo php-fpm7.4 -tt

This command is used to test the configuration of PHP-FPM, specifically for version 7.4, and check for syntax errors without actually starting the service. The -tt option tells PHP-FPM to perform a configuration test and report back any issues found. This can be very helpful when making changes to the PHP-FPM configuration files, as it allows you to verify that all configurations are correct before restarting the service, which could impact your live environments.

         3. To be able to apply these changes right away, you must restart the PHP service after carefully testing the PHP configuration to make sure all settings are used correctly and error-free. This is an important step since it activates any newly added or modified configuration settings, making them applicable to your application’s environment. Restarting the service helps eliminate any outdated processes that could interfere with performance and ensure that your updates take effect. By doing this, you maximize your application’s stability and speed by ensuring that your PHP environment is in complete alignment with your present configuration requirements. 

Use this command to restart:

sudo service php7.4-fpm restart

Monitoring and Analyzing Slow Logs

To monitor performance issues effectively, navigate to the /var/log directory and locate the php-slow.log file. This log captures details such as the date, pool ID, script filename, and a backtrace of the most recent function calls for each request that exceeds the predefined execution time threshold. Regularly reviewing and analyzing these logs, such as when a request took over 30 seconds due to a get() function, as seen below, is essential for identifying and addressing specific slowdowns within your application.

For example, the request is the following:

 

Warning: [pool www] child 39086, script '/var/www/tddemo/index.php' (request: "GET /index.php") executing too slow (21.839782 sec), logging
  • It’s essential to focus on identifying the pool ID related to the downstream calls. This detail is crucial as it indicates which PHP-FPM pool managed the slow request, providing insights into potential configuration or resource allocation issues specific to that pool.
  • This warning indicates that a child process with ID 39086 within the [pool www] was handling a request for /var/www/tddemo/index.php. The process took approximately 21.84 seconds to execute, significantly exceeding typical execution times, prompting the log entry.

Here’s the detailed log entry:

  • To further understand the context, examine the php-slow.log entry:
[28-Apr-2024 16:17:31] [pool www] pid 65748
script_filename = /var/www/tddemo/wp-admin/td-demo-test.php
[0x00007f293ce14630] get() /var/www/tddemo/wp-includes/plugins/w3-total-cache/Cache_Redis.php:91
[0x00007f293ce14550] get_with_old() /var/www/tddemo/wp-content/request/class-http.php:96
[0x00007f293ce13fb0] update_object_term_cache() /var/www/tddemo/wp-includes/posts.php:7885
[0x00007f293ce13eb0] _prime_post_caches() /var/www/tddemo/wp-includes/class-wp-query-list.php:3175
[0x00007f293ce13670] get_posts() /var/www/tddemo/wp-includes/class-wp-query-list.php:3787
[0x00007f293ce13600] query() /var/www/tddemo/wp-includes/posts.php:2417
[0x00007f293ce13540] get_posts() /var/www/tddemo/wp-content/plugins/wdm-course//functions.php:45
[0x00007f293ce13300] setting_default_values() /var/www/tddemo/wp-includes/class-http.php:308
[0x00007f293ce13220] apply_filters() /var/www/tddemo/wp-includes/class-http.php:332
[0x00007f293ce131b0] do_action() /var/www/tddemo/wp-includes/plugin.php:517
[0x00007f293ce130d0] do_action() /var/www/tddemo/wp-admin/td-demotest.php:45 

Final Remarks

We have learned how to enable slow logging in the PHP FPM pool configuration, performed testing, and restarted the service to take effect immediately.  By implementing slow logging, PHP developers acquire a crucial tool for identifying and addressing potential issues within the PHP FPM environment. This diagnostic measure helps pinpoint problematic scripts and processes, enabling developers to intervene before these issues disrupt system stability, thus maintaining operational continuity.

Slow logging is essential for understanding the specific challenges that may arise in script execution, focusing on debugging and issue resolution. Regular monitoring of slow logs is recommended to effectively manage and mitigate potential problems, making it a standard practice in both development and maintenance cycles.

In addition to slow logging, consider leveraging other types of logs to build a comprehensive overview of your system’s health and efficiency. Access logs, error logs, debug logs, performance logs, security logs, and system logs each provide different perspectives that are crucial for a holistic approach to system monitoring and troubleshooting. By analyzing these logs together, you can gain a deeper understanding of different system behaviors, leading to more effective problem-solving and informed system management decisions. This approach not only aids in troubleshooting but also supports a more stable and reliable operational environment.

Get $3 OFF ALL CCP, SAA, CDA, and SysOps Video Courses!

Tutorials Dojo portal

Be Inspired and Mentored with Cloud Career Journeys!

Tutorials Dojo portal

AWS Exam Readiness Courses

Enroll Now – Our Azure Certification Exam Reviewers

azure reviewers tutorials dojo

Enroll Now – Our Google Cloud Certification Exam Reviewers

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS Exam Readiness Digital Courses

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE Intro to Cloud Computing for Beginners

FREE AWS, Azure, GCP Practice Test Samplers

Recent Posts

Written by: Irene Bonso

Irene Bonso is currently thriving as a Junior Software Engineer at Tutorials Dojo and also an active member of the AWS Community Builder Program. She is focused to gain knowledge and make it accessible to a broader audience through her contributions and insights.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?