Welcome to reprint, but please quote the original source at the beginning or end.
(Read this article, no one will know Php Xdebug better than you.)
Create a thin project (command line)
Create a project and install a favorite micro-framework in Composer as an example
$ mkdir debug
$ cd debug
$ composer require mikecao/flight
Copy the code
Write the main program index.php
cat >> index.php <<EOF <? php require 'vendor/autoload.php'; Flight::route('/', function(){ echo 'hello world! '; }); Flight::start(); EOFCopy the code
Run the project with the PHP built-in server
$ php -S localhost:8080
[Tue Dec 1 23:31:12 2020] PHP 7.4.13 Development Server (http://localhost:8080) started
[Tue Dec 1 23:31:35 2020] [::1]:57780 Accepted
[Tue Dec 1 23:31:35 2020] [::1]:57780 [200]: GET /
[Tue Dec 1 23:31:35 2020] [::1]:57780 Closing
Copy the code
The last three lines of output above are accessed from another terminal on the machinehttp://localhost:8080
Logs generated when
$ curl -L localhost:8080
hello world!
Copy the code
(Use PHP’s built-in server to run the project just to check that it is running properly; This built-in server can only be used in development environments, not production environments.)
Transfer development toPhpStorm
中
JetBrains’ PhpStorm and its installation are not covered here.
inPhpStorm
Open project in
Open thepreferences(The MAC shortcut key isCommand+
In Win/Linux, the shortcut key isCtrl+Alt+s
), the left sidebar is positioned toLanguages & Frameworks
= >PHP
Add the version of PHP that was previously installed in PHPBrew here. Refer to the abovePHPBrew Usage Guide(Note: Be sure to installxdebug
Extension:$phpbrew ext install xdebug
Otherwise, it cannot be debugged.)
Apply the version of PHP you just added to the current project
withNginx
Run the project
Installation and Management
- Regardless of the system environment – compile and install, do not repeat
- macOS
$brew install nginx $brew services start/stop/restart nginxCopy the code
- Debian / Ubuntu
$sudo apt update $sudo apt install nginx-full $sudo systemctl start/stop/restart nginxCopy the code
- RHEL / CentOS
$sudo yum update $sudo apt install nginx $sudo systemctl start/stop/restart nginxCopy the code
Configuration (in this articlemacOS 下 Homebrew 版 NginxAs an example)
Enable PHP-FPM (+ FPM variant is required when installing PHP with PHPBrew)
$ phpbrew fpm start
Starting php-fpm...
Copy the code
Create nginx.conf in the project root directory as the nginx configuration file
server { server_name localhost; listen 8000; root /Users/chaos/Work/php/demos/debug; location / { try_files $uri $uri/ /index.php; Fastcgi_pass Unix: / Users/chaos /. Phpbrew/PHP / 7.4.13 / var/run/PHP - FPM. The sock. fastcgi_index index.php; include fastcgi.conf; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }}Copy the code
The configuration file soft links to Nginx configuration directory/usr/local/etc/Nginx/servers
$ ln -sf /Users/chaos/Work/php/demos/debug/nginx.conf /usr/local/etc/nginx/servers/php-debug.conf
Copy the code
Check nginx.conf for errors
$ nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Copy the code
Start the Nginx
$ brew services start nginx
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
Copy the code
Using curl to access the Nginx deployed service, verify that the project is running successfully
$ curl -L localhost:8000
hello world!
Copy the code
Xdebug
debugging
Mostly aboutXdebug
In the previous part of the configuration has been configured, let’s verify whether the configuration is successful
PhpStorm click Run on the top menu bar, select Web Server Debug Validation from the drop-down menu, and click the Validate button at the bottom of the pop-up window.
- If the command output is successful, the debugging environment is set up
- If an error is displayed, a troubleshooting message is given
Three warnings were found and checked according to the troubleshooting information. The problem was found that Xdebug 3.0.0 had a lot of changes, resulting in a lack of compatibility with this problem in the latest stable version of PhpStorm, 2020.2.4. However, it is expected to be resolved by 2020.3.
Xdebug 2.x: phpbrew ext install Xdebug 2.9.8: Validate
Here we look at phP-FPM information
$ phpbrew fpm info ... Xdebug support => Enabled Version => 3.0.0...Copy the code
So you need to reboot phP-FPM to load the new Xdebug module
$ phpbrew fpm restart Stopping php-fpm... Starting php-fpm... $ phpbrew fpm info ... Xdebug support => Enabled Version => 2.9.8...Copy the code
Click again on theValidate
button
Phpbrew Config calls the editor to open the php.ini file and adds:
[xdebug]
xdebug.remote_enable=1
Copy the code
stayphpbrew fpm restart
And then click againValidate
Button, success!
This makes it fun to debug projects deployed in Nginx. Here we click on the toolbarListening to the button
Start listening for connection requests
The browser is debugged as the request client
Install the plug-in for your browser
- Xdebug Helper for Firefox
- Xdebug Helper for Chrome
Here the author takesChromeFor example, right-click the browser after the installation is completeXdebug Helper
Click on the iconOptions
Open the options page and selectIDE key
为 PhpStorm
Open a browser window and enter in the address barlocalhost:8000
When you visit the service, the page is displayedhello world!. Click on theXdebug Helper
Icon, clickDebug
At this point, the icon will turn green (or useAlt+Shift+X
Shortcut key quick switchDebug
Mode)
Ready to start debugging:
- Make sure that
PhpStorm
的Listening to the
Button activated - in
PhpStorm
To break the line of code to be debugged - Ensure that the browser’s
Xdebug Helper
In aDebug
Mode, the icon turns green
Refresh the browser page,PhpStorm
Listen for incoming requests while the browser request is inPending
Status, pending the request response
After selecting the correct PHP file in the popover,PhpStorm
You enter debug mode and successfully stop at the breakpoint where you can view the current itemvariableAnd a single step
Click the Resume Program button on the left of the debug window, and the Program resumes from the breakpoint. At this point, the browser side request receives the long-awaited response and renders hello World! With the words
The command line
Or otherRequesting client
A debugging
In a real pure interface development scenario, in addition to simple requests like localhost:8000, there are many requests with complex parameters and various HTTP method requests (GET/POST/PUT/HEAD/DELETE/OPTIONS). Simply initiating debugging through the browser address bar is not adequate.
Developers usually use command line tools (such as curl/Httpie) or professional request debugging clients like Postman to initiate request tests.
According to the official documentation of Xdebug, when the browser plug-in enables the Debug mode, it only adds a Cookie to the current session: XDEBUG_SESSION=PHPSTORM
To curl:
$ curl -H "Cookie: XDEBUG_SESSION=PHPSTORM" localhost:8000
$ curl --cookie XDEBUG_SESSION=PHPSTORM localhost:8000
$ curl -b XDEBUG_SESSION=PHPSTORM localhost:8000
Postman, too, won’t go into detail
At this point, PhpStorm debug deployed in Nginx project, complete!
Ask questions
Remember there was a slide on configurationThe PHP interpreter
Figure (re-handling is placed at the bottom of this paragraph), which is clearly configuredXdebug
, andWeb Server Debug Validation
Still prompt to gophp.ini
addxdebug.remote_enable=1
JetBrains certainly wouldn’t be so boring as to place a useless configuration item here. Where does this configuration item actually work?
PhpStorm
Toolbar clickAdd Configuration
, click on the+
Number, choosePHP Build-in Web Server
, change the port to8088(or other unoccupied ports)
Select the one you just createdRun the configure php build-in server
, click on the rightGreen triangle button, run the project (while ensuring thatListening to the button
Also active)
Execute again from the command line:
$ curl -H "Cookie: XDEBUG_SESSION=PHPSTORM" localhost:8000
Copy the code
Bingo!!! PhpStorm successfully debugged and stopped at a breakpoint!
Divergence: 1 inPhpStorm
debuggingThe remote service
The service is deployed in the local Nginx, the PHP interpreter is called through the local PHP-FPM for request distribution, and Xdebug is “loaded” (the default debug port is 9000). PhpStorm also listens to the local Xdebug 9000 port for interception and debugging.
More often than not, the service is not deployed locally, but on a remote server. How do you debug it?
Next, debug the local (192.168.1.100) service on another host (192.168.1.101) to simulate remote debugging in real development.
Add xdebug.remote_connect_back=1 to the end of php.ini on this machine (192.168.1.100)
PhpStorm on another host (192.168.1.101) starts listening by clicking the Listen button on the toolbar and setting a breakpoint in the code window
Run on a terminal on the host (192.168.1.101)
$curl -h "Cookie: XDEBUG_SESSION=PHPSTORM" 192.168.1.100:8000Copy the code
You can see that PhpStorm is in debug mode and stops at the breakpoint.
Divergent 2: inVSCode
In the debuggingThe local service
Open VSCode and install the PHP Debug extension
Open the project project and click on the left sideRun
, click on theCreate a launch.json file
, the choice ofPHP
The environment
VSCode will automatically create two configurations:
Listen for XDebug
: This configuration is similarPhpStorm
In theListening to the button
Launch currently open script
: Execute according tophp-cli
Mode run/debugThe PHP script for the current window
chooseListen for XDebug
And click on the one on the leftThe green iconStart running listening, and hit a breakpoint, command line executioncurl --cookie XDEBUG_SESSION=VSCODE localhost:8000
, can be seenVSCodeThe debug state is successfully entered and the breakpoint is stopped.
Divergence 3: inVSCode
debuggingThe remote service
Similar to Divergence 1, on another host (192.168.1.101) :
- Make sure that
php.ini
Have been addedxdebug.remote_connect_back=1
- choose
Listen for XDebug
And click on the one on the leftThe green iconStart running the listener and set a breakpoint - Run the command on the cli
Curl, cookie XDEBUG_SESSION = VSCODE 192.168.1.100:8000
Can seeVSCodeThe debug state is successfully entered and the breakpoint is stopped.
So, debugging services (local and remote) deployed in Nginx in VSCode is also done!
Some of thethinking 或 confusion
Xdebug
的IDE key
(even in thephp.ini
Configuration of thexdebug.idekey=xxx
) inRemote debugging
Does not take effect while debugging the requestCookieIn additionalXDEBUG_SESSION
Setting any non-empty string takes effect. isXdebug
的 bug?- No matter use
PhpStorm
orVSCode
The remote server is being debuggedXdebug
The debugging port (default: 9000) is unreachable (onlylocalhost
Accessible), butOn the breakpoint Open to monitorCan you debug again? - Multi-person collaborative debugging, referenceJetBrainsThe documentMultiuser debugging via Xdebug proxiesuse
DBGp agent