PhpStorm won't talk to Xdebug despite an apparently successful connection being established
I’ve spent about three days now trying to get PhpStorm working with Xdebug, without success. I am more-or-less a complete beginner in using a modern IDE, and all I want to do is some gentle debugging of a simple PHP shell script (on macOS High Sierra). In other words, my script, PHP, Xdebug and PhpStorm are all located and run purely on my local machine. (I’m only scratching the surface, but the basics of PhpStorm look fantastic and other than debug the learning curve isn’t nearly as bad as I was expecting).
My progress so far has been a successful (as far as I know) install of Xdebug 2.9.8 on top of my PHP 7.1.16. This installation, and the correct path to my php.ini file, are confirmed by phpinfo()
My php.ini file, in /private/etc, has the following entries relevant to Xdebug:
[xdebug]
zend_extension = /usr/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so
xdebug.remote_enable = 1
xdebug.remote_host = 127.0.0.1
xdebug.remote_port = 9000
xdebug.remote_log = /users/wrb/documents/xdebug.log
… and I’m sure PHP is picking up this file correctly as the remote log is written to that location without problems.
After loads of beginners’ errors getting to this stage, I think I’ve now reached a more subtle problem. Following a debug attempt, the Xdebug remote log is showing that its server HAS, or thinks it has, made a successful connection to PhpStorm:
796] Log opened at 2023-02-14 12:54:22
[796] I: Connecting to configured address/port: 127.0.0.1:9000.
[796] I: Connected to client. :-)
[796] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///Applications/Flashair/FlashAirSync%202.0.php" language="PHP" xdebug:language_version="7.1.14" protocol_version="1.0" appid="796" idekey="16842"><engine version="2.9.8"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2020 by Derick Rethans]]></copyright></init>
And there the log stops. Despite the encouraging ‘Connected to client’ message in the log, while the debug session was running, PhpStorm (in the Debug window) just gave the following message, and no debug information whatsoever appears:
- Waiting for incoming connection with ide key '16842'
The ‘Ide key’, whatever that is, always matches the value in the remote log file whenever I make a fresh attempt, so presumably Xdebug and PhpStorm are successfully handshaking this key in some way. But after that, they seem to have decided not to talk to each other (or more precisely, is it PhpStorm that has decided not to talk to Xdebug?). The 'Listening for PHP debug connections' button is definitely turned on.
Possible causes I’m considering, but don’t know how to pursue further, are:
1) PhpStorm’s ‘PHP Info’ window, which I can get to by following links in the error messages that appear after a failed debug session, always shows:
xdebug.remote_enable 0
whereas phpinfo(), when run from within the script, always shows:
xdebug.remote_enable => On => On
If remote-enable is indeed off, that sounds a fairly fundamental problem. But if it is off, how do I turn it on, especially if phpinfo() thinks it is on anyway? Why is there a discrepancy between the two sets of information?
2) At the top of PhpStorm’s ‘PHP Info’ window, there is the line:
PHP CGI: Not Installed (php-cgi sapi is necessary to use built-in web server)
Is this ‘built-in web server’ used by Xdebug? If so, how do I install ‘php-cgi sapi', which I thought was something used for PHP web development that I hoped I wouldn’t need.
3) When I run PhpStorm's Web Server Debug Validation script, I get four green ticks against the following:
Debugger extension: Xdebug 2.9.8
Remote host: 127.0.0.1
Remote port: 9000
Remote connect back: option is enabled
But then there is an information button against the last of these, which states ‘xdebug.remote_host: option will be ignored’. If true, presumably I don’t want remote connect back enabled (whatever it is), but how do I turn it off? On the other hand, the log seems to show a successful connection to 127.0.0.1 anyway.
4) Finally, I have read several hints and tips online which refer to ‘restarting the Web server’ after making configuration changes, for example php.ini. What Web server are they talking about, and how might I restart it?
I’d be very grateful for any advice here please, as it’s become agonising especially with the macOS security integrity thingy that means I have to switch off and reboot from the recovery partition every time I want to edit php.ini! My only consolation is that it seems from reading the mass of online material that many people way more expert than me get foxed by this stuff. If I ever get to do any actual debugging, it will be trivial compared to setting all this up.
Apologies if I’ve forgotten to include any important information, which I can easily provide if necessary.
Please sign in to leave a comment.
Hi there,
(Commenting only on the overall issue, not the individual points)
Is that the full Xdebug log? Seems truncated / incomplete. You may want to enable more detailed logging on PhpStorm side for this subsystem to see if it actually get that connection. See https://www.jetbrains.com/help/phpstorm/troubleshooting-php-debugging.html#389aa54e
Anyway: please change Xdebug port from default 9000 to some another (9003 is a good choice) in both php.ini and PhpStorm. Make sure they got applied (restart your Mac if not sure) and try again.
There is a chance that Xdebug connects to php-fpm that also uses TCP 9000 port by default (and you may have it installed without knowing it). That is why Xdebug v3 got default port changed from 9000 to 9003 (to avoid this most-common reason for not working debugging connection).
An addendum: I've now tried to look at the TCP dialogue between Xdebug and PhpStorm on my local machine using Wireshark, and if I’ve understood it correctly then immediately after Xdebug sends the <init xmlns= … to PhpStorm (line 4 of the remote log, above), PhpStorm replies with a 405 Method Not Allowed error. And at that point they stop talking to each other altogether.
@Bill Woodbridge
I have posted a comment already, but it needs an approval first (as it contains a link to PhpStorm help page). I'll repeat the other part here:
Anyway: please change Xdebug port from default 9000 to some another (9003 is a good choice) in both php.ini and PhpStorm. Make sure they got applied (restart your Mac if not sure) and try again.
There is a chance that Xdebug connects to php-fpm that also uses TCP 9000 port by default (and you may have it installed without knowing it). That is why Xdebug v3 got default port changed from 9000 to 9003 (to avoid this most-common reason for not working debugging connection).
Thank you so much! Debugging has now sprung into life! I didn't even need to restart anything.
Because I knew I had Xdebug v2 I just followed along with the general view that port 9000 was the correct one for that version. It's good to know that in the end there wasn't too much wrong with my installation.
Thanks again, Bill
I've now seen your full comment - so just for completeness, yes what I've pasted was the entire contents of the Xdebug remote log (although I'm not sure whether I could have specified a more verbose logging level, nor which level I was actually on). After it wrote these entries, I stopped the debug session with the red button, but nothing more was ever written to the log.
I was wondering how to create the equivalent log from PhpStorm to see its end of the messaging, so thanks for that link, but hopefully I won't need it now the problem's solved.
Thanks again!
Not in Xdebug v2.
Xdebug v3 has customizable logging level (check the docs for xdebug.log_level, it's 7 by default, max 10)... but it will not write much useful info for this specific case.
Since the connection was not established with PhpStorm but with php-fpm, the red Stop button in PhpStorm did nothing to that session.
In this particular case the PhpStorm log would be pretty empty (for the same reason as in the previous point).
To sum it up: extra logging would be pretty useless here as no PhpStorm was involved in the actual debug session. It's more about experience (reading about similar issues elsewhere), checking debug troubleshooting sections in the IDE help / Xdebug docs, knowing how Xdebug actually works and check the networking side of things (like you did with Wireshark) -- in this particular case it would have been checking what process is actually listening on Xdebug port ("sudo lsof -nP -iTCP -sTCP:LISTEN" kind of command on Mac and look out for TCP 9000 entries) and google more from there.
BTW: PHP 7.1 is no longer supported as well as Xdebug v2. It makes sense to install more recent PHP version (7.4 at least, better 8.x) and get Xdebug v3 -- it has improvements in many areas including troubleshooting area. Unless of course, you really need that PHP 7.1 for your actual project.
Thanks, I'll certainly do that now it's working, but I didn't want to change too much all at once.
For those still having trouble with this, I have fixed this by unchecking “Ignore external connections through unregistered server configurations” in PHP > Debug
A shame there isn't a comment section in the documentation, so the community could help with such edge cases, much like PHP.net.