I'm having a hard time to debug my Laravel app when there's a bug in the code. Let's say I have the route:
Route::get('/test', function () {
return 'it_works';
});
Everything works fine. However, if I add a bug in the code like this:
Route::get('/test', function () {
return 'all_good' . $not_declared_variable; //Obviously a bug, variable doesn't exist
});
When there's a bug, the CPU usage goes to more than 100% (on PHP-FPM). It takes minutes for nginx to write something in the log, this simple posted in this question takes between 15-45 secs to have an entry in the log saying the variable $not_declared_variable
is not declared. Plus the fact that the CPU usage is so high that if I make 3-5 requests in a row my laptop freezes. I have a i5 with 256GB SSD and 16GB RAM, so I don't think my laptop config is the issue here.
I have some deep logic happening in some controllers and it has been super annoying to wait 3-5 minutes until I get some entry in the logs to find out what's going on.
It feels that some misconfig in the PHP-FPM or NGINX is the cause. This is my nginx config file:
server {
listen 80;
listen [::]:80;
server_name admin-control.test;
root /var/www/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt/;
log_not_found off;
}
error_log /var/log/nginx/admin_error.log;
access_log /var/log/nginx/admin_access.log;
}
I have enabled opcache in the php.ini:
[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
Am I missing something here? Thanks in advance guys
Edit(1):
I identified the PID of the PHP-FPM process and run strace -ffttTo
to see what's going on and I got thousands of the following lines:
18:07:15.810151 mremap(0x7f3a79b5f000, 1556750336, 1556754432, MREMAP_MAYMOVE) = 0x7f3d6db5d000 <0.005863>
18:07:15.816138 mremap(0x7f3d6db5d000, 1556754432, 1556758528, MREMAP_MAYMOVE) = 0x7f3a79b5d000 <0.000243>
18:07:15.816512 mremap(0x7f3a79b5d000, 1556758528, 1556762624, MREMAP_MAYMOVE) = 0x7f3d6db5b000 <0.005760>
18:07:15.822443 mremap(0x7f3d6db5b000, 1556762624, 1556766720, MREMAP_MAYMOVE) = 0x7f3a79b5b000 <0.000273>
18:07:15.822863 mremap(0x7f3a79b5b000, 1556766720, 1556770816, MREMAP_MAYMOVE) = 0x7f3d6db59000 <0.007095>
18:07:15.830289 mremap(0x7f3d6db59000, 1556770816, 1556774912, MREMAP_MAYMOVE) = 0x7f3a79b59000 <0.000264>
18:07:15.830704 mremap(0x7f3a79b59000, 1556774912, 1556779008, MREMAP_MAYMOVE) = 0x7f3d6db57000 <0.005861>
18:07:15.836794 mremap(0x7f3d6db57000, 1556779008, 1556783104, MREMAP_MAYMOVE) = 0x7f3a79b57000 <0.000319>
18:07:15.837250 mremap(0x7f3a79b57000, 1556783104, 1556787200, MREMAP_MAYMOVE) = 0x7f3d6db55000 <0.006262>
18:07:15.843705 mremap(0x7f3d6db55000, 1556787200, 1556791296, MREMAP_MAYMOVE) = 0x7f3a79b55000 <0.000304>
For those coming here later, I managed to solved my issue. It was some misconfig in the php-fpm/xdebug.ini
(I still didn't manage to make XDebug work with Laradock in my Linux).
I have tried many configs in order to enable XDebug but I'm failing miserably. This is the config that I have now, it's not working but at least I don't have the high CPU usage problem anymore:
xdebug.mode=debug
xdebug.remote_host=192.168.0.16
xdebug.remote_connect_back=0
xdebug.remote_port=9001
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=0
xdebug.remote_enable=1
xdebug.cli_color=0
xdebug.profiler_enable=0
xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling"
xdebug.remote_log="/var/log/xdebug.log"
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.max_nesting_level=250
xdebug.var_display_max_children=-1
xdebug.var_display_max_data=-1
xdebug.var_display_max_depth=-1
Gonna keep trying to solve the Xdebug issue but won't post anything here because it is a different issue...