Skip to content

Commit

Permalink
Merge pull request #264 from khs1994-docker/dev
Browse files Browse the repository at this point in the history
Update xdebug case #260
  • Loading branch information
khs1994 authored Mar 18, 2018
2 parents fbd2627 + baf0b1e commit 343c3b5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
16 changes: 13 additions & 3 deletions bin/lnmp-php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ fi

DIR="$( cd "$( dirname "$0" )" && pwd )"

if ! [ -f $DIR/../logs/php-fpm/cli_error.log ];then
touch $DIR/../logs/php-fpm/cli_error.log
fi

. $DIR/.arch

. $DIR/../.env

# -S 参数需要容器暴露端口

S_ERROR(){
Expand All @@ -41,21 +47,25 @@ if [ "$1" = '-S' ];then
exec docker run -it --rm \
--mount type=bind,src=$PWD,target=/app,consistency=cached \
--mount src=lnmp_composer_cache-data,target=/tmp/cache \
--mount type=bind,src=$DIR/../config/php/${PHP_INI:-php.development.ini},target=/usr/local/etc/php/php.ini \
--mount type=bind,src=$DIR/../logs/php-fpm/cli_error.log,target=/var/log/php-fpm/php_errors.log \
--network lnmp_backend \
-e APP_ENV=${APP_ENV} \
-p $ADDR:$PORT:$PORT \
${DOCKER_IMAGE}:${TAG} \
php -S 0.0.0.0:$PORT "$@"
php -d zend_extension=xdebug -d error_log=/var/log/php-fpm/php_errors.log -S 0.0.0.0:$PORT "$@"
fi

exec docker run -it --rm \
--mount type=bind,src=$PWD,target=/app,consistency=cached \
--mount src=lnmp_composer_cache-data,target=/tmp/cache \
--mount type=bind,src=$DIR/../config/php/${PHP_INI:-php.development.ini},target=/usr/local/etc/php/php.ini \
--mount type=bind,src=$DIR/../logs/php-fpm/cli_error.log,target=/var/log/php-fpm/php_errors.log \
--network lnmp_backend \
-e APP_ENV=${APP_ENV} \
${DOCKER_IMAGE}:${TAG} \
php $COMMAND
php -d zend_extension=xdebug -d error_log=/var/log/php-fpm/php_errors.log $COMMAND

if ! [ -z "$CREATE" ];then
docker network rm lnmp_backend
docker network rm lnmp_backend > /dev/null 2>&1
fi
11 changes: 10 additions & 1 deletion bin/lnmp-phpunit
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ docker network inspect lnmp_backend > /dev/null 2>&1 || (docker network create l

DIR="$( cd "$( dirname "$0" )" && pwd )"

if ! [ -f $DIR/../logs/php-fpm/cli_error.log ];then
touch $DIR/../logs/php-fpm/cli_error.log
fi

source $DIR/.arch

. $DIR/../.env

docker run -it --rm \
--mount type=bind,src=$PWD,target=/app,consistency=cached \
--mount src=lnmp_composer_cache-data,target=/tmp/cache \
--mount type=bind,src=$DIR/../config/php/${PHP_INI:-php.development.ini},target=/usr/local/etc/php/php.ini \
--mount type=bind,src=$DIR/../logs/php-fpm/cli_error.log,target=/var/log/php-fpm/php_errors.log \
--network lnmp_backend \
-e APP_ENV=testing \
${DOCKER_IMAGE}:${TAG} \
vendor/bin/phpunit "$@"
vendor/bin/phpunit -d zend_extension=xdebug -d error_log=/var/log/php-fpm/php_errors.log "$@"

if ! [ -z "$CREATE" ];then
docker network rm lnmp_backend
Expand Down
22 changes: 17 additions & 5 deletions config/php/php/conf.d/xdebug.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# 每次升级 PHP 版本注意更新该路径
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
; 每次升级 PHP 版本注意更新该路径
; zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
zend_extension=xdebug
xdebug.remote_enable=on
; 监听 客户端 端口,为防止混乱禁止填写 9000 端口
xdebug.remote_port=9001
# 监听 客户端 端口,为防止混乱禁止填写 9000 端口
; 关于该地址,禁止填写 127.0.0.1、localhost 等,请填写路由器分配的网址 如 192.168.1.* 、192.168.199.* 、
;
;
; 务必自行替换为自己的地址
;
;
;
xdebug.remote_host=192.168.199.100
# 关于该地址,禁止填写 127.0.0.1、localhost 等,请填写路由器分配的网址 如 192.168.1.* 、192.168.199.* 、
; 也可以在容器没有设置 DNS 的情况下使用下面的地址,注意 daemon.json --dns 等设置 DNS ,会导致错误。
;
; xdebug.remote_host=host.docker.internal
;
; 如果启用该配置, xdebug.remote_host 设置将被忽略
; 警告!严禁修改该配置,我实际测试若设为 1 ,将不能启动调试,愿意探索的自己试
xdebug.remote_connect_back=0
# 如果启用该配置, xdebug.remote_host设置将被忽略
xdebug.remote_handler=dbgp
xdebug.remote_log=/var/log/php-fpm/xdebug-remote.log
4 changes: 3 additions & 1 deletion docs/phpunit.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 本项目使用 PHPUnit 最佳实践
# 本项目使用 PHPUnit in Docker + PHPStorm 最佳实践

按照 [步骤](command.md) 将命令加入 `PATH`

Expand All @@ -9,5 +9,7 @@ $ lnmp-composer require phpunit/phpunit

# $ lnmp-composer install

# 该目录必须有 PHPUnit 配置文件 phpunit.xml

$ lnmp-phpunit
```
44 changes: 43 additions & 1 deletion docs/xdebug.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
# xdebug in Docker (PhpStorm)
# 本项目使用 Xdebug in Docker + PhpStorm 最佳实践

## 准备 PHP 项目

* PHP 项目

* NGINX 配置

* 验证浏览器能通过网址访问到你的项目

## 设置 Xdebug

配置文件路径 `./config/php/php/conf.d/xdebug.ini`

> 这里仍以示例配置为例,如何正确的自定义配置请查看 [这里](config.md)
### 调试 HOST (必选项)

`xdebug.remote_host=192.168.199.100``192.168.199.100` 替换为你自己的 IP。这就要求你的电脑必须固定 IP。不然电脑 IP 每次变化时,必须重新修改配置文件。

在 Docker 没有配置 DNS 的情况下可以使用下面的配置

`xdebug.remote_host=host.docker.internal`

所以有以下几种情况

* Docker 没有配置 DNS 建议使用第二种配置方法

* 电脑拥有固定 IP,采用第一种方法,但由于网络环境(家、公司)不定,每次还是得修改 Xdebug 设置里的 IP

* 电脑 IP 不定,又想自定义 Docker DNS 配置,那就只能每次 IP 变化时重新修改 xdebug 设置里的 IP

### 调试端口(可选项)

`xdebug.remote_port=9001`

### 警告

其他选项严禁修改,除非你明确知道某个选项将产生什么影响。

## 重启容器

```bash
$ ./lnmp-docker.sh restart php7
```

## `IDE` 设置中配置 `Xdebug`

`Preferenences` -> `Languages & ...`-> `PHP` -> `Debug`-> `xdebug`-> `Debug port 9001`(默认为 `9000`,这里改为 `9001`)
Expand All @@ -20,6 +58,10 @@

* [Chrome](https://github.com/mac-cain13/xdebug-helper-for-chrome)

### 设置扩展

在扩展配置中 IDE key 选择 PhpStorm

## IDE 中点击工具栏电话图标

## 编写代码
Expand Down

0 comments on commit 343c3b5

Please sign in to comment.