Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to debug Docker project in PHPSTORM

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to debug the Docker project in PHPSTORM, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Basic usage of Docker

First of all, you need to know the two most basic things of Docker: image and container.

Image image, the most basic thing to build a Docker environment is image. It can be simply understood that this is the image in which the virtual machine was created.

Container container. That is, the virtual environment created according to image. It can be simply understood as a running virtual machine.

The configuration file for Docker is Dockerfile. The contents of this file are commands for building container. The basic commands are:

FROM sets the image on which container is running. It must be written at the beginning of Dockerfile. You can find the right image in Docker Hub.

RUN executes commands when image build. Generally used in the installation environment

CMD executes commands when container is first started. Commonly used to start services

COPY copies the host's files to container

WORKDIR sets the working directory. All commands work on the basis of this directory

Now that we know the above three commands, we can simply write a Dockerfile.

# basic image installs xdebug extension for php:7.3-apacheFROM php:7.3-apache# and opens RUN pecl install xdebug & &\ docker-php-ext-enable xdebug# to copy host phpinfo.php to container / var/www/html/phpinfo.php COPY. / phpinfo.php / var/www/html/phpinfo.php

Run in the directory of Dockerfile after writing.

Docker build-t test/testmyphp.

When the command is finished, enter the following command to see the created image

$docker image lsREPOSITORY TAG IMAGE ID .test / testmyphp latest 4931b92274f2.

To get container up and running, use the following command. Use-p to specify the mapping port, with the dormitory port on the left and the container port on the right

Docker run-p 81:80 test/testmyphp

View running container

$docker container lsCONTAINER ID IMAGE PORTS .10b8c28b2f69 test/testmyphp 0.0.0.0 80/tcp.

Enter the shell of container

$docker exec-it CONTAINER_ ID value bashroot@10b8c28b2f69:/var/www/html#

At this point, we have built the basic Docker environment. Let's take a look at how to get PHPSTORM to debug a Docker project.

Debugging mode 1-Docker mount debugging

The Dockerfile mentioned above is used for testing here, and it needs to be configured and added according to the actual situation.

Configure the Dockerfile# base image to install the xdebug extension for php:7.3-apacheFROM php:7.3-apache# and open RUN pecl install xdebug & &\ docker-php-ext-enable xdebug#! Use a startup script to start the service!! COPY. / start.sh / start.shRUN chmod + x / start.shCMD ["/ start.sh"]

Start.sh content

# configure Xdebugecho "xdebug.client_host = host.docker.internal" > > / usr/local/etc/php/conf.d/docker-php-ext-xdebug.iniecho "xdebug.client_port = 9003" > > / usr/local/etc/php/conf.d/docker-php-ext-xdebug.iniecho "xdebug.mode = debug" > > / usr/local/etc/php/conf.d/docker-php-ext-xdebug.iniecho "xdebug.max_nesting_level = 1000" > > / usr/local/etc / php/conf.d/docker-php-ext-xdebug.iniecho "xdebug.discover_client_host = true" > > / usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini# restart apache sshservice apache2 restart

Rebuild image:

# it's best to delete the previous value and then rebuild the docker image rm IMAGE value-fdocker build-t test/testmyphp.

Don't rush to run after the reconstruction, container. Locate the location of our project code first

Use Docker's Bind mounts technology. Map the project directory of the host to the website directory of container

Docker run-p 81:80-mount type=bind,source=/home/xp/test_docker/test_program,target=/var/www/html test/testmyphp

At this point, the services required for the debugging environment are installed, and then configure PHPSTORM.

Configure PHPSTORM

Open the project directly using Open

Configure directory mapping

Go to File-> Settings-> Languages & Frameworks-> PHP-> Servers. Configure directory mapping

Note that you must tick Use path mappings before you can configure directory mapping

Absolute path on the server needs to type the server path manually.

Set the Xdebug port

Go to File-> Settings-> Languages & Frameworks-> PHP-> Debug. Set Xdebug Debug port to 9003. Consistent with the php configuration.

Configure a Run/Debug Configuration

Click Add Configuration.... in the upper right corner of PHPSTORM. Enter the configuration surface and add a PHP Web Page. And make the following simple configuration (change the name to the url path)

Start PHP Debug Listening

Directly click on the small phone in the upper right corner of PHPSTROM to turn on monitoring.

Verification

Put a breakpoint on the php file and click the Beetle-style Debug button in the upper right corner. The breakpoint can be successful.

Debugging mode 2-ssh tunnel

This approach can be used not only on Docker environments, but also on remote servers. The disadvantage is that you have to install a lot of extra services in container.

Configure Dockerfile

Install the ssh service. PHPSTORM needs ssh for directory mapping, otherwise Debug will not succeed

Install xdebug. This is the basic extension component for debugging

Set ssh to allow root to log in. After all, it's just a docker debugging environment, so it won't be so troublesome. Of course, if it is an online business, of course, we should do a good job in the allocation of permissions.

Change the root password. After all, I can't connect if I don't know the password.

Start the ssh service

Restart the apache service

The Dockerfile mentioned above is used for testing here, and it needs to be configured and added according to the actual situation.

