使用Docker,nginx,PHP7-FPM和Xdebug进行IDE调试

时间:2018-08-11 19:47:38

标签: nginx docker-compose xdebug php-7.2 fpm

我目前正在使用docker-compose在macOS上准备开发堆栈,以便能够在 PHP7-FPM (端口)上使用 Xdebug (端口:9009) :9000)和 nginx (端口:80)服务器。

显然配置可以,但是我无法通过IDE进行调试。

这是我的设置:

我的.env文件:

APP_NAME=testeXdebug
HOST_SERVER_NAME=myapp
HOST_IP=docker.for.mac.localhost
# Use docker.for.mac.localhost - for OS X
# Use docker.for.win.localhost - for Windows

Dockerfile PHP7-FPM + Xdebug:

FROM php:7.2-fpm

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

docker-compose.yml文件:

version: '3.5'

services:
  web:
    image: nginx:1.15.2
    ports:
      - '80:80'
    volumes:
      - '.:/usr/share/nginx/html'
      - './config/default.conf:/etc/nginx/conf.d/default.conf'
      - '/tmp/${APP_NAME}/web:/var/log/nginx'
    env_file:
      - '.env'
    depends_on:
      - 'php-fpm'
    links:
      - 'php-fpm'

  php-fpm:
    build: './docker'
    ports:
      - '9000:9000'
      - '9009:9009'
    expose:
      - 9000
      - 9009
    volumes:
      - '.:/usr/share/nginx/html'
      - './config/php-dev.ini:/usr/local/etc/php/conf.d/php-dev.ini'
      - '/tmp/${APP_NAME}/php-fpm:/var/log/xdebug'
    environment:
      XDEBUG_CONFIG: "remote_host=${HOST_IP}"
      PHP_IDE_CONFIG: "serverName=${HOST_SERVER_NAME}"
    env_file:
      - '.env'

php-dev.ini文件:

; Xdebug
xdebug.default_enable = 1
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.remote_port = 9009
xdebug.profiler_enable = 0
xdebug.idekey = PHPSTORM
xdebug.remote_handler = dbgp
xdebug.remote_mode = req
xdebug.remote_log = /var/log/xdebug/xdebug.log

nginx default.conf文件:

server {
    listen          80;
    server_name     myapp;
    root            /usr/share/nginx/html;
    index           index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass    php-fpm:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

然后在扩展名为Xdebug helper的情况下通过浏览器访问服务器时:

enter image description here

我得到此调试日志(xdebug):

Log opened at 2018-08-11 19:22:53
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 192.168.128.1:9009.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///usr/share/nginx/html/index.php" language="PHP" xdebug:language_version="7.2.8" protocol_version="1.0" appid="9" idekey="PHPSTORM"><engine version="2.6.1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2018 by Derick Rethans]]></copyright></init>

-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>

Log closed at 2018-08-11 19:22:53

nginx访问日志:

192.168.128.1 - - [11/Aug/2018:18:57:25 +0000] "GET /favicon.ico HTTP/1.1" 200 94197 "http://docker.for.mac.localhost/index.php?XDEBUG_SESSION_START=PHPSTORM" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
192.168.128.1 - - [11/Aug/2018:19:22:53 +0000] "GET /index.php?XDEBUG_SESSION_START=PHPSTORM HTTP/1.1" 200 94341 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
192.168.128.1 - - [11/Aug/2018:19:22:53 +0000] "GET /favicon.ico HTTP/1.1" 200 94205 "http://docker.for.mac.localhost/index.php?XDEBUG_SESSION_START=PHPSTORM" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
192.168.128.1 - - [11/Aug/2018:19:30:12 +0000] "GET /_intellij_phpdebug_validator.php HTTP/1.1" 200 516 "-" "Java/1.8.0_152-release" "-"

并使用以下调试扩展程序设置VSCode:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9009,
            "pathMappings": {
                "/usr/share/nginx/html": "${workspaceRoot}"
            }
        }
    ]
}

添加一些断点:

enter image description here

PhpStorm 上:

enter image description here

使用docker-compose的PhpStorm CLI解释器:

enter image description here

但是在开始侦听PHP调试连接时,我得到了Port 9009 is busy

IDE永远不会启动调试工具... =(
我可能会错过什么?请帮帮我!


评论后:

ports删除 php-fpm 服务的exposedocker-compose.yml设置时

thiago@MA-TPR testeXdebug $ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                NAMES
735fc48fad63        nginx:1.15.2          "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp   testexdebug_web_1
b9b16af98fb5        testexdebug_php-fpm   "docker-php-entrypoi…"   4 minutes ago       Up 4 minutes        9000/tcp             testexdebug_php-fpm_1
thiago@MA-TPR testeXdebug $ 

并从xdebug获取此日志:

Log opened at 2018-08-12 00:56:39
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 192.168.160.1:9009.
W: Creating socket for '192.168.160.1:9009', poll success, but error: Operation now in progress (29).
E: Could not connect to client. :-(
Log closed at 2018-08-12 00:56:39

从PHP容器执行nc

root@b9b16af98fb5:/var/www/html# nc -zv docker.for.mac.localhost 9009
Warning: inverse host lookup failed for 192.168.65.2: Unknown host
docker.for.mac.localhost [192.168.65.2] 9009 (?) open
root@b9b16af98fb5:/var/www/html# 

2 个答案:

答案 0 :(得分:3)

  

但是在开始侦听PHP Debug连接时,我得到了Port 9009 is busy

根本不需要在Docker容器中公开Xdebug端口。

如果公开它。Docker将是侦听该端口并将任何连接转发到容器的端口。但是必须要监听它的是IDE / VSCode / PhpStorm ...,因为它是连接到IDE的Xdebug,而不是其他方式

首先解决。

  

xdebug.remote_connect_back = 1

我建议关闭此功能,并在xdebug.remote_hostdocker.for.mac.localhost)中指定实际的主机。

Xdebug使用remote_connect_back选项检测到的IP(取决于我猜的设置以及Docker的工作方式)很可能不是主机的IP。但这就是您的需要-那是您的IDE(PhpStorm)/编辑器(VSCode)运行的地方,并且Xdebug必须连接到该地方。

答案 1 :(得分:0)

如果您使用的是“ Docker for Mac”,那么在xdebug配置文件中,您也可以使用以下代码:

xdebug.remote_host = host.docker.internal
  

我想从集装箱连接到主机上的服务
  主机的IP地址正在更改(如果没有网络访问权限,则没有IP地址)。从18.03开始,我们的建议是连接到特殊的DNS名称host.docker.internal,它将解析为主机使用的内部IP地址。这是出于开发目的,不适用于Docker for Mac以外的生产环境。

该网关也可以通过gateway.docker.internal访问。