Use xdebug to debug php code in docker, quick start tutorial for xdebug configuration, and solutions to common pitfalls during configuration and debugging.

There is a difference between using xdebug to debug the configuration of php code in docker and using xdebug to debug local code or the configuration of code on a remote server. The following tutorial is to take you quickly to use xdebug to debug the code in docker and help you avoid some pit.

Prerequisite: The code in your docker can be run and accessed normally.

1. Install xdebug in xdbug.

非常棒的安转指导网址: https://xdebug.org/wizard.php   ,这是个非常棒的安装指导网站,只要按照上面一步一步走都能成功安装的,缺点就是都是英文,本文着重讲怎么配置. 怎么安装的其实那个网站已经列得很详细也很简单, 大致步骤就是:将echo phpinfo();输出的整个内容复制到该网站中,该网站会解析你的phpinfo信息,并提供合适xdebug给你下载,你下载xdebug后解压并执行make && make install, 此时就会生成个xdebug.so文件, 然后在php.ini文件中引入该文件即是安转成功.

2. xdebug configuration (this article will focus on the introduction)

 2.1 In docker], enter the php configuration file php.ini and add the following configuration: 

;引入xdebug扩展
zend_extension="/usr/local/php7/lib/php/20170718/xdebug.so"
;开启远程调试
xdebug.remote_enable=1
;据说connect_back开启会有问题
;xdebug.remote_connect_back=1
;关键配置.该地址为你宿主机(也就是你电脑)的ip地址
xdebug.remote_host=192.168.43.219
xdebug.remote_handler=dbgp
;xdebug.remote_mode="req"
;关键.端口可以自己设置但是注意要选择没被占用的.后面phpstormde的prot要填写这个端口号
xdebug.remote_port=9050
;后面phpstorm的idekey的值要和这个一样
xdebug.idekey=PHPSTORM
xdebug.remote_log=/var/log/xdebug.log

Remember to reload php-fpm to make the configuration effective.

2.2 Configuration on the host. Enter the settings of phpstorm (the entry on the mac is phpstorm->preferences, and the window seems to be called settings). Set port 9050

Set idekey to PHPSTORM

Add a server (here assume that your code access URL is www.test.com and the port is the default 80)

Configure a debug configuration

2.3 Start debugging, after you visit http://www.test.com?XDEBUG_SESSION_START=PHPSTORM on your browser, you can enter the breakpoint you hit. 

If you visit without? XDEBUG_SESSION_START=Does the PHPSTORM parameter trigger the debugging mode? If you think it is troublesome to bring this parameter every time, Google Chrome can go to the next xdebug helper (its role is actually to put it on the cookie when you visit the website That string of parameters)

 At this point, you can happily xdebug.

3. Common problems

 3.1 When the time is too long, a 502 error occurs?

  The 502 error is a php error. Generally, it exceeds the maximum execution time of the php script. You can set the php-fpm configuration and set the request_terminate_timeout field to a longer point.

 3.2 There is a 504 error?

  First of all, it is clear that the error is an error reported by nginx, so the problem is that it appears on nginx. 
  Enter docker, and add the following to the http block on the nginx configuration file :

#其中read_timeout是关键,send_timeout和connect_timeout不配置一般也是可以的
fastcgi_read_timeout 3600s; 
fastcgi_send_timeout 3600s; 
fastcgi_connect_timeout 3600s;

  Remember to restart or reload nginx, otherwise it will take effect.

   In this case, nginx is fastcgi_pass (fastcgi forwarding), and in another case, nginx is used as proxy_pass. (This kind of configuration proxy_read_timeout 3600s; otherwise it will time out)

   Like nginx as proxy_pass, let me give an example. For example, the project on my docker is in nginx + php-fpm mode. Can monitor port 85, so I don’t want to bring the port number every time I visit the docker project on the host, I want to visit www.test85.com to access the project in docker, so you need to add it on the host at this time Layer nginx, and then configure all www.test85.com requests to proxy_pass to port 85. For this reason, for xdebug debugging in this case, two nginx may have a 504 timeout. So the http block of nginx on the host Proxy_read_timeout 3600s should be added;  and fastcgi_read_timeout 3600s should be added to nginx on docker to prevent their timeouts respectively.   I hope you can better understand how to configure the nginx timeout 504 error in this example. It will be more clear.

Guess you like

Origin blog.csdn.net/weixin_37281289/article/details/106178718