Debugging

Using Xdebug

lxc-xdebug

To run Xdebug on a container run lxc-xdebug {container-name}.

Command Line Xdebug

In the containers php is running without xdebug by default.

To run with xdebug you need to run \php or phpx.

Breakpoints

Breakpoints allow you to stop code execution at a specified line of code.

These are useful when you're not sure what variables' values are at a specific point, or which paths the execution is running through. They're much preferable to die() statements because you can find out what's happening without having to re run the application

Setting breakpoints in PHPStorm

Conditional Breakpoints

If you need to break only when certain codition is true, you can set a conditional breakpoint. To do that place normal breakpoint, right click the red circle and insert your condition.

Warning

Never use your code to create conditional breakpoints.

Watches

Watches are useful for inspecting a variable or function's value as you step through the code.

They constantly display the value of the expression at whatever point of execution you're at, so that you can see when, and why the value changes.

Adding watches in PHPStorm

xdebug_break()

Adding an xdebug_break() function call to your code will cause execution to pause on that line. It's the same as setting a breakpoint, but can be useful when your IDE's "conditional breakpoint" functionality doesn't cover your needs.

Debugging Locally & Prevention of Running in Production

By having conditional statement below, you can be more safe, while working with data that is specific to development environment only. However, you should better be pasting this code in PHPStorm debugging console.

1
2
3
4
<?php
if ($_SERVER['USER'] == 'ec' && strpos($_SERVER['HTTP_HOST'], 'desktop.com') !== false) {
    // debugging code here
}

Debugging PHP fpm responses

Install fcgi below, to allow us to debug php-fpm socket directly.

yum install fcgi -y

Then create a bash snippet below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env bash

yum install fcgi -y

HTTPS="on";
SCRIPT_FILENAME="/var/www/vhosts/www.staging.{someclient}.developmagento.co.uk/pub/index.php";
QUERY_STRING="";
REQUEST_METHOD="GET";
CONTENT_TYPE="";
CONTENT_LENGTH="";

SCRIPT_NAME="/index.php";
REQUEST_URI="/somepage.html";
DOCUMENT_URI="/index.php";
DOCUMENT_ROOT="/var/www/vhosts/www.staging.{someclient}.developmagento.co.uk/pub";
SERVER_PROTOCOL="HTTP/1.1";
SERVER_PORT="8080";

GATEWAY_INTERFACE="CGI/1.1";
SERVER_SOFTWARE="nginx/1.12.0";

REMOTE_ADDR="127.0.0.1";
REMOTE_PORT="44108";
SERVER_ADDR="127.0.0.1";
SERVER_NAME="www.staging.{someclient}.developmagento.co.uk";



cgi-fcgi -bind -connect /var/run/php-fcgi-php-fpm-backend.sock

To get variable values above, edit your magento index.php and put the line at the top

<?php var_dump($_SERVER);die;

Then fill in the variables above...

Use this PHP snippet below to generate server variables, temporary place it in pub/index.php DEV environment.

1
2
3
4
5
6
<?php

foreach ($_SERVER as $key => $value) {
    echo "export $key=\"$value\";\n";
}
die;

Useful resources

https://www.thatsgeeky.com/2012/02/directly-connecting-to-php-fpm/

FastCGI buffers