Modify the previous Dockerfile.

# basic image installs the ssh service RUN apt-get update for php:7.3-apacheFROM php:7.3-apacheENV APACHE_DOCUMENT_ROOT / var/www/html# & &\ apt-get install ssh-y# installs the xdebug extension and opens RUN pecl install xdebug & &\ docker-php-ext-enable xdebug# to copy the host's phpinfo.php to container / var/www/html/phpinfo.php COPY. / phpinfo.php / var/www/html/phpinfo.php#!! Use a startup script to complete the work of process 3 4 5!! COPY. / start.sh / start.shRUN chmod + x / start.shCMD ["/ start.sh"]

Start.sh content

#! / bin/bash# set ssh to allow root to log in to echo 'PermitRootLogin yes' > > / etc/ssh/sshd_config# to modify root password echo root:123456 | chpasswd# configure Xdebug. The configuration of Xdebug 3 is as follows Different from Xdebug 2 echo "xdebug.client_host = host.docker.internal" > > / usr/local/etc/php/conf.d/docker-php-ext-xdebug.iniecho "xdebug.client_port = 9003" > > / usr/local/etc/php/conf.d/docker-php-ext-xdebug.iniecho "xdebug.mode = debug" > > / usr/local/etc/php/conf.d/docker-php-ext-xdebug.iniecho "xdebug.max_nesting_level = 1000" > > / usr / local/etc/php/conf.d/docker-php-ext-xdebug.iniecho "xdebug.discover_client_host = true" > > / usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini# restart apache sshservice apache2 restartservice ssh restart# to add this Otherwise, the container will stop when it is finished. Sleep infinity

Rebuild image:

# it's best to delete the previous value and then rebuild the docker image rm IMAGE value-fdocker build-t test/testmyphp.

Start container:

Docker run-p 81:80-p 2222 22 test/testmyphp

At this point, the services required for the debugging environment are installed, and then configure PHPSTORM.

Configure PHPSTORM

Open PHPSTORM and create a new Project. Select New Project from Existing Files

Select Web server is on remote host.

Next all the way. Go to configure Remote Server

And then Next all the way.

When the configuration is complete, you will be able to see our project.

Code debugging cannot be done directly at this time. We need to do the following work

Configure CLI Interpreter

Configure directory mapping

Set the Xdebug port

Configure a Run/Debug Configuration

Start PHP Debug Listening

Ps: since some of the steps are the same, just pull the screenshot above.

Configure CLI Interpreter

Go to File-> Settings-> Languages & Frameworks-> PHP. Set up CLI Interpreter

Create a new CLI Interpreter. Select From Docker, Vagrant, VM, WSL,Remote....

Here we can enter SSH, or we can select Docker directly. I use SSH here.

Sets the PHP executable path. If you don't know, you can enter container and use whereis php to search.

Configure directory mapping

Go to File-> Settings-> Languages & Frameworks-> PHP-> Servers. Configure directory mapping

Note that you must tick Use path mappings before you can configure directory mapping

Absolute path on the server needs to type the server path manually.

Set the Xdebug port

Go to File-> Settings-> Languages & Frameworks-> PHP-> Debug. Set Xdebug Debug port to 9003. Consistent with the php configuration.

Configure a Run/Debug Configuration

Click Add Configuration.... in the upper right corner of PHPSTORM. Enter the configuration surface and add a PHP Web Page. And make the following simple configuration (change the name to the url path)

Start PHP Debug Listening

Directly click on the small phone in the upper right corner of PHPSTROM to turn on monitoring.

Verification

Put a breakpoint on the php file and click the Beetle-style Debug button in the upper right corner. The breakpoint can be successful.

Extension-remote server debugging

This can be extended to configure if the debugging target is a remote server instead of a local Docker.

In fact, it is the same as the above steps, but we need to modify the traffic monitored by Xdebug.

Briefly talk about the principle of Xdebug snooping:

When php-xdebug receives a request with XDEBUG_SESSION_START, it will send the current Debug message to client_host and cilent_port configured in xdebug.

In our start.sh startup file, the settings are as follows:

Xdebug.client_host = host.docker.internalxdebug.client_port = 9003

The host.docker.internal value in Xdebug automatically sets the ip on the request side to the debug side, that is, automatically sends Debug information to any request IP.

The value of xdebug.client_port is the port to which the Debug information is sent.

We can imagine that locally, xdebug can access our client's ip and port, because they are all on the same local area network.

However, if you are debugging a remote server, the public network server cannot send Debug information to the debugger due to public network and private network reasons (unless the debugger is also on the public network. )

So we need to set xdebug.client_host to 127.0.0.1 to have xdebug forward Debug information to local port 9003.

Then the ssh tunnel is used for port forwarding, mapping the port 9003 monitored by the debugger to the 9003 on the server. In this way, you can receive the Debug message forwarded by xdebug. This can be done using the ssh tunnel.

Let's test it, first modify the php.ini. Let the xdebug.client_ host value be 127.0.0.1

Ssh tunnel port forwarding

Ssh-N-R remote IP: remote port: 127.0.0.1 root@ 9003 remote IP on how to debug the Docker project in PHPSTORM is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report