Apple在发布macOS High Sierra后,系统也终于自带了php v7.1,相比于以前,若是想使用php7,还得额外想办法( Homebrew 或者 php-osx )而言着实方便了很多。php
可是,系统自带的PHP只有基础的配置,若是想作PHP开发,Xdebug仍是必须的,如下就总结一下如何在macOS High Sierra中为系统自带的PHP增长Xdebug模块。html
Xdebug官网安装文档中有MAC推荐的方式,鉴于系统自带的是PHP是v7.1.7,因此在选择的时候,须要选择php71-xdebug这个安装包。git
另外因为brew中的php71-xdebug依赖于php71的,因此建议加上--without-homebrew-php
这个参数,这样的话brew就会忽略安装php71。github
brew install php71-xdebug --without-homebrew-php
不过这个时候,或许你会碰到下面这样的报错:web
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
时发现这个目录根本不存在。xcode
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
也不行):bash
ln: /usr/include: Operation not permitted
不过好在Apple为开发人员准备了Xcode,这是一个很强大的工具,可是体积也很大(下载安装有点慢),而通常咱们只须要它提供的Command Line Tools就够了,上面的问题,其实只要安装Command Line Tools就能够解决:cookie
xcode-select --install
接下来,跟着提示作,安装、赞成协议...php7
等待安装结束之后,再用 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!
通过上面步骤,系统里面是有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 ...
VSCode是目前最流行的开发工具之一,虽然轻量,可是对标各种IDE绝不逊色,微软良心之做,经过安装不一样的插件能够扩展它的能力,其中有一款 PHP Debug 的插件,能够做为Xdebug的桥梁,方便直接经过Xdebug调试PHP,官方的描述十分贴切:
PHP Debug Adapter for Visual Studio Code
官网的指导也写的至关不错:
Install XDebug
I highly recommend you make a simpletest.php
file, put aphpinfo();
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 yourphpinfo()
output under "Loaded Configuration File".Enable remote debugging in your php.ini:
[XDebug] xdebug.remote_enable = 1 xdebug.remote_autostart = 1There are other ways to tell XDebug to connect to a remote debugger than
remote_autostart
, like cookies, query parameters or browser extensions. I recommendremote_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》,后续若有更新,能够查看原文。