xdebug / vscode-php-debug

PHP Debug Adapter for Visual Studio Code 🐞⛔
MIT License
763 stars 178 forks source link

Unable to make Xdebug work with Remote SSH #947

Closed justsanjit closed 7 months ago

justsanjit commented 7 months ago

My current project is situated on a remote development server. I've got two options for working on my code.

  1. Mapping the remote user directory to my Local Windows machine.
  2. Utilizing the Remote SSH extension in VSCode to directly work on Dev server.

I decide to opt for second choice, but I can't seem to get xdebug up and running. In development server (xx.xxx.xxx.xxx) xdebug is running on port 9003 with dbgp proxy also running on save server accepting connections on port 9000 via the following command

dbgpProxy -i xx.xxx.xxx.xxx:9000 -s 127.0.0.1:9003

Additionally, there is nginx configuration on the dev server that allows me to access my project on the browser via sanjit-project.subdomain.domain.com

To clarify, the server I'm working on (xx.xxx.xxx.xxx) sits within a VPN. On my local computer, I accesss my project via Remote. SSH extension and begin working on my project. Following are my current configuration:

PHP version: 7.4.33 Xdebug version: 3.1.6 VS Code extension version: 1.34.0

Your launch.json:

{
    "name": "Listen for Xdebug",
    "type": "php",
    "request": "launch",
    "hostname": "127.0.0.1",
    "port": 9000,
    "log": true,
    "proxy": {
        "enabled": true,
        "host": "xx.xxx.xxx.xxx",
        "port": 9000,
        "key": "sanjit2"
    }
}

Xdebug php.ini config:

xdebug.client_host: localhost
xdebug.port: 9003
xdebug.mode: debug
xdebug.start_with_request: yes
xdebug.discover_client_host: yes

xdebug.idekey: PHPSTROM
xdebug.output_dir: /tmp
xdebug.log: no value (this is not set)

Xdebug logfile (from setting xdebug.log in php.ini): I think log is not enabled on dev server, and I don't have permissions to change it.

VS Code extension logfile (from setting "log": true in launch.json):

Listening on { address: '127.0.0.1', family: 'IPv4', port: 9999 }
Registering sanjit2 on port 9999 with proxy @ xx.xxx.xxx.xxx:9000
Registration successful
....

Code snippet to reproduce:

<?php

phpinfo();
$a = 5;   <= Break point
$b = 9;
$c = $a + $b;
echo $c;
echo "Works";

// more code here

With the above setup, when I initiate the debugger, I receive logs in the Debug console. I have the Xdebug helper installed in my browser, and the IDE key is set too "sanjit2". However, despite adding breakpoints, upon page refresh, it doesn't halt at the breakpoints. Could you please help identify what might be amiss here? Any guidance would be highly appreciated.

zobo commented 7 months ago

Using the VSCode Remote SSH extension, in my opinion, indeed provides the best experience, but there are some technical details one must be aware of.

When you use Remote SSH your vscode actually runs on the remote server. That is, where your PHP files are and where your PHP and Xdebug process is executing. Providing you are not using Docker (or other form of containerization and network virtualization) your vscode php-debug extension and Xdebug are actually running on the same host and are reachable over localhost. Also your dbgpProxy runs on the same host.

First, there are some strange things in your config, let me point those out, then I'll give some suggestions.

  1. In launch.json you list port and proxy.port both 9000. A log entry about proxy registration hints that port is actually 9999.
  2. proxy.host could also be 127.0.0.1/localhost
  3. you register with IDE KEY sanjit2 but your xdebug.ini defines PHPSTROM as static IDE key, so this IDE and Xdebug won't ever get connected. Either remove it from Xdebug.ini or change it to sanjit2.
  4. Since you have xdebug.client_host: localhost the xdebug.discover_client_host: yes does not make sense. Remove it.

I would suggest you stop dbgpProxy and not use it. Change your launch.json port to 9003 and remove the proxy configuration.

justsanjit commented 7 months ago

Thank you for the prompt response. Based on your suggestions:

In launch.json you list port and proxy.port both 9000. A log entry about proxy registration hints that port is actually 9999.

The proxy.port and proxy registration log both indicate 9999. This was a typo while creation of this issue.

