首頁  >  文章  >  後端開發  >  macOS PHP7如何增加Xdebug

macOS PHP7如何增加Xdebug

藏色散人
藏色散人轉載
2021-12-21 16:53:001823瀏覽

macOS系統PHP7增加Xdebug

Apple在發布macOS High Sierra後,系統也終於自帶了php v7.1,相較於之前,如果想使用php7,還得額外想辦法( Homebrewphp-osx )而言著實方便了不少。

但是,系統自帶的PHP只有基礎的配置,如果想做PHP開發,Xdebug還是必須的,以下就總結一下如何在macOS High Sierra中為系統自帶的PHP增加Xdebug模組。 【推薦:PHP7教學

基礎環境( macOS 及PHP 資訊)

    ##macOS High Sierra: v10.13.3
  • PHP: v7.1.7

安裝Xdebug

Xdebug官網安裝文件中有MAC推薦的方式,鑑於系統自帶的是PHP是

v7.1.7,所以在選擇的時候,需要選擇php71-xdebug這個安裝套件。

macOS PHP7如何增加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
接下來,跟著提示做,安裝、同意協議...


macOS PHP7如何增加Xdebug##等待安裝結束以後,再用

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的設定項目:<pre class="brush:php;toolbar:false">[xdebug] zend_extension=&quot;/usr/local/opt/php71-xdebug/xdebug.so&quot; 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</pre>然後就是重啟

php-fpm

<pre class="brush:php;toolbar:false"># 关闭php-fpm sudo killall php-fpm # 启动php-fpm sudo php-fpm</pre>運行

php -i "(command-line 'phpinfo()')" | grep xdebug

後,你就可以看到關於Xdebug的設定內容了:<pre class="brush:php;toolbar:false">xdebug ... xdebug.remote_autostart =&gt; On =&gt; On xdebug.remote_connect_back =&gt; On =&gt; On xdebug.remote_cookie_expire_time =&gt; 3600 =&gt; 3600 xdebug.remote_enable =&gt; On =&gt; On xdebug.remote_handler =&gt; dbgp =&gt; dbgp xdebug.remote_host =&gt; localhost =&gt; localhost xdebug.remote_log =&gt; no value =&gt; no value xdebug.remote_mode =&gt; req =&gt; req xdebug.remote_port =&gt; 9000 =&gt; 9000 xdebug.remote_timeout =&gt; 200 =&gt; 200 xdebug.scream =&gt; Off =&gt; Off ...</pre>

Visual Studio Code - PHP Debug

VSCode是目前最受歡迎的開發工具之一,雖然輕量,但是對標各類IDE毫不遜色,微軟良心之作,透過安裝不同的插件可以擴展它的能力,其中有一款

PHP Debug

的插件,可以作為Xdebug的橋樑,方便直接透過Xdebug調試PHP,官方的描述十分貼切:PHP Debug Adapter for Visual Studio Code

官網的指導也寫的相當不錯:

    Install XDebug

  1. 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#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.
  2. The path of your php.ini is shown in your
  3. 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.

  4. If you are doing web development, don't forget to restart your webserver to reload the settings
  5. Verify your installation by checking your phpinfo() output for an XDebug section.

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

好了,经过上面的操作,你应该可以跟Demo里面一样在VSCode中调试PHP了。
macOS PHP7如何增加Xdebug

以上是macOS PHP7如何增加Xdebug的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除