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

macOS PHP7如何增加Xdebug

Dec 21, 2021 pm 04:53 PM
macos php7

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的設定項目:<div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">登入後複製</div></div>然後就是重啟

php-fpm

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"># 关闭php-fpm sudo killall php-fpm # 启动php-fpm sudo php-fpm</pre><div class="contentsignin">登入後複製</div></div>運行

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

後,你就可以看到關於Xdebug的設定內容了:<div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">登入後複製</div></div>

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

apache怎麼刪除多於的服務器名 apache怎麼刪除多於的服務器名 Apr 13, 2025 pm 01:09 PM

要從 Apache 中刪除多餘的 ServerName 指令,可以採取以下步驟:識別並刪除多餘的 ServerName 指令。重新啟動 Apache 使更改生效。檢查配置文件驗證更改。測試服務器確保問題已解決。

apache服務器怎麼重啟 apache服務器怎麼重啟 Apr 13, 2025 pm 01:12 PM

要重啟 Apache 服務器,請按照以下步驟操作:Linux/macOS:運行 sudo systemctl restart apache2。 Windows:運行 net stop Apache2.4 然後 net start Apache2.4。運行 netstat -a | findstr 80 檢查服務器狀態。

vscode需要什麼電腦配置 vscode需要什麼電腦配置 Apr 15, 2025 pm 09:48 PM

VS Code 系統要求:操作系統:Windows 10 及以上、macOS 10.12 及以上、Linux 發行版處理器:最低 1.6 GHz,推薦 2.0 GHz 及以上內存:最低 512 MB,推薦 4 GB 及以上存儲空間:最低 250 MB,推薦 1 GB 及以上其他要求:穩定網絡連接,Xorg/Wayland(Linux)

macos終端怎麼打開 macos終端怎麼打開 Apr 12, 2025 pm 05:39 PM

在 macOS 終端中打開文件:打開終端導航到文件目錄:cd ~/Desktop使用 open 命令:open test.txt其他選項:使用 -a 選項指定特定應用程序使用 -R 選項僅在 Finder 中顯示文件

vscode 無法安裝擴展 vscode 無法安裝擴展 Apr 15, 2025 pm 07:18 PM

VS Code擴展安裝失敗的原因可能包括:網絡不穩定、權限不足、系統兼容性問題、VS Code版本過舊、殺毒軟件或防火牆干擾。通過檢查網絡連接、權限、日誌文件、更新VS Code、禁用安全軟件以及重啟VS Code或計算機,可以逐步排查和解決問題。

git怎麼查看倉庫地址 git怎麼查看倉庫地址 Apr 17, 2025 pm 01:54 PM

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

vscode 可以用於 mac 嗎 vscode 可以用於 mac 嗎 Apr 15, 2025 pm 07:36 PM

VS Code 可以在 Mac 上使用。它具有強大的擴展功能、Git 集成、終端和調試器,同時還提供了豐富的設置選項。但是,對於特別大型項目或專業性較強的開發,VS Code 可能會有性能或功能限制。

vscode終端使用教程 vscode終端使用教程 Apr 15, 2025 pm 10:09 PM

vscode 內置終端是一個開發工具,允許在編輯器內運行命令和腳本,以簡化開發流程。如何使用 vscode 終端:通過快捷鍵 (Ctrl/Cmd ) 打開終端。輸入命令或運行腳本。使用熱鍵 (如 Ctrl L 清除終端)。更改工作目錄 (如 cd 命令)。高級功能包括調試模式、代碼片段自動補全和交互式命令歷史。

See all articles