sublime text 에디터에 xdebug 설치하고 원격 디버깅하는 방법.

 

사전 작업

Sublime text plugin 설치

  1. Ctrl-Shift-P Package Control: Install Package
     
  2. Xdebug client 설치
     
  3. 섭라임 재시작

 

PHP 서버 설정

  1. xdebug 모듈 설치(Windows RHEL/CentOS 리눅스에 xdebug 설치 참고)

 

xdebug 주요 설정

  • remote_host : xdebug 가 구동되는 서버에 연결할 ip. IDE 를 사용하는 개발자 PC 의 ip 를 적어주면 된다.
  • remote_port : 개발자 PC 에 xdebug client 가 떠 있는 포트. 기본 설정은 9000 이지만 이 포트를 사용하는 프로그램들이 가끔 있으므로 9001등 별도의 포트 권장
  • idekey : PHP 서버의 xdebug 를 함부로 띄울수 없게 구동시 브라우저에 다음과 같은 파라미터를 구동해야 함. 이때 사용할 key로 아무 문자열이나 설정하면 됨
    • XDEBUG_SESSION_START=idekey
    • XDEBUG_SESSION_STOP=idekey
  • remote_autostart : XDEBUG_SESSION_START 로 띄우지 않고 기본적으로 php 구동시 xdebug 를 자동 구동. 보안에 취약하므로 localhost 에서만 사용하고 원격 호스트는 사용하지 말 것.

 

디버깅 환경 구성

vagrant 에서 Homestead 사용

  1. sudo vi /etc/php5/fpm/conf.d/20-xdebug.ini

    zend_extension=xdebug.so
    xdebug.remote_enable = 1
    xdebug.remote_connect_back = 1
    xdebug.remote_host=10.0.2.2
    xdebug.remote_port = 9001
    xdebug.max_nesting_level = 250
    xdebug.idekey=sublime.xdebug
    xdebug.remote_log="/var/log/xdebug/xdebug.log"
    CODE
  2. php5-fpm 재구동

    sudo service php5-fpm restart
    CODE

 

Local 웹 서버에서 사용

XAMPP, MAPP 등 APM 스택 사용시 설정

  1. php.ini 에 다음 내용 추가

    [xdebug]
    zend_extension = "D:\devel\php-5.6.8-Win32-VC11-x86\ext\php_xdebug-2.3.2-5.6-vc11.dll"
    ;zend_extension = /absolute/path/to/your/xdebug-extension.so
    xdebug.remote_enable = 1
    xdebug.remote_autostart = 1
    xdebug.remote_host = "127.0.0.1"
    xdebug.remote_port = 9001
    xdebug.max_nesting_level = 250
    CODE
    localhost 에서 사용시 xdebug.remote_autostart =1 로 설정하지 않으면 xdebug 가 제대로 기동되지 않았음.
  2. PHP 재시작 (웹 서버 또는 php-fpm)

    sudo service httpd restart
    sudo service nginx restart
    sudo service php5-fpm restart
    CODE

설정 확인

  1. phpinfo() 를 출력하는 i.php 를 만들고 웹 브라우저로 연결하여 xdebug 설정 여부 확인

    <?php
    phpinfo();
    ?>
    CODE

 

Sublime 설정

  1. Preferences -> Package Settings -> Xdebug -> Settings- User
  2. Xdebug.sublime-settings 설정
    vagrant 사용시 

    {
    	"path_mapping": {
            "/home/vagrant/Code/Laravel" : "C:\\Users\\lesstif\\Code\\Laravel"
        },
        "url": "http://homestead.app/",
        "ide_key": "sublime.xdebug",
        "port": 9001,
        "super_globals": true,
        "close_on_stop": true
    }
    CODE

    localhost 사용시 

    {
    	"path_mapping": {
            "C:\\Users\\lesstif\\Code\\Laravel" : "C:\\Users\\lesstif\\Code\\Laravel"
        },
        "url": "http://localhost/",
        "port": 9001,
        "super_globals": true,
        "close_on_stop": true
    }
    CODE

    path_mapping: 원격 디버깅시 key(/home/vagrant/Code/Laravel)를 원격지 소스, value 를 로컬 소스 경로(C:\\Users\\lesstif\\Code\\Laravel를 지정. 두 개의 소스는 일치해야 함.

    localhost 의 웹 서버에 구동할 경우 DocumentRoot 와도 일치해야 함.


    url : xdebug 가 붙어 있는 원격 웹 서버 url

    path_mapping의  경로를 public 까지 잡으면 public 에 있는 파일만 디버깅 됨.

디버깅

섭라임에서 디버깅

  1. 서브라임 재구동
  2. Break point 를 설정할 소스를 열고 해당 라인에서 마우스 우클릭  Xdebug -> Add/Remove Breakpoint 설정
  3. Ctrl-Shift-P 를 누르고 xdebug 를 친 후에 메뉴에서 Start Debugging (Launch Browser) 선택
  4. 브레이크 포인트에 멈췄으면 디버깅 시작. Step Into 단축키는 Ctrl-Shift-F7
  5. 디버깅 

 

명령행 php 에서 디버깅

laravel 의 artisan 등 명령행에서 php를 실행할 경우  -d 옵션으로 php.ini 에 있는 xdebug 설정을 넘겨주어야 한다.

다음 파일을 적당한 이름(ex: xdebug-artisan.bat) 로 저장한다.

@ECHO OFF

php -d xdebug.profiler_enable=On -d xdebug.remote_enable=1 -d xdebug.remote_autostart=1 ^
	-d xdebug.remote_host="127.0.0.1" -d xdebug.remote_port=9001 -d xdebug.max_nesting_level=250 ^
	artisan %*
CODE

 

sublime 에서 artisan을 열고 브레이크 포인트를 건 후에 명령행에서 실행한다.

 xdebug-artisan.bat route:list
CODE

Ref