PhpStorm+Docker+php7+Xdebug奇葩说

Xsdebug运行原理

当浏览器输入http://192.168.1.186/index.php?XDEBUG_SESSION_START=13128,当PHP服务器的Xdebug模块检测到请求URL中带有XDEBUG_SESSION_START参数时,则将该请求的响应挂起,并自动进入调试模式。此时,Xdebug将会尝试以指定的协议(一般是DBGP)与Xdebug配置中指定的地址xdebug.remote_host:xdebug.remote_port建立连接。在新版本中会将参数写入cookie,url中删除一样可以触发debug。

大神原理论:http://www.softown.cn/post/117.html

PhpStorm+Docker+php7+Xdebug奇葩说_第1张图片
原理图

Dockerfile(opencart)

FROM php:7.1.5-fpm
#FROM php:5.6.30-fpm
MAINTAINER Mark Dthan <[email protected]>

#阿里云镜像加速
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak
COPY sources-jessie.aliyun /etc/apt/sources.list

RUN apt-get update && apt-get install -y --no-install-recommends \
    ntp \
    libfreetype6-dev \
    libicu-dev \
    libjpeg62-turbo-dev \
    libmcrypt-dev \
    libpng12-dev \
&& rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-configure \
    gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/

#安装扩展
RUN docker-php-ext-install \
    gd \
    mbstring \
    mcrypt \
    zip \
    mysqli

RUN yes | pecl install xdebug

COPY conf/docker-php-ext-xdebug.ini /usr/local/etc/php/conf.d/

#RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=1.0.0-alpha11

ENV PHP_MEMORY_LIMIT 2G
ENV PHP_PORT 9000
ENV PHP_PM dynamic
ENV PHP_PM_MAX_CHILDREN 10
ENV PHP_PM_START_SERVERS 4
ENV PHP_PM_MIN_SPARE_SERVERS 2
ENV PHP_PM_MAX_SPARE_SERVERS 6
ENV APP_MAGE_MODE default

COPY conf/php.ini /usr/local/etc/php/
COPY conf/php-fpm.conf /usr/local/etc/
COPY bin/* /usr/local/bin/ 

WORKDIR /www

CMD ["/usr/local/bin/start"]

conf/php.ini

memory_limit = PHP_MEMORY_LIMIT

conf/php-fpm.conf

; This file was initially adapated from the output of: (on PHP 5.6)
;   grep -vE '^;|^ *$' /usr/local/etc/php-fpm.conf.default

[global]

error_log = /proc/self/fd/2
daemonize = no

[www]

; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/2

user = www-data
group = www-data

listen = [::]:PHP_PORT

pm = PHP_PM
pm.max_children = PHP_PM_MAX_CHILDREN
pm.start_servers = PHP_PM_START_SERVERS
pm.min_spare_servers = PHP_PM_MIN_SPARE_SERVERS
pm.max_spare_servers = PHP_PM_MAX_SPARE_SERVERS

env[MAGE_MODE] = APP_MAGE_MODE

clear_env = no

; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes

conf/docker-php-ext-xdebug.ini

[xdebug]
#日期看编译过程,可以通过docker映射进去,方便修改参数
zend_extension="zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so"
xdebug.remote_enable = on
xdebug.remote_connect_back = on
xdebug.remote_handler = dbgp
xdebug.remote_port = 9001
xdebug.remote_host = 192.168.1.186            #远程主机ip,默认localhost,指向docker容器本身[关键]
xdebug.idekey = PHPSTORM

bin/start

#!/bin/bash
[ ! -z "${PHP_MEMORY_LIMIT}" ]         && sed -i "s/PHP_MEMORY_LIMIT/${PHP_MEMORY_LIMIT}/" /usr/local/etc/php/php.ini
[ ! -z "${PHP_PORT}" ]                 && sed -i "s/PHP_PORT/${PHP_PORT}/" /usr/local/etc/php-fpm.conf
[ ! -z "${PHP_PM_MAX_CHILDREN}" ]      && sed -i "s/PHP_PM_MAX_CHILDREN/${PHP_PM_MAX_CHILDREN}/" /usr/local/etc/php-fpm.conf
[ ! -z "${PHP_PM_START_SERVERS}" ]     && sed -i "s/PHP_PM_START_SERVERS/${PHP_PM_START_SERVERS}/" /usr/local/etc/php-fpm.conf
[ ! -z "${PHP_PM_MIN_SPARE_SERVERS}" ] && sed -i "s/PHP_PM_MIN_SPARE_SERVERS/${PHP_PM_MIN_SPARE_SERVERS}/" /usr/local/etc/php-fpm.conf
[ ! -z "${PHP_PM_MAX_SPARE_SERVERS}" ] && sed -i "s/PHP_PM_MAX_SPARE_SERVERS/${PHP_PM_MAX_SPARE_SERVERS}/" /usr/local/etc/php-fpm.conf
[ ! -z "${APP_MAGE_MODE}" ]            && sed -i "s/APP_MAGE_MODE/${APP_MAGE_MODE}/" /usr/local/etc/php-fpm.conf

# Make sure this line comes last, otherwise find/replace will replace above vars
[ ! -z "${PHP_PM}" ]                   && sed -i "s/PHP_PM/${PHP_PM}/" /usr/local/etc/php-fpm.conf

# Start the cron service
#/usr/sbin/cron

# Start the php-fpm service
/usr/local/sbin/php-fpm

sources-jessie.aliyun

deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib

nginx配置

# $document_root换成WORKDIR(/www)
location ~ \.php$ {
            root           /usr/local/codes/opencart;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /www$fastcgi_script_name;
            include        fastcgi_params;
        }

PhpStorm端口设置(和上面配置一致)

PhpStorm+Docker+php7+Xdebug奇葩说_第2张图片

DBGp设置(端口和浏览器端口一致)

PhpStorm+Docker+php7+Xdebug奇葩说_第3张图片

Servers配置

  • ip和xdebug一致,这里比较奇葩,填127.0.0.1无法进入调试模式
  • 项目映射,左侧项目根目录,右侧和docker的WORKDIR一致,否则也无法进入调试模式
    PhpStorm+Docker+php7+Xdebug奇葩说_第4张图片

调试配置

PhpStorm+Docker+php7+Xdebug奇葩说_第5张图片

启动调试模式

  • 点击瓢虫自动跳转浏览器,url会带调试参数。若没有参数检查配置是否有出入

最后

  • 折腾了好久好久了,终于看到曙光了
  • 本人菜鸟,大神路过带一带
  • 若有误导欢迎指正

你可能感兴趣的:(PhpStorm+Docker+php7+Xdebug奇葩说)