Debugging Slow PHP Scripts using PHP-FPM slow_log variable

Since the beginning of times, when you needed to check out PHP errors in the logs you always went to take a look into the famous error_log, that is part of all common web servers.

However, since I switched to Nginx and started using PHP-FPM as default daemon for php processing, I discovered something really cool. Unlike mod_php that only allows you to log php slow queries inside the general log file, by using the stack Nginx + PHP-FPM you can configure a separate php log to show the slow PHP scripts that are running on your system.

Slowly scripts can lead to system errors and bad user experience. Having a way to log all the slow PHP processes over the system is really useful to optimize the performance of your web applications.

debugging slow php scripts

PHP-FPM slow_log variable configuration

How can I setup slow_log to log all slow PHP scripts?

Open your favourite text editor on the terminal, vim or nano:

nano -w /etc/php-fpm.d/www.conf

or

/etc/php5/fpm/pool.d/www.conf

Now, press CTRL + W and search for “slowlog“. Once you’ve found the variable, set the desired location of your php slow log file, for example:

slowlog = /var/log/php-fpm/slow.log

or

slowlog = /var/log/php5/slow.log

There is another important variable to set:

request_slowlog_timeout = 5s

In my case I’ve set 5s as maximum a script can take to execute before it is marked as slow and logged inside the slowlog. This will help you to find slow scripts like never before.

Configuring PHP error log

Another useful thing you can do in order to have a correct way to debugging slow PHP scripts is to set the error_log properly:

php_admin_flag[log_errors] = on

Also set the error_log name:

php_admin_value[error_log] = /var/log/php-fpm/error.log

If you have not specified any custom error_log path for your site, the php errors will be logged into Nginx default error_log location, usually found at /etc/nginx/logs/error.log or  /var/nginx/logs/error.log.

Debugging PHP-FPM errors

If you want to have a record of your PHP-FPM daemon, connections and more, you can also set a custom error location by editing php-fpm.conf file:

nano -w /etc/php-fpm.conf

Set the error_log as you see here:

error_log = /var/log/php-fpm/error.log

Restart PHP-FPM to apply the changes:

service php-fpm restart

If you need to take a look to the log you can do it using more or tail, as you see below:

tail -f /var/log/php-fpm/slowlog.log

more /var/log/php-fpm/error.log

Conclusion

Now you know a good way to start the debug of your slow PHP scripts. Remember that this logs should be used only when you have web application errors or slow execution of scripts, once you finished investigating and fixing your issues, it is recommended to turn them off as you will save lot of disk space and avoid unnecessary I/O operations.

About the Author: Santiago Borges

Experienced Sr. Linux SysAdmin and Web Technologist, passionate about building tools, automating processes, fixing server issues, troubleshooting, securing and optimizing high traffic websites.

Leave a Reply

Your email address will not be published. Required fields are marked *