0%

Mac debug PHP with xdebug in vscode

起因

发现 10.13 自带的 php7 不能安装 xdebug… 否则需要关闭 SIP,算了不如自己再装一个。

前期准备

  1. brew 自行安装
  2. apache2.4 mac 自带
  3. vscode 自行安装
  4. xcode command-line-tool 自行安装
  5. php7.1 后面说
  6. xdebug 同上

php 7.1

brew 在新版中更新了 php 的位置,可以使用 brew search php 查看。

这里安装 php 7.1 版本,安装命令 brew install php@7.1

最后安装完成的信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module /usr/local/opt/php@7.1/lib/httpd/modules/libphp7.so

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
/usr/local/etc/php/7.1/

php@7.1 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have php@7.1 first in your PATH run:
echo 'export PATH="/usr/local/opt/php@7.1/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.1/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@7.1 you may need to set:
export LDFLAGS="-L/usr/local/opt/php@7.1/lib"
export CPPFLAGS="-I/usr/local/opt/php@7.1/include"

可参考其中配置,在 bash(zsh)中执行

1
2
echo 'export PATH="/usr/local/opt/php@7.1/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.1/sbin:$PATH"' >> ~/.zshrc

或者你确实需要替换系统 php,则执行 brew link php@7.1 会提示你下一步操作。

另外,如果需要多版本切换,可参考:
https://www.cnblogs.com/it-tsz/p/9311768.html

如果需要替换系统 apache,可参考:
http://www.hangge.com/blog/cache/detail_2333.html

如果只是需要调试 php,暂不需要上面两步操作。

xdebug

两种方式安装,先介绍简单的:

pecl 安装

通过 brew 安装的 php 已经添加了这个功能,可通过
pecl install xdebug-2.7.1

版本号需要再次确认下,可以参看第二种方法确认。

这种方式安装的 xdebug 自动配置在 php.ini 第一行,最好调整至最后一行,见收尾部分。

编译安装

https://xdebug.org/wizard.php

访问上述页面,将 phpinfo() 信息或者 php -i 提交,可以得到编译安装方式。在这里我的信息是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Download xdebug-2.7.1.tgz
Install the pre-requisites for compiling PHP extensions. These packages are often called 'php-dev', or 'php-devel', 'automake' and 'autoconf'.
Unpack the downloaded file with tar -xvzf xdebug-2.7.1.tgz
Run: cd xdebug-2.7.1
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: 20160303
Zend Extension Api No: 320160303
If 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/local/lib/php/pecl/20160303
Update /usr/local/etc/php/7.1/php.ini and change the line
zend_extension = /usr/local/lib/php/pecl/20160303/xdebug.so
Make sure that zend_extension = /usr/local/lib/php/pecl/20160303/xdebug.so is below the line for OPcache.

按照他的顺序来就可以。

收尾

在 php.ini 末尾添加如下信息(端口如果冲突可以修改为其他端口)

1
2
3
4
5
6
7
zend_extension=/usr/local/lib/php/pecl/20160303/xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.scream=0
xdebug.show_local_vars=1

apache - httpd.conf

找到 /etc/apache2/httpd.conf 文件,更改 php7 那一行为如下两行:

1
2
LoadModule php7_module /usr/local/opt/php@7.1/lib/httpd/modules/libphp7.so
#LoadModule php7_module libexec/apache2/libphp7.so

vscode

  1. 搜索 php debug 进行安装
  2. 设置 - setting.json - 添加配置 “php.validate.executablePath”: “/usr/local/opt/php@7.1/bin/php”
  3. 打开工程文件夹
  4. 切换到调试页面,选 php 并(自动)创建配置项(如果改了端口需要同步修改下端口信息)
  5. 调试运行,浏览器访问,即可

扫尾

第一个问题

启动 node 报错了,因为 php 用到的一个依赖和 node 冲突了,参考如下解决就行了,没仔细研究。
https://www.jianshu.com/p/f276676a464e
https://segmentfault.com/a/1190000018065230?utm_source=tag-newest

第二个问题

zsh: /usr/local/bin/pip: bad interpreter: /usr/local/opt/python/bin/python3.7: no such file or directory

随 php 安装的 py3 更新了版本,并且覆盖了软连接
brew link --overwrite python@2 可解决

第三个问题

1
2
3
ImportError: dlopen(/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib
Referenced from: /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so
Reason: image not found

安装 php 更新了 readline 到 8

1
2
3
4
/usr/local/opt/readline/lib/:
libhistory.8.0.dylib libhistory.dylib libreadline.a
libhistory.8.dylib libreadline.8.0.dylib libreadline.dylib
libhistory.a libreadline.8.dylib pkgconfig

解决:
ln -s /usr/local/opt/readline/lib/libreadline.8.0.dylib /usr/local/opt/readline/lib/libreadline.7.dylib