이번에 m1을 사용하며 VSCode도 사용해보게 되었다.
마음의 평안에 도움이 되는 Xdebug를 설치해본다.
1. Extentions 설치
왼쪽에 테트리스 블럭같은 아이콘을 클릭하면 extentions 목록이 보인다.
PHP Debug 를 검색해서 설치한다.
하단에 아주 자세하게 설정하는 방법이 나와있다.
차례차례 따라하면 된다.
2. Xdebug 설정
2-1. Xdebug wizard 사용
test.php 파일을 생성하고 phpinfo(); 를 실행한 결과를 복사해서 Xdebug installation wiazrd에 붙여 넣는다.
그럼 마법사가 내 환경설정에 알맞은 설정 튜토리얼을 제공해준다. 너무 좋은데?
나는 document root에 index.php 파일을 생성해서 phpinfo() 를 출력하도록 했다.
그럼 브라우저에서 localhost 로 접속하면 언제는 phpinfo()를 볼 수 있어서 좋다!
Xdebug 마법사 : xdebug.org/wizard
Xdebug: Support — Tailored Installation Instructions
Installation Wizard This page helps you finding which file to download, and how to configure PHP to get Xdebug running. Please paste the full output of phpinfo() (either a copy & paste of the HTML version, the HTML source or php -i output) and submit the f
xdebug.org
위 사이트에 들어가면 아래와 같이 나의 phpinfo() 결과를 붙여넣을 수 있는 입력창이 나온다.
phpinfo()를 확인할 수 있는 브라우저에서 전체 선택하여 복사한다. (Ctrl + a, Ctrl + c)
그리고 Analyse my phpinfo() output 버튼을 누르면, 내 환경에 알맞은 설정 방법이 나온다.
나의 환경에 알맞는 설정이긴 하지만, 나름 퓨어한 상태의 환경이므로 큰 특이사항은 없을 것 같다.
* Installation Wizard 결과
Summary
- Xdebug installed: no
- Server API: Apache 2.0 Handler
- Windows: no
- Zend Server: no
- PHP Version: 7.3.24-
- Zend API nr: 320180731
- PHP API nr: 20180731
- Debug Build: no
- Thread Safe Build: no
- OPcache Loaded: no
- Configuration File Path: /etc
- Configuration File: /etc/php.ini
- Extensions directory: /usr/lib/php/extensions/no-debug-non-zts-20180731
Instructions
- Download xdebug-3.0.2.tgz
- Install the pre-requisites for compiling PHP extensions.
On your Mac, we only support installations with 'homebrew', and brew install php && brew install autoconf should pull in the right packages. - Unpack the downloaded file with tar -xvzf xdebug-3.0.2.tgz
- Run: cd xdebug-3.0.2
-
Run: phpize (See the FAQ if you don't have phpize).
As part of its output it should show:
Configuring for: ... Zend Module Api No: 20180731 Zend Extension Api No: 320180731If it does not, you are using the wrong phpize. Please follow this FAQ entry and skip the next step.
- Run: ./configure
- Run: make
- Run: cp modules/xdebug.so /usr/lib/php/extensions/no-debug-non-zts-20180731
- Edit /etc/php.ini and add the line
zend_extension = /usr/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so - Restart the webserver
2-2. Xdebug 확장 모듈 설치
1. xdebug-3.0.2.tgz 다운로드
2. php, autoconf - brew로 설치
brew install php && brew install autoconf
brew list로 확인해보고 넘어간다.
3. 다운로드한 압축파일 풀기 - document root에 풀었다.
tar -xvzf xdebug-3.0.2.tgz
4. 압축 해제된 디렉터리로 이동
cd xdebug-3.0.2
5. Xdebug 설치 명령 입력
phpize
phpize는 php의 확장 모듈을 설치할 때 사용되는 명령어이다.
가이드에 따르면, 정상적인 출력은 이러하다.
Configuring for:
...
Zend Module Api No: 20180731
Zend Extension Api No: 320180731
여기서부터 뭔가 이상했다... phpize 입력한 결과가 이렇게 나왔다.
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
얼핏 봐도 가이드와 확연히 다르지만, 괜히 다음 명령어도 입력해봤다.
./configure
make
역시나 또 에러 뿜뿜
fatal error: 'php.h' file not found
#include "php.h"
^~~~~~~
1 error generated.
make: *** [xdebug.lo] Error 1
에러의 원인은 /usr/include 디렉터리가 없어서 나타나는 문제였다. (애플이 출시하면서 없앴다고 한다..)
에러 발생 윗 라인들을 보면 -I/usr/include/... 를 참조하려고 하는 시도들을 볼 수 있다.
이 곳을 참고해 오류를 수정할 수 있었다. 감사합니다...
profilingviewer.com/installing-xdebug-on-catalina.html
Installing Xdebug on macOS 10.15 Catalina (Xcode 11)
Tutorial - solutions for common problems installing xdebug on macOS
profilingviewer.com
다음 명령으로 Xcode SDK 경로를 확인한다.
xcrun --show-sdk-path
다음의 명령으로 출력이 되면 준비 완료!
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
이제 새로운 phpize와 php-config의 수정된 버전을 생성해서 사용할 것이다.
홈 디렉터리에 두 파일을 넣어둘 새로운 디렉터리를 생성하고 phpize와 php-config를 복사해온다.
mkdir ~/php-private
cp /usr/bin/phpize ~/php-private/
cp /usr/bin/php-config ~/php-private/
제대로 복사가 됐는지 확인!
grep version= ~/php-private/php-config
그럼 이렇게 나온다. 잘 됐군
version="7.3.24-(to be removed in future macOS)"
위 블로그의 글쓴이는 패치 파일을 만들어서 php-private에 있는 사본을 패치하는 방법으로 했다.
난 좀 더 무식하게... 사본에서 변경이 필요한 부분을 패치파일에서 잘 골라내서 직접 수정했다 ㅋㅋ...
- phpize 수정
#includedir="`eval echo ${prefix}/include`/php"
# custom added
XCODE_SDK_ROOT=$(/usr/bin/xcrun --show-sdk-path)
includedir="`eval echo ${XCODE_SDK_ROOT}${prefix}/include`/php"
기존의 includedir을 주석 처리하고, 내가 수정한 부분은 나중에 알아볼 수 있게 따로 표시해뒀다.
- php-config 수정
#include_dir="${prefix}/include/php"
# custom added
XCODE_SDK_ROOT=$(/usr/bin/xcrun --show-sdk-path)
include_dir="${XCODE_SDK_ROOT}${prefix}/include/php"
이제 정말 준비가 끝났다. document root에 풀어놓은 xdebug-3.0.2 디렉터리로 이동해서, 위에서 수정한 phpize를 실행한다.
나의 document root는 ~/Sites이다.
cd ~/Sites/xdebug-3.0.2
~/php-private/phpize
그럼 짜잔~
Configuring for:
PHP Api Version: 20180731
Zend Module Api No: 20180731
Zend Extension Api No: 320180731
다음 명령어인 .configure는 아래와 같이 경로를 지정해준다.
% echo ~/php-private/php-config
/Users/myUser/php-private/php-config // 복사해서 사용
% ./configure --enable-xdebug --with-php-config=/Users/myUser/php-private/php-config
설치되는 과정을 보면 /include 앞에 MacOSX.sdk 경로가 추가된 것을 확인할 수 있다.
이제야 Xdebug 확장 모듈을 설치하기 위한 준비가 끝났다.. 헥헥
빌드해보자~~
make
그러면 이러한 결과가.. 감격 ㅠㅠ
----------------------------------------------------------------------
Libraries have been installed in:
/Users/myUser/Sites/xdebug-3.0.2/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
during execution
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
다시 xdebug.org에서 알려준 가이드로 돌아가서 다음 단계를 진행한다.
현재 위치는 여전히 ~/Sites/xdebug-3.0.2이다.
cp modules/xdebug.so /usr/lib/php/extensions/no-debug-non-zts-20180731
이제 php.ini에서 xdebug 모듈을 활성화하기 위해 아래의 문구를 추가한다.
zend_extension = /usr/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so
웹서버를 재시작하고 xdebug 모듈 확장이 잘 되었는지 확인해본다.
php -i | grep xdebug
결과로 이렇게 나오면 성공!
xdebug
xdebug support => enabled
...
또는 아까 사용했던 phpinfo() 출력 결과에서 'xdebug' 키워드를 검색해서 확인해봐도 좋다.
이제 VSC로 가서 디버깅을 해보자.
VSC 디버깅 모드 사용하기
작업할 프로젝트 디렉터리를 열고,
단축키 F5 또는 상단 메뉴의 Run > Start Debugging을 누르면
루트 디렉토리에 .vscode/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": [
{
"type": "php",
"request": "launch",
"name": "Launch Chrome against!!",
"url": "http://프로젝트 도메인",
"webRoot": "${workspaceFolder}",
}
]
}
역시나 뭔가 잘 안되어서 VSC도 껐다가 다시 켰다. 바로 반영이 샤샥 되지는 않는가보다...ㅠㅠ
php 코드의 라인수 옆을 클릭하면 빨갛게 break point 가 설정된다.
좌측에 보면 디버깅 탭이 있다. 클릭!
디버깅 탭을 들어가 보면 내가 설정한 breakpoints 목록이 아래쪽에 보인다.
break point 설정 후 Start Debugging을 실행한다.
그러면 해당 라인에서 멈추고, 디버깅 탭에서 내가 원하는 변수, 전역 변수 모두 확인해볼 수 있다.
그런데 조금 길어지는 문자열이나 배열의 값은 한정적으로 보여주는 게 좀 아쉽...
해당 변수에서 우클릭해서 복사한 뒤 다른 곳에 붙여 넣기 하면 해당 변수를 볼 수는 있다..
조금 써보다가 영 불편하면 스톰으로 넘어가야겠다...
(결국 스톰으로 바꿨다... php.ini에 xdebug 설정이 되어있으므로 스톰에서도 디버깅 가능)
'MAC' 카테고리의 다른 글
맥북에어 M1에서 M2로 마이그레이션하기 (1) | 2023.12.28 |
---|---|
Mac M1에서 APM(Apache, PHP, MySQL) 설치하기 (1) | 2021.02.22 |