프로그래밍/PHP

[PHP] xDebug를 이용한 서버 원격 디버깅

채윤아빠 2019. 8. 22. 20:10
728x90
반응형



웹 서버 xDebug 설정

원격의 웹 서버에 대한 PHP 디버깅은 xDebug를 활용하면, 실제 실행되는 환경을 디버깅 할 수 있습니다.

디버깅 포트 확인

웹 서버에 특정 헤더를 보내어 디버깅을 하겠다고 알려주면, 웹서버는 개발자 PC의 기본 9,000 포트로 디버깅을 준비하고 디버깅을 진행합니다.

따라서, 웹 서버에서 개발자 PC의 9,000 포트로 접속할 수 있도록 관련 방화벽 정책 설정되어 있어야 하고, 실제로 웹 서버에서 개발 PC 9000 포트로 연결에 문제가 없는지 다음과 같이 확인할 수 있습니다.

# ssh -v -o ConnectTimeout=3 -o ConnectionAttempts=1 10.103.20.61 -p9000
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 10.103.20.61 [10.103.20.61] port 9000.
debug1: connect to address 10.103.20.61 port 9000: Connection refused
ssh: connect to host 10.103.20.61 port 9000: Connection refused

"Connection refused"가 나오거나, "Connection established"가 나오면 웹 서버에 대한 원격 디버깅이 가능하도록 방화벽 관련 설정이 완료된 것입니다.
방화벽 관련 문제로 접속을 할 수 없을 경우에는 다음과 같이 "Connection timed out"이 발생합니다.

# ssh -v -o ConnectTimeout=3 -o ConnectionAttempts=1 10.103.20.60 -p9000
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 10.103.20.60 [10.103.20.60] port 9000.
debug1: connect to address 10.103.20.60 port 9000: Connection timed out
ssh: connect to host 10.103.20.60 port 9000: Connection timed out

xDebug 모듈 설치

웹 서버단 PHP에 xDebug 모듈을 설치합니다.

CentOS

다음과 같이 "yum"을 이용하여 xdebug 모듈을 설치합니다.

# yum install -y php-pecl-xdebug

"/etc/php.ini" 파일에 다음과 같이 "xDebug" 설정을 추가합니다.

[xDebug]
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.profiler_enable = 1
xdebug.remote_enable = 1
xdebug.remote_autostart = 0
xdebug.remote_port = 9000
xdebug.remote_connect_back = on
xdebug.idekey = "editor-xdebug" 

웹 서비스를 재시작 합니다.

Docker-Alpine 컨테이너

컨테이너의 Dockerfile에 다음과 같이 "php7-xdebug" 모듈이 설치되도록 합니다.

$ apk add php7-xdebug

컨테이너의 Dockerfile에 다음과 같은 내용을 추가하여 도커의 컨테이너 구동시 "php.ini"에 설정 되도록 합니다.

echo "[xDebug]" >> /etc/php7/php.ini;
echo 'zend_extension=/usr/lib/php7/modules/xdebug.so' >> /etc/php7/php.ini;
echo 'xdebug.coverage_enable=1' >> /etc/php7/php.ini;
echo 'xdebug.profiler_enable=1' >> /etc/php7/php.ini;
echo 'xdebug.remote_autostart=0' >> /etc/php7/php.ini;
echo 'xdebug.remote_connect_back=1' >> /etc/php7/php.ini;
echo 'xdebug.remote_enable=1' >> /etc/php7/php.ini;
echo 'xdebug.remote_log=/tmp/xdebug.log' >> /etc/php7/php.ini;
echo 'xdebug.idekey="XDEBUG_ECLIPSE"' >> /etc/php7/php.ini;

Windows Apache

apache 및 php 버전에 맞는 xdebug 모듈 (dll)을 다운로드 받은 후에 PHP 설치 폴더의 "ext" 폴더에 다운로드 받은 dll 파일(예: php_xdebug-2.7.2-7.3-vc15-x86_64.dll)을 설치합니다.

PHP 설치 폴더의 "php.ini" 파일에 다음과 같이 xDebug 설정을 추가합니다.

[XDebug]
; xdebug*.dll 파일 경로
zend_extension=C:/Dev/php-7.3.1/ext/php_xdebug-2.7.2-7.3-vc15-x86_64.dll
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_enable=1
xdebug.remote_autostart=1


개발자 PC

VS Code

"Microsoft Visual Studio Code"를 설치하고, xDebug를 이용하여 디버깅하기 위하여 "PHP Debug" 확장을 설치합니다.

PHP Debug

Id: felixfbecker.php-debug
Description: Debug support for PHP with XDebug
Version: 1.13.0
Publisher: Felix Becker
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug

디버깅하고자 하는 웹 서비스 프로젝트 폴더를 연 후에, "Debug" 패널을 선택 후, 다음과 같이 "PHP"에 대한 디버깅 설정을 추가합니다.

그러면, 다음과 같이 "launch.json" 파일이 생성됩니다.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

자동으로 생성된 "launch.json" 설정에서 디버깅하고자 하는 웹 서비스 폴더 구조에 맞게 "pathMappings" 정보를 입력합니다.

        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/app/public/": "${workspaceRoot}/",
            }
        },
    ]

크롬 브라우저

"xDebug Helper" 확장을 설치합니다. ; https://chrome.google.com/webstore/category/extensions

디버깅을 하고자 하는 웹 서비스 서버에 접속한 후에, 다음과 같이 xdebug를 활성화합니다.


이후, 디버깅을 하고자하는 페이지로 이동하면, "VS Code"에 설정한 중단점(Break point)부터 디버깅을 할 수 있습니다.


참고자료