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:8080Logs 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.

inPhpStormOpen 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 installxdebugExtension:$phpbrew ext install xdebugOtherwise, it cannot be debugged.)

Apply the version of PHP you just added to the current project

withNginxRun 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 articlemacOSHomebrewNginxAs 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

Xdebugdebugging

Mostly aboutXdebugIn 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 theValidatebutton

Phpbrew Config calls the editor to open the php.ini file and adds:

[xdebug]
xdebug.remote_enable=1
Copy the code

stayphpbrew fpm restartAnd then click againValidateButton, success!

This makes it fun to debug projects deployed in Nginx. Here we click on the toolbarListening to the buttonStart 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 HelperClick on the iconOptionsOpen the options page and selectIDE keyPhpStorm

Open a browser window and enter in the address barlocalhost:8000When you visit the service, the page is displayedhello world!. Click on theXdebug HelperIcon, clickDebugAt this point, the icon will turn green (or useAlt+Shift+XShortcut key quick switchDebugMode)

Ready to start debugging:

  1. Make sure thatPhpStormListening to theButton activated
  2. inPhpStormTo break the line of code to be debugged
  3. Ensure that the browser’sXdebug HelperIn aDebugMode, the icon turns green

Refresh the browser page,PhpStormListen for incoming requests while the browser request is inPendingStatus, pending the request response

After selecting the correct PHP file in the popover,PhpStormYou 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 lineOr otherRequesting clientA 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 interpreterFigure (re-handling is placed at the bottom of this paragraph), which is clearly configuredXdebug, andWeb Server Debug ValidationStill prompt to gophp.iniaddxdebug.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?

PhpStormToolbar 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 buttonAlso 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 inPhpStormdebuggingThe 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: inVSCodeIn 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 ofPHPThe environment

VSCode will automatically create two configurations:

  • Listen for XDebug: This configuration is similarPhpStormIn theListening to the button
  • Launch currently open script: Execute according tophp-cliMode run/debugThe PHP script for the current window

chooseListen for XDebugAnd 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: inVSCodedebuggingThe remote service

Similar to Divergence 1, on another host (192.168.1.101) :

  • Make sure thatphp.iniHave been addedxdebug.remote_connect_back=1
  • chooseListen for XDebugAnd click on the one on the leftThe green iconStart running the listener and set a breakpoint
  • Run the command on the cliCurl, cookie XDEBUG_SESSION = VSCODE 192.168.1.100:8000Can 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 thethinkingconfusion

  1. XdebugIDE key(even in thephp.iniConfiguration of thexdebug.idekey=xxx) inRemote debuggingDoes not take effect while debugging the requestCookieIn additionalXDEBUG_SESSIONSetting any non-empty string takes effect. isXdebugbug?
  2. No matter usePhpStormorVSCodeThe remote server is being debuggedXdebugThe debugging port (default: 9000) is unreachable (onlylocalhostAccessible), butOn the breakpoint Open to monitorCan you debug again?
  3. Multi-person collaborative debugging, referenceJetBrainsThe documentMultiuser debugging via Xdebug proxiesuseDBGp agent