The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Time:2021-4-8

Why?

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 wantedcase; debuggingPHPThe code is still in usevar_dump($re); die();. tryXdebugBreak 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 avoidsvar_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

Xdebug is

XdebugIs a PHP extension, provides debugging and performance analysis functions. [1] It uses dbgp debugging protocol.

The debugging information that Xdebug can provide includes the following contents:

  • Stack and function traces in error message [2] 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 [3]
  • Code coverage analysis
  • Debug script functions interactively with the debugger front end. [4]

The above is from wiki

Let’s do it

Environment introduction

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.

This setPHP 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

Configure Xdebug

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

About thisxdebug.remote_hostThis IP is not just for granted127.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 the127.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 fileindex.php, getSERVERVariableREMOTE_ADDRProperty.

##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.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Configure docker

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

Configuring phpstorm

When the above is ready, we will start our play, configurationPHPStorm.

1. WillDocker Remote APIConfiguration on phpstorm

Since my environment is equipped with VBox and NAT, the port is 42376. You need to configure your own environment.

BelowConnection SuccessfulThe configuration is successful. thereName:Write down the name and use it later.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

2. Add PHPCli Interpreter.

Search: PHP language, click on the far right···Button

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

choiceFrom Dcoker...

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

thereServer:Name selectionStep 1The name of the dcoker server for.Image name:Choose your PHP container.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

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 boxPHP version:

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Here is the display effect after configuration,PHP language level:Best and PHP container consistent

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

3. ConfigurationDeployment

This configuration is used to let ide know the running environment of PHP code and the directory mapping relationship between local PHP code.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Choose hereLocal or mounted flolderFirst, I’ll let you type inNew server name:

What I input isbs. You can also enter others, but all of them should be recorded. This name will be used later.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

hereFolder:Configure the file path of phpstorm runtime environment. (that is, the MAC file path.)

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Here we look at the PHP container code path

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Cut toDeployemntConfiguredMappingTab, inDeploment path:Fill in the code path in the container,Local path:Select the local environment code path.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Configuration 4PHP > ServerHere, click the import button in the circle. Select the new one in the pop-up boxDeploymentto configure. (it’s just enteredNew server name

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

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, clickOK.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Don’t worry, close the pop-up box, and then confirmAbsolute 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.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Good phpstorm configuration is complete.

Docker runtime configuration

Don’t worry

In the docker runtime container, two more environment variables need to be configuredPHP_IDE_CONFIG, XDEBUG_CONFIG.

  1. serverNamewriteDeploymentThe name of the configuration.
  2. remote_hostFill in the IP of phpstorm environment, that is, in PHP$_SERVER['REMOTE_ADDR']The value of.
  3. remote_portThe default is 900
  • You need to be rightdocker-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 itdocker 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

Start debugging

1. Enable Xdebug listening and click the red phone below.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

Now it’s time to start listening.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

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)

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

3. Start visiting the web site.

The first breakpoint here is at the red arrow by clicking the green buttonStep 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 alsoVariablesSee the defined variable$confThe value of.

The most complete PHP tutorial in the history of PHP storm Xdebug breakpoint debugging docker environment

epilogue

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.

reference resources

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)