LoginSignup
2
4

More than 5 years have passed since last update.

Xdebug+VSCodeで再びハマったメモ。vagrant+virtualbox+ubuntu上のdocker上のphp+nginx

Last updated at Posted at 2018-09-05

概要

以前にxdebugを有効化したが、
バージョンを色々上げたら動かなくなった話。
動くようになったので、以下に動いた状態の設定を記す

動作環境

  • windows10
  • vagrant2.1.2
  • virtualbox5.2.18
  • ubuntu-18.04
  • Docker version 18.06.0-ce, build 0ffa825
  • docker-compose version 1.22.0, build f46880fe

設定

ホスト:Visual Studio Code の PHP DEBUGの設定

.vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9001,
      "pathMappings": {
        "/var/www/html": "${workspaceRoot}/src"
      }
    }
  ]
}

ゲスト: nginxサーバの設定

server/nginx/server.conf
server {
    listen 80 default;
    server_name localhost;
    root /var/www/html;
    index index.php index.html index.htm;
    charset utf-8;
    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    # access_log /data/access.log;
    # error_log  /data/error.log;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
        # フォルダ名に合わせてphpfpmの部分を書き換える
        #fastcgi_pass phpfpm_php7_1:9000;

        # xdebugの初期ポートと競合する
        fastcgi_pass php7:9000;

        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}
server/php7/Dockerfile
FROM php:7-fpm
RUN apt-get update 
RUN docker-php-ext-install pdo_mysql

RUN pecl install xdebug
RUN echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug_settings.ini 

RUN echo '\
log_errors = On\n\
error_log =/dev/stderr\n\
error_reporting = E_ALL\n\
display_errors = On\n\
' >> /usr/local/etc/php/conf.d/docker.ini

環境変数でXdebugの設定を行うようにした。 remote_host=10.0.2.2の設定が必要になった。

docker-compose.yml
version: "3"
services:
  # サーバー
  nginx: 
    build: ./server/nginx
    ports: 
      - 80:80
    volumes:
      - ./server/nginx/server.conf:/etc/nginx/conf.d/server.conf
      - ./src:/var/www/html

  # php
  php7: 
    build: ./server/php7
    volumes:
      - ./src:/var/www/html
      - ./server/php7/php.ini:/usr/local/etc/php/php.ini
    environment:
      # 9000番はfast-cgiが使っているので1つずらす
      XDEBUG_CONFIG: "remote_enable=1 remote_autostart=1 remote_port=9001 remote_host=10.0.2.2"

  # データベースへアクセス
  adminer:
    image: adminer
    ports: 
      - "8081:8080"

  # データベース
  db:
    build: ./server/mysql
    volumes:
      - dbdata:/var/lib/mysql
      - /etc/localtime:/etc/localtime:ro # 時刻の同期
      - ./server/mysql/init:/docker-entrypoint-initdb.d # 初期実行
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: "password"
      TZ: "Asia/Tokyo"

volumes:
  dbdata: # 名前付きボリューム名

この時点のソース

参考

XDEBUG EXTENSION FOR PHP | DOCUMENTATION
stack over flow
Debug your PHP

2
4
0

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
2
4