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》,後續若有更新,能夠查看原文。