proxy.host could also be 127.0.0.1/localhost

If I change the proxy host to 127.0.0.1 or localhost, when I attempt to start the debugger, I get a popup with the connect EONNREFUSED 127.0.0.1:9000 error.

And these are the extension logs:

Listening on { address: '127.0.0.1', family: 'IPv4', port: 9999 }
Registering PHPSTORM on port 9999 with proxy @ 127.0.0.1:9000

<- outputEvent
OutputEvent {
  seq: 0,
  type: 'event',
  event: 'output',
  body:
   { category: 'stderr', output: 'PROXY ERROR: connect ECONNREFUSED 127.0.0.1:9000n' } }

PROXY ERROR: connect ECONNREFUSED 127.0.0.1:9000
<- launchResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 2,
  command: 'launch',
  success: false,
  message: 'connect ECONNREFUSED 127.0.0.1:9000',
  body:
   { error: { id: -111, format: 'connect ECONNREFUSED 127.0.0.1:9000', showUser: true } } }

you register with IDE KEY sanjit2 but your xdebug.ini defines PHPSTROM as static IDE key, so this IDE and Xdebug won't ever get connected. Either remove it from Xdebug.ini or change it to sanjit2.

I updated the IDE Key to PHPSTORM in launch.json and adjusted my xdebug helper to use the same key.

Since you have xdebug.client_host: localhost the xdebug.discover_client_host: yes does not make sense. Remove it.

Unfortunately, I'm unable to make these changes on the dev server. Other developers on the team are also using Xdebug with the same php.ini via PHPStorm, so I believe it should be fine.

I can't remove the dbgpProxy as I lack the necessary permissions, and other developers are using Xdebug on the same project via this proxy.

Do you have any other suggestions as to why I might be receiving the connect EONNREFUSED 127.0.0.1:9000 error when using the proxy host 127.0.0.1?

zobo commented 7 months ago

I understand. The connection refused probably comes from the fact that dbgpProxy -i xx.xxx.xxx.xxx:9000 -s 127.0.0.1:9003 binds to the specific interface and is not accessible on localhost. Since you cannot remove dbgpProxy here is the suggested launch.json. If I understand this is the only part of the system you have control over.

{
    "name": "Listen for Xdebug",
    "type": "php",
    "request": "launch",
    "port": 0,
    "log": true,
    "proxy": {
        "enabled": true,
        "host": "xx.xxx.xxx.xxx",
        "port": 9000,
        "key": "PHPSTROM"
    }
}

Note that xdebug.ini says PHPSTROM instead of PHPSTORM. Note the typo. I have added the typo-ed ide key into the suggested launch.json config. Note that I removed hostname from launch.json. This is because php-debug registers to dbgpProxy with your servers external IP, but using hostname: localhost would cause php-debug to only accept connections on localhost.

I changed port to 0 to let php-debug use random ports, but you can also use a fixed unused port like 9999.

Do you maybe see the logs of dbgpProxy?

justsanjit commented 7 months ago

It worked, but for one time only. During my first attempt, everything functioned properly.

Out of curiosity, I wanted to see if it would keep working after restarting VSCode and Remote SSH. Unfortunately, I forgot to stop the debugging before exiting VSCode. Now, when I restart VSCode and try to reconnect, I encounter the following error.

Listening on { address: '::', family: 'IPv6', port: 43397 }
Registering PHPSTORM on port 43397 with proxy @ xx.xxx.xxx.xxx:9000
<- outputEvent
OutputEvent {
  seq: 0,
  type: 'event',
  event: 'output',
  body:
   { category: 'stderr',
     output: "PROXY ERROR: A client for 'PHPSTORM' is already connected\n" } }

PROXY ERROR: A client for 'PHPSTORM' is already connected

It seems like the dbgp Proxy remembers my previous IDE Key and is still registered. How can I bypass this error now?

Note: PHPSTROM was a typo as well, In my php.ini file PHPSTROM is the IDE key.

zobo commented 7 months ago

Yes, dbgpProxy has some issues.

https://stackoverflow.com/a/55703579 see the part with telnet and proxystop

justsanjit commented 7 months ago

Thank you so much, @zobo. It works like a charm.