资讯专栏INFORMATION COLUMN

macOS系统PHP7增加Xdebug

sPeng / 1903人阅读

摘要:但是,系统自带的只有基础的配置,如果想做开发,还是必须的,以下就总结一下如何在中为系统自带的增加模块。本文先发布于我的个人博客系统增加,后续如有更新,可以查看原文。

Apple在发布macOS High Sierra后,系统也终于自带了php v7.1,相比于之前,如果想使用php7,还得额外想办法( Homebrew 或者 php-osx )而言着实方便了不少。

但是,系统自带的PHP只有基础的配置,如果想做PHP开发,Xdebug还是必须的,以下就总结一下如何在macOS High Sierra中为系统自带的PHP增加Xdebug模块。

基础环境( macOS 及 PHP 信息)

macOS High Sierra: v10.13.3

PHP: v7.1.7

安装Xdebug

Xdebug官网安装文档中有MAC推荐的方式,鉴于系统自带的是PHP是v7.1.7,所以在选择的时候,需要选择php71-xdebug这个安装包。

另外由于brew中的php71-xdebug依赖于php71的,所以建议加上--without-homebrew-php这个参数,这样的话brew就会忽略安装php71

brew install php71-xdebug --without-homebrew-php

不过这个时候,或许你会碰到下面这样的报错:

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:

