Guide to Configuring PHP-FPM Slow Logs - Ubuntu Server or AlmaLinux

 

Guide to Configuring PHP-FPM Slow Logs

To ensure  php-fpm-slowlog.sh script has data to analyze, we need to properly configure PHP-FPM to generate slow request logs. These logs capture details about PHP scripts that take longer than a specified duration to execute.

https://gist.github.com/linuxmalaysia/f6d9ad909318eafd91804f78eacd112f

Here's how to set it up:

Step 1: Locate Your PHP-FPM Pool Configuration File

PHP-FPM pool configuration is typically found in a directory like /etc/php/8.3/fpm/pool.d/. From  ls output, your main pool configuration file is www.conf.

  • File Path: /etc/php/8.3/fpm/pool.d/www.conf

Step 2: Edit the www.conf File

Open this file using a text editor (like nano or vi):

sudo nano /etc/php/8.3/fpm/pool.d/www.conf

Step 3: Configure request_slowlog_timeout and slowlog

Inside the www.conf file, we need to find or add the following two directives within the [www] pool section.

  1. request_slowlog_timeout: This directive defines the maximum execution time for a PHP script after which a log entry will be made to the slow log file. It's crucial to set this to a value lower than your request_terminate_timeout (which is often 60s by default) so that slow requests are logged before they are forcefully terminated.

    • Recommendation: Start with a value like 5s or 10s. This allows us to catch scripts that are slow but not necessarily timing out yet.
  2. slowlog: This directive specifies the path to the slow log file.

    • Your current snippet shows: slowlog = /var/log/$pool.log.slow

      • $pool is a variable that PHP-FPM replaces with the name of the current pool (in your case, www). So, the actual path will resolve to /var/log/www.log.slow. This is the file we analysis script is designed to read.

Locate these lines (they might be commented out with a semicolon ;) and uncomment/modify them as follows:

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0' means 'off'.
; Available units: s(econds), m(minutes), h(ours). Default Unit: seconds
; Default Value: 0
request_slowlog_timeout = 10s ; <--- Set this to a reasonable value like 5s or 10s

; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
slowlog = /var/log/$pool.log.slow ; <--- Uncomment this line

Step 4: Ensure Log Directory Permissions

Make sure the directory where the slow log will be written (/var/log/ in this case) has appropriate permissions for the PHP-FPM user (often www-data or nginx). If PHP-FPM cannot write to this directory, the slow log will not be generated.

Check this with:

ls -ld /var/log/

And adjust ownership if necessary (e.g., sudo chown www-data:www-data /var/log/ or sudo chmod g+w /var/log/ if www-data is in a group with write access).

Step 5: Restart PHP-FPM

After making changes to the configuration file, we must restart the PHP-FPM service for the changes to take effect:

sudo systemctl restart php8.3-fpm # Or whatever command system uses for php-fpm (e.g., php-fpm, php-fpm8.3)

Step 6: Monitor the Slow Log

Once PHP-FPM is restarted and the application receives traffic, we should start seeing entries in the slow log file (/var/log/www.log.slow) whenever a script exceeds the request_slowlog_timeout.

WE can monitor it in real-time using:

tail -f /var/log/www.log.slow

Now, our php-fpm-slowlog.sh script will have the necessary data to perform its analysis.

Catatan popular daripada blog ini

PgBouncer: A Guide to Efficiently Managing Multiple Instances

HOWTO: Install WSL2 and Move AlmaLinux 9 to Another Drive