PHPThe program reported an error, and the naked eye reviewed it many times, and felt that it was well written; there was a switch code branch to judge why it could not enter what I wanted
PHPThe code is still in use
var_dump($re); die();. try
XdebugBreak point debugging.
What can he do
Turn on debug monitoring, step by step along the code into the deepest part of the program. You’ll learn the real code steps and the call relationships. You can also know the value and change of each variable when the program is running. At the same time, it avoids
var_dumpCode implantation (if you forget to delete it and submit it online, the stigma will be nailed to you)
OK, let’s introduce our main character Xdebug
XdebugIs a PHP extension, provides debugging and performance analysis functions.  It uses dbgp debugging protocol.
The debugging information that Xdebug can provide includes the following contents:
- Stack and function traces in error message  have:
- Full parameter display of user defined function
- Function name, file name and line indication
- Support member function
- memory allocation
- Protect infinite recursion
Xdebug also provides:
- Profile analysis information of PHP script 
- Code coverage analysis
- Debug script functions interactively with the debugger front end. 
The above is from wiki
Let’s do it
This article focuses on debugging PHP Xdebug in docker environment.
Let’s first talk about the demo environment of the writer: MAC environment, install virtual box, run an Ubuntu virtual machine, and then install docker CE Software. Seeing this, don’t rush to close the website. I think my environment is a bit of a miracle, but the general process is similar.
PHP DockerEnvironment this article has a detailed introduction: [docker quickly builds a set of PHP, nginx, mysql, redis, Xdebug, memcached development environment
Install the software
Installing Xdebug for PHP
Xdebug official documentation has been introduced.
- Linux or Mac environment through the command line:
$ pecl install xdebug
- Windows users can download it directly according to their PHP version through Xdebug download page
*.dllPut it in the extended directory.
$PHP - V // view PHP version
- Manual compilation and installation
For the network is not so smooth, PECL can not use time.
##Get the source code $ git clone https://github.com/xdebug/xdebug.git ##Enter directory $ cd xdebug ##PHP check $ phpize ##Pre compilation configuration, generally speaking, phpize is ready for most of the work, as well as configuration $ ./configure ##Compile $ make ##Test and install $ make test && make install
- Docker environment to install PHP extensions for PHP
Docker environment provides two ways. The first is to download and install online through PECL. Limited by network conditions, it will probably fail.
Then you can try to download the compressed package of PECL Xdebug through other ways and add it to the docker container for installation.
FROM php:7.3-fpm-buster ... #Method 1 PECL installation RUN yes | pecl install xdebug \ && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \ && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \ && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini ## ------------------------------------------------ #Method 2 download and install Xdebug ADD ./xdebug-2.6.1.tgz /tmp/php7-xdebug RUN cd /tmp/php7-xdebug/xdebug-2.6.1/ && phpize && ./configure && \ make && make install
Find our profile
##File / etc / PHP / conf.d/ xdebug.ini zend_ extension= xdebug.so //Enable Xdebug extension! xdebug.remote_connect_back=1 xdebug.remote_ Enable = 1 // enable remote debugging xdebug.remote_mode = "req" xdebug.remote_handler = "dbgp" xdebug.remote_ Host = "10.0.2.2" // this is the IP address of your phpstorm running environment xdebug.remote_ Port = 9000 // the default is 9000. It's better not to move xdebug.idekey= "Phpstorm" // default value for phpstorm IDE xdebug.remote_autostart = 1
xdebug.remote_hostThis IP is not just for granted
127.0.0.1You need to see if the environment you are running phpstorm and PHP are in the same environment. If so, you can fill in the
127.0.0.1No problem. If it’s docker, it’s definitely not in the same environment. How do we see the IP address of our ide environment? First edit the entry file
##File: ~ / public/ index.php <?php print_r($_SERVER['REMOTE_ADDR']);die();
Open the browser to access the PHP FPM service. What’s here,
xdebug.remote_hostJust fill in as much as you want.
In the docker environment, to debug code with IDE, IDE also needs to deal with docker. It is to configure the API entry of dcoker in phpstorm.
For this, you can refer to this article to configure the docker environment by opening the remote API to access port 2375.
Reprint with reference
When the above is ready, we will start our play, configuration
Docker Remote APIConfiguration on phpstorm
Since my environment is equipped with VBox and NAT, the port is 42376. You need to configure your own environment.
Connection SuccessfulThe configuration is successful. there
Name:Write down the name and use it later.
2. Add PHP
Search: PHP language, click on the far right
Step 1The name of the dcoker server for.
Image name:Choose your PHP container.
After clicking confirm, phpstorm will check the version of PHP container and whether Xdebug extension is enabled. If there is no error, it means that the environment is correct and you can proceed to the next step.
Pay attention to the red box
Here is the display effect after configuration,
PHP language level:Best and PHP container consistent
This configuration is used to let ide know the running environment of PHP code and the directory mapping relationship between local PHP code.
Local or mounted flolderFirst, I’ll let you type in
New server name:。
What I input is
bs. You can also enter others, but all of them should be recorded. This name will be used later.
Folder:Configure the file path of phpstorm runtime environment. (that is, the MAC file path.)
Here we look at the PHP container code path
Deploment path:Fill in the code path in the container,
Local path:Select the local environment code path.
PHP > ServerHere, click the import button in the circle. Select the new one in the pop-up box
Deploymentto configure. (it’s just entered
New server name）
File/DirecotryIf you check it, it’s usually OK.
Absolute path on the serverIt is the root directory in the docker container where your code runs. After adjustment, click
Don’t worry, close the pop-up box, and then confirm
Absolute path on the server。
It is estimated that it is a bug of phpstorm or the demand of the product. You need to configure it once inside and outside the bullet box.
Good phpstorm configuration is complete.
Docker runtime configuration
In the docker runtime container, two more environment variables need to be configured
DeploymentThe name of the configuration.
remote_hostFill in the IP of phpstorm environment, that is, in PHP
$_SERVER['REMOTE_ADDR']The value of.
remote_portThe default is 900
- You need to be right
docker-compose.yamlAdd environment variables to PHP container in configuration file:
##File: ~ / docker- compose.yaml version: "2" services: php: image: paulxu/php:5.6-fpm-jessis-pdo-xdebug-mysqli-gd-mb-zip-2 volumes: ... ports: ... environment: PHP_IDE_CONFIG: "serverName=bs" XDEBUG_CONFIG: "remote_host=10.0.2.2 remote_port=9000"
- If you use it
docker runPull up the container and add the following parameters
$ docker run ... --env PHP_IDE_CONFIG="serverName=dealman" --env PHP_IDE_CONFIG="serverName=bs" ... php /bin/bash
1. Enable Xdebug listening and click the red phone below.
Now it’s time to start listening.
2. Find the entry file
～/index.php, select the first line of code, hit a breakpoint, (do not know where to click, press common + F8)
3. Start visiting the web site.
The first breakpoint here is at the red arrow by clicking the green button
Step over, the code is executed step by step to the blue arrow, and this line of code is also highlighted by the IDE blue background. At this time, you can also
VariablesSee the defined variable
$confThe value of.
Before the boss is still showing off with Notepad hand tearing code, over and over again. In the increasingly fast pace of development, the use of advanced tools, as well as detailed dashboard to develop debugging code. I hope you can see that the configuration is successful.
Any questions are welcome.
Vscode debugging PHP multiprocess program with Xdebug
Configuring Xdebug in phpstorm based on docker under mac
Using Xdebug to debug PHP program in PHPStorm (framework / native is applicable)