提示缺失依赖,从而导致phpize无法正常工作,phpize是用来准备 PHP 扩展库的编译环境的,理论上系统自带的PHP应该是有phpize的,但是没有在/usr/include/php/*里面找到它需要的模块,并且检索/usr/include时发现这个目录根本不存在。

Google了一圈,解决问题,就需要在/usr/include中补全相关的内容,在OSX v10.10以前系统,需要手动做软链来解决:

sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include /usr/include

但是v10.11以后的系统重写了安全策略,所以会遇到权限问题(sudo也不行):

ln: /usr/include: Operation not permitted

不过好在Apple为开发人员准备了Xcode,这是一个很强大的工具,但是体积也很大(下载安装有点慢),而一般我们只需要它提供的Command Line Tools就够了,上面的问题,其实只要安装Command Line Tools就可以解决:

xcode-select --install

接下来,跟着提示做,安装、同意协议...

等待安装结束以后,再用 brew 来安装 php71-xdebug:

brew install php71-xdebug --without-homebrew-php

一切结束以后,brew会给出提示:

To finish installing xdebug for PHP 7.1:
  * /usr/local/etc/php/7.1/conf.d/ext-xdebug.ini was created,
    do not forget to remove it upon extension removal.
  * Validate installation via one of the following methods:
  *
  * Using PHP from a webserver:
  * - Restart your webserver.
  * - Write a PHP page that calls "phpinfo();"
  * - Load it in a browser and look for the info on the xdebug module.
  * - If you see it, you have been successful!
  *
  * Using PHP from the command line:
  * - Run `php -i "(command-line "phpinfo()")"`
  * - Look for the info on the xdebug module.
  * - If you see it, you have been successful!
开启PHP的Xdebug

经过上面步骤,系统里面是有Xdebug了,但是在php.ini配置文件中不一定有,因此需要手动添加Xdebug的配置项:

[xdebug]
zend_extension="/usr/local/opt/php71-xdebug/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

然后就是重启php-fpm

# 关闭php-fpm
sudo killall php-fpm

# 启动php-fpm
sudo php-fpm

运行php -i "(command-line "phpinfo()")" | grep xdebug后,你就可以看到关于Xdebug的配置内容了:

xdebug
...
xdebug.remote_autostart => On => On
xdebug.remote_connect_back => On => On
xdebug.remote_cookie_expire_time => 3600 => 3600
xdebug.remote_enable => On => On
xdebug.remote_handler => dbgp => dbgp
xdebug.remote_host => localhost => localhost
xdebug.remote_log => no value => no value
xdebug.remote_mode => req => req
xdebug.remote_port => 9000 => 9000
xdebug.remote_timeout => 200 => 200
xdebug.scream => Off => Off
...
Visual Studio Code - PHP Debug

VSCode是目前最流行的开发工具之一,虽然轻量,但是对标各类IDE毫不逊色,微软良心之作,通过安装不同的插件可以扩展它的能力,其中有一款 PHP Debug 的插件,可以作为Xdebug的桥梁,方便直接通过Xdebug调试PHP,官方的描述十分贴切:

PHP Debug Adapter for Visual Studio Code

官网的指导也写的相当不错:

Install XDebug
I highly recommend you make a simple test.php file, put a phpinfo(); statement in there, then copy the output and paste it into the XDebug installation wizard. It will analyze it and give you tailored installation instructions for your environment.
In short:

On Windows: Download the appropiate precompiled DLL for your PHP version, architecture (64/32 Bit), thread safety (TS/NTS) and Visual Studio compiler version and place it in your PHP extension folder.

On Linux: Either download the source code as a tarball or clone it with git, then compile it.

Configure PHP to use XDebug by adding zend_extension=path/to/xdebug to your php.ini.
The path of your php.ini is shown in your phpinfo() output under "Loaded Configuration File".

Enable remote debugging in your php.ini:

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

There are other ways to tell XDebug to connect to a remote debugger than remote_autostart, like cookies, query parameters or browser extensions. I recommend remote_autostart because it "just works". There are also a variety of other options, like the port (by default 9000), please see the XDebug documentation on remote debugging for more information.

If you are doing web development, don"t forget to restart your webserver to reload the settings

Verify your installation by checking your phpinfo() output for an XDebug section.

这里需要注意的是它推荐开启Xdebug配置项中的remote_autostart这一项。

好了,经过上面的操作,你应该可以跟Demo里面一样在VSCode中调试PHP了。

本文先发布于我的个人博客《macOS系统PHP7增加Xdebug》,后续如有更新,可以查看原文。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28441.html

相关文章

  • 用Visual Studio Code Debug世界上最好的语言(Mac篇)

    摘要:悬停以查看现有属性的描述。欲了解更多信息,请访问默认是已经被占用上一步我们配置远程端口是默认是已经被占用上一步我们配置远程端口是然后就愉快最好的语言吧其他部分系统增加 用Visual Studio Code Debug世界上最好的语言(Mac篇) 首先,你要有台Macbook Pro,接着才继续看这个教程. PS:Windows用户看这里用Visual Studio Code Debu...

    crossea 评论0 收藏0
  • MacOS 升级自带PHP5.6 升级到 PHP7.1

    摘要:自带的是版本,由于带来了不少新特性和性能的提升,所以决定升级输入下面命令,安装安装包,安装过程需要输入系统的密码打开终端输入,为什么还是提示版本还是不会覆盖原来的版本,而是安装了两个版本,新版本安装的根目录,启动脚本路径你可以打开 mac自带的是php5.6版本,由于php7带来了不少新特性和性能的提升,所以决定升级php7 输入下面命令,安装php7.1安装包,安装过程需要输入系统的...

    MadPecker 评论0 收藏0
  • [进阶篇]docker编排PHP开发坏境

    摘要:开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括虚拟机集群和其他的基础应用平台。容器进入容器名暴露端口暴露端口使用调试环境中安装了调试,需对进行配置后启用,配置如下配置完成后需要重启下容器。 showImg(https://segmentfault.com/img/bVbgmdS?w=567&h=272); Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻...

    PingCAP 评论0 收藏0
  • [进阶篇]docker编排PHP开发坏境

    摘要:开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括虚拟机集群和其他的基础应用平台。容器进入容器名暴露端口暴露端口使用调试环境中安装了调试,需对进行配置后启用,配置如下配置完成后需要重启下容器。 showImg(https://segmentfault.com/img/bVbgmdS?w=567&h=272); Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻...

    kevin 评论0 收藏0
  • php7 使用 phpunit 部分错误和解决方案

    摘要:报错信息问题和解决测试其实已经通过了,但,代表没有任何断言被执行。增加或修改这行到的中每个测试都在独立的进程中运行。 预先准备(brew 安装的情况下) php7 php7-xdebug runkit7 报错信息1: Error:No code coverage driver is available 问题和解决: # 没有成功安装xdebug brew search php7...

    gaosboy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<