LoginSignup
5
5

More than 3 years have passed since last update.

Vagrant + Virtualbox + Docker + Laradoc + Laravel + VSCode + XDebug の 環境を設定したメモ。

Last updated at Posted at 2019-10-07

概要

docker+laravel+VSCode+Xdebugという開発環境を作る」を参考に laradock で作った環境にPHP Debugでアクセスしたかったが、案外ハマった。xdebug.remote_host=10.0.2.2と設定しなければならないところを適当な値にしてしまっていたのが原因。host.docker.internalはwindows用であった(*注

環境

  • Windows 10 Home
  • Vagrant 2.2.5
  • virtualbox 6.0.12
  • Ubuntu 18.04 LTS (Bionic Beaver)
  • Docker version 19.03.2, build 6a30dfc
  • docker-compose version 1.24.1, build 4667896b
  • VSCode 38.1
  • Laradoc
  • Laravel 6.0

ディレクトリ構成

※参考github

- .vscode
  - extensions.json
  - launch.json
  - settings.json
- laravel_docker
  - laradock #githubからcloneしたもの
    - .env
    - php-fpm
      - xdebug.ini
    - workspace
      - xdebug.ini
  - sampleapp # プロジェクトのディレクトリ
    - app
      - Http
        - Middleware
          - EncryptCookies.php
      - public
        - index.php # ブレークポイントを試しにしかける場所

ソース修正

XDebug のインストール

laravel_docker/laradock/.env
... 

WORKSPACE_INSTALL_WORKSPACE_SSH=false
WORKSPACE_INSTALL_SUBVERSION=false
- WORKSPACE_INSTALL_XDEBUG=false
+ WORKSPACE_INSTALL_XDEBUG=true
WORKSPACE_INSTALL_PHPDBG=false
WORKSPACE_INSTALL_SSH2=false
...
PHP_FPM_INSTALL_MEMCACHED=false
- PHP_FPM_INSTALL_XDEBUG=false
+ PHP_FPM_INSTALL_XDEBUG=true
PHP_FPM_INSTALL_XHPROF=false
...

workspaceとphp-fpmのxdebugの設定は同一とする。
remote_host10.0.2.2となる。

ゲスト OS 側からホスト OS を認識したい場合は 10.0.2.2 でアクセス可能。(VirtualBox ではホスト OS のネットワークアダプタに NAT を利用して、IP アドレス 10.0.2.2 を割り当てるのがデフォルトの設定。*注)

laravel_docker/laradock/php-fpm/xdebug.ini
-; xdebug.remote_host=dockerhost
-xdebug.remote_connect_back=1
-xdebug.remote_port=9000
-xdebug.idekey=PHPSTORM
+xdebug.remote_host=10.0.2.2 
+xdebug.remote_connect_back=0
+xdebug.remote_port=9001
+xdebug.idekey=Listen for XDebug

-xdebug.remote_autostart=0
-xdebug.remote_enable=0
-xdebug.cli_color=0
+xdebug.remote_autostart=1
+xdebug.remote_enable=1
+xdebug.cli_color=1
 xdebug.profiler_enable=0
 xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling"
laravel_docker/laradock/workspace/xdebug.ini
-; xdebug.remote_host=dockerhost
-xdebug.remote_connect_back=1
-xdebug.remote_port=9000
-xdebug.idekey=PHPSTORM
+xdebug.remote_host=10.0.2.2 
+xdebug.remote_connect_back=0
+xdebug.remote_port=9001
+xdebug.idekey=Listen for XDebug

-xdebug.remote_autostart=0
-xdebug.remote_enable=0
-xdebug.cli_color=0
+xdebug.remote_autostart=1
+xdebug.remote_enable=1
+xdebug.cli_color=1
 xdebug.profiler_enable=0
 xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling"

設定したら、コンテナを作り直す。

bin/container_build.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)
parent_dir=$(cd $bin_dir/.. && pwd)
docker_dir=$(cd $parent_dir/laravel_docker/laradock && pwd)
cd $docker_dir
docker-compose build php-fpm workspace

VSCode側の設定

.vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9001,
      "pathMappings": {
        "/var/www": "${workspaceFolder}/laravel_docker/sampleapp"
      },
      "log": true
    }
  ]
}
.vscode/extensions.json
{
  "recommendations": [
    "felixfbecker.php-debug"
  ],
  "unwantedRecommendations": []
}

例外でデバッグが見にくくならないように設定。

adminerなど、同梱されているツールでcookieを使うことがあるが、Laravelの暗号化機能がそれを認識すると暗号化不可の例外だらけになってしまう。除外しておく。

laravel_docker\sampleapp\app\Http\Middleware\EncryptCookies.php
<?php
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;

class EncryptCookies extends Middleware
{
    protected $except = [
        'XDEBUG_SESSION', 'adminer_version', 'adminer_sid', 'adminer_key', 'adminer_permanent', '_ga'
    ];
}

動作確認

ソースの行番号の左側を押して、ブレークポイントを設定する。
虫のアイコンからデバッグ画面にして、「Listen for XDebug」を選んで |> ボタンを押して実行。
ブラウザからララベルのページにアクセスしてブレークポイントで止まることを確認する。

image.png

この時点のソース

参考

Laravel Webアプリケーション開発 参考ソース
VSCode で xdebug を利用する on ローカル開発 with Laravel プロジェクト
docker+laravel+VSCode+Xdebug という開発環境を作る
homebrew で PHP+XDebug+VSCode の開発環境構築をやり直す
PhpStorm で Xdebug を使えるようにしよう!
docker+laravel+VSCode+Xdebugという開発環境を作る
Vagrant + VirtualBox note.
Docker v18.03以降でコンテナからホストに接続したい(Docker for Windows)

5
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5