PHP-FPM中启用慢日志配置(用于检测执行较慢的PHP脚本)

xiaohai 2019-06-13 09:09:54 2913人围观 标签: PHP  php-fpm 
简介某些时候在寻找某个接口反映较慢的排除方法,除了从网络和服务器的硬件上面排除外,可能还需要我们通过PHP-FPM来排查问题。所以本文我们将说明如何通过PHP-FPM来查询慢日志,帮我们来排查相关问题。
开启php-fpm的慢日志

首先需要定位自己安装php目录,然后再etc/php-fpm.d目录下找到对应的配置文件,如我这里配置的为www.conf,文件中有如下信息:

; The log file for slow requests ; Default Value: not set ; Note: slowlog is mandatory if request_slowlog_timeout is set ;slowlog = log/$pool.log.slow ; The timeout for serving a single request after which a PHP backtrace will be ; dumped to the 'slowlog' file. A value of '0s' means 'off'. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays) ; Default Value: 0 ;request_slowlog_timeout = 0 ; Depth of slow log stack trace. ; Default Value: 20 ;request_slowlog_trace_depth = 20

上面:

  • slowlog:是需要存放慢日志的文件路径
  • request_slowlog_timeout:慢日志的时间,如果为0表示不开启
  • request_slowlog_trace_depth:慢日志跟踪的堆栈深度

开启慢日志必须要将slowlog和request_slowlog_timeout开启才行,slowlog设置后一定要在php的安装目录下新建一个log目录,否则重启会失败,所以这里我们只需要将前面的分号去掉即可,去掉后重启php-fpm即可。在log目录下多了一个www.log.slow的文件。

测试

我这里通过一个laravel项目中写了一个sleep(10);代码,然后运行,观察慢日志文件发现:

script_filename = /home/laravel/public/index.php [0x00007f9c13822000] sleep() /home/laravel/app/Http/Controllers/TestController.php:39 [0x00007f9c13821930] index() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54 [0x00007f9c138218c0] call_user_func_array() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54 [0x00007f9c13821820] callAction() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45 [0x00007f9c13821720] dispatch() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php:212 [0x00007f9c13821680] runController() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php:169 [0x00007f9c13821590] run() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php:658 [0x00007f9c13821480] Illuminate\Routing\{closure}() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30 [0x00007f9c138213b0] Illuminate\Routing\{closure}() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41 [0x00007f9c138212a0] handle() /home/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149 [0x00007f9c13821090] Illuminate\Pipeline\{closure}() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 [0x00007f9c13820f50] Illuminate\Routing\{closure}() /home/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:68 [0x00007f9c13820cb0] handle() /home/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149 [0x00007f9c13820aa0] Illuminate\Pipeline\{closure}() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 [0x00007f9c13820960] Illuminate\Routing\{closure}() /home/laravel/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49 [0x00007f9c13820850] handle() /home/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149 [0x00007f9c13820640] Illuminate\Pipeline\{closure}() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 [0x00007f9c13820500] Illuminate\Routing\{closure}() /home/laravel/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:63 [0x00007f9c138203a0] handle() /home/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149 [0x00007f9c13820190] Illuminate\Pipeline\{closure}() /home/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53

这样我们就很非常容易定位某个代码执行时间较长的位置。