Saturday, June 2, 2018

Xdebug for Netbeans

Use xdebug for Netbeans


Now we will configure php for xdebug.
Open /etc/php.ini
$ sudo vi /etc/php.ini

And add the following to /etc/php.ini:
[XDebug]
zend_extension=/usr/lib64/php/modules/xdebug.so
html_errors=on
xdebug.remote_autostart=on
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host="192.168.33.10"
xdebug.remote_port=9000
xdebug.remote_mode=req
xdebug.idekey="netbeans-xdebug"
xdebug.remote_connect_back = On

(Press a on your keyboard to go into insert mode, then you can edit the file. Press escape key to stop the insert mode. After stopping the insert mode, press shift key + g to go to the lowest row. Shift + zz to save and close the file. You can go to command mode by pressing : on your keyboard. To save and quit by the command mode, input :wq and enter. To exit the file without saving the file, inout :q! and enter. To search a word, press ? then write a word what you want to search. For example, if you write ?aaa and press enter, "aaa" is searched and highlighted in the file. Press n to jump to next match.)

Please check carefully if the IP address (highlighted in red above) is correct or not. Use your IP address specified in Vagrantfile.
The location of xdebug.so is also important (highlighted in blue above).
Check if the location is correct with "locate" command. If you don't have it, install "mlocate":
$ sudo yum install -y mlocate
$ sudo updatedb
Then use the locate command.
$ sudo locate xdebug.so
If the location is wrong, change it correctly.


Then restart httpd:
$ sudo systemctl restart httpd

Xdebug and Netbeans configuration


Please make sure you have the info.php in the shared folder, which we created to see the PHP-related information.


To see this, we created a file.

This.


If you could make sure you have info.php file in the shared folder, download and install Netbeans from here:
https://netbeans.org/features/php/

I downloaded Netbeans version 8.2.

After installation, run the Netbeans as administrator then choose File --> New Project.


Choose "PHP" --> "PHP Application with existing sources"
Then click Next.


You will see the following. Click "Browse" and designate the project folder. This time, it is the shared folder "MyCentOs". Please make sure PHP 7.0 is selected for the PHP version and UTF-8 is selected for the Default Encoding.

You will see the following. For "Project URL", write your project URL. We made it "192.168.33.10" in the Vagrantfile, so we will write so. The index is info.php this time (you don't need to click "Browse", just write "info.php" in the "Index File" box). Then click "Finnish".


Now right-click on the project name. And click "Properties".



Choose "Run Configuration" and click "Advanced".


You will see the following. Double-click on the "Server Path" and write the path leading to the PHP project in the server. This time, the shared folder itself is the project folder, so just write "/vagrant".
In the "Project Path", write the path leading to the PHP project in your local computer. You click "..." to browse the path.
When everything is done, click "OK".

Now you should be able to run xdebug. Click the line number to add a break point. Click the play button to start the debug mode.


Then the execution stops at the break point, where you can check the what is inside a variable and so on. Step execution is possible by pressing F7. If you have done everything successfully, "netbeans-xdebug: running" should appear at the bottom right of the Netbeans.


Supplemental advice


If you want to stop execution at the first line every time the project is executed, check the "Stop at First line":





Then the execution will stop every time any code is loaded in the virtual machine. You can see more carefully what happens in the execution.

If your xdebug is not working correctly, check your server log:
# less /var/log/httpd/error_log

To check connections, use this command on Teraterm:
# netstat -an

After starting debug on netbeans, if you use this netstat command on Teraterm, you can see the port of xdebug is 80:
tcp6       0      0 192.168.33.10:80       192.168.33.2:49901      ESTABLISHED

The port of netbeans is 9000:
tcp        0      0 192.168.33.10:52024    192.168.33.2:9000       ESTABLISHED

The debugging port of netbeans can be changed here:

Diagram from stackoverflow:
Citation from stackoverflow (an answer from Linus Kleen) https://stackoverflow.com/questions/8049776/xdebug-for-remote-server-not-connecting

If the connection is not established, it is highly possible that your connection is blocked by your anti-virus program or firewall. You can check it in your anti-virus program or firewall and remove the block as it is a safe connection.


CakePHP3


If you are using CakePHP3, change the run configuration as shown below. Note that "cake" out of "http://192.168.33.10/cake/webroot" is a name of the cakephp folder. The name can be different. Either way, what you need to do is specify the directory of webroot folder of the cakephp.