PHP + phpstorm + Xdebug remote debugging

wwz_ henu 2022-02-13 05:26:22 阅读数:879

php phpstorm xdebug remote debugging

PHP The principle of remote debugging is : In the browser ( Or by other means ) Access the remote server address , The remote server will debug The message is sent to the configured address ( Mark as address 1), This address is PHPStorm Listening address ,PHPStorm Process after receiving the data , And return the data to the remote server , In turn , Until the breakpoint or end .

Address 1 Should be the address that the remote server can access , But there is no independent external network in the company IP, I'm not willing to use domain name mapping software , So I decided to use PHP Of socket To forward data .

Ideas :

  • Create on remote server socket, monitor 9001 port ,9000 Port occupied
  • Create... On this machine socket1, Connect to remote server 9001
  • Create... On this machine socket2, Connecting the machine PHPstorm Monitoring 9000
  • socket received DEBUG After the data , Forward data to socket1,socket1 Forward data to value socket2
  • socket2 received PHPStorm After the data , Forward data to socket1,socket1 Forward data to remote server
  • .......
  • .......

1. demand

A remote server , I use Alibaba cloud hosts , Domain name is www.wanglovechu.com

XDEBUG Expand ( remote server )

SOCKET Expand ( remote server )

PHPStorm( Local )

Project documents ( Local 、 remote server )

PHP Running environment ( remote server )

2. Start remote debugging on the remote server

xdebug.remote_host=localhost
xdebug.remote_port=9000
These two lines indicate that the remote server will debug Data sent to localhost The host 9000 port , If this machine has an external network IP, Can be xdebug.remote_host Set as the external network of this machine IP, At the same time, the specified port shall be ensured ( In this case 9000) Accessible .

3. To configure PHPStorm

3.1

In position 1 Write the project address on the remote server , In position 2 Write the local and remote project addresses , That is, configure item mapping .

3.2


stay 3( A drop-down list ) Choose from 3.1 Created in server(wanglovechu)

stay 4 Write the starting address in , if index.php Configured to DirectoryIndex(nginx In Chinese, it means index), Can be written directly “/”

3.3


Can be in 5 Write a custom name , stay 6 Place selection 3.2 Medium configured server, You can also click 7 Create a new configuration server

4. To write socket Script

Remote server-side script server.php

<?php
/**
* Created by PhpStorm.
* User: wwzhenu
* Date: 2018/1/22
* Time: 14:33
*/
# The server sends data to this interface
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === FALSE)
die('socket_create fail');
$ip = '0.0.0.0';
$port = 9001;
if (socket_bind($socket, $ip, $port) === FALSE)
die('9001 socket_bind fail');
if (socket_listen($socket, 4) === FALSE)
die('9001 socket_listen fail');
$client=NULL;
$server=NULL;
$read[]=$socket;
do{
socket_select($read,$w,$e,NULL);
foreach ($read as $s){
if ($s==$socket){
echo 'a new connection'.PHP_EOL;
$get=socket_accept($s);
$data=socket_read($get,10000000);
# Receive client data
if ($data=='wang'){
echo 'client connect'.PHP_EOL;
$client=$get;
}
# received chrome data
else{
ECHO 'debug data'.PHP_EOL;
$server=$get;
if (isset($client))
socket_write($client,$data,strlen($data));
}
echo $data.PHP_EOL;
}else if($s==$client){
echo 'receive PHPstorm data'.PHP_EOL;
$data=socket_read($s,10000000);
echo $data.PHP_EOL;
socket_write($server,$data,strlen($data));
}else if ($s==$server){
echo 'receive debug data'.PHP_EOL;
$data=socket_read($s,10000000);
echo $data.PHP_EOL;
socket_write($client,$data,strlen($data));
}
}
$read=[$socket];
if (isset($client))
$read[]=$client;
if (isset($server))
$read[]=$server;
}while(1);
The script needs to be executed locally client.php
<?php
/**
* Created by PhpStorm.
* User: wwzhenu
* Date: 2018/1/22
* Time: 14:44
*/
# Connect remote
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === FALSE)
die('socket_create fail');
$ip="www.wanglovechu.com";
$port=9001;
socket_connect($socket, $ip, $port);
socket_write($socket,'wang',4);
# Connect PHPstorm
$phpstorm = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($phpstorm === FALSE)
die('socket_create fail');
$ip="127.0.0.1";
$port=9000;
socket_connect($phpstorm, $ip, $port);
$read[] = $socket;
$read[] = $phpstorm;
do{
socket_select($read,$w,$e,NULL);
foreach ($read as $s){
# Received remote data
if ($s==$socket){
echo 'receive debug data'.PHP_EOL;
$data=socket_read($s,10000000);
socket_write($phpstorm,$data,strlen($data));
}
# received PHPstorm data
elseif ($s==$phpstorm){
echo 'PHPstorm data'.PHP_EOL;
$data=socket_read($s,10000000);
socket_write($socket,$data,strlen($data));
}
echo $data.PHP_EOL;
}
$read=[$phpstorm,$socket];
}while(1);
5. Execute the script

Execute... On the server server.php

Execute... Locally client.php

6. Breaking point , Start debugging


Click the bug button in the upper right corner , Start debugging , The configured browser will be used to access www.wanglovechu.com/socket/index.php.....

Check it again PHPStorm, You can view the debugging information

notes : These two scripts can only be used once , If you need to debug again , The script needs to be re executed , Because every time DEBUG,PHPStorm Will use the new socket, Those who are interested can improve .

If there is any inaccuracy or error, please leave a message to inform , Thank you for .


copyright:author[wwz_ henu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130526201820.html