咱們在軟件開發的時候,常常要進行調試,其中有一種調試方法就是斷點,此時能夠用PHP的Xdebug擴展來使用。php
1, 安裝PHP-Xdebug擴展;ubuntu
sudo apt-get install php-xdebug
安裝完成後,用 php -v的命令查看結果vim
php -v PHP 5.6.30-12~ubuntu16.04.1+deb.sury.org+1 (cli) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
能夠看到已經有Xdebug v2.5.5安裝好了.bash
2, 在php的配置文件中對xdebug擴展進行配置,這裏有兩種方式:session
a: 最直接的方式,是找到php.ini,在最後面加入如下配置項;dom
sudo vim /etc/php/5.6/fpm/php.ini
zend_extension=/usr/lib/php/20131226/xdebug.so xdebug.remote_host=localhost xdebug.remote_port=9100 xdebug.remote_enable=1 xdebug.remote_autostart=1
b: 另外一種是在Xdebug的配置文件中,加入相同的配置項;socket
咱們能夠在/etc/php/5.6/fpm/conf.d中看到性能
20-xdebug.ini -> /etc/php/5.6/mods-available/xdebug.ini
能夠進入這個文件中,看到這裏已經講zend_extension的路徑添加進去了,將其餘配置添加進去優化
zend_extension=xdebug.so
以上兩種方法是等價的,由於php的配置文件會引入各個擴展的配置文件this
咱們逐項來看看這些配置分別表明什麼意思
① xdebug.remote_host=localhost
Type: string, Default value: localhost
Selects the host where the debug client is running, you can either use a host name, IP address, or 'unix:///path/to/sock' for a Unix domain socket. This setting is ignored if xdebug.remote_connect_back is enabled.
Support for Unix domain sockets was introduced in Xdebug 2.6.
選擇debug客戶端在那個主機上運行,能夠設置成一個host名稱,IP地址,或者Unix域名的路徑. 若是xdebug.remote_connect_back 被設置爲 enabled時,此host設置將被忽略。
② xdebug.remote_port=9100
Type: integer, Default value: 9000
The port to which Xdebug tries to connect on the remote host. Port 9000 is the default for both the client and the bundled debugclient. As many clients use this port number, it is best to leave this setting unchanged.
這是Xdebug嘗試鏈接的遠程host的端口. 9000端口是客戶端和隨附的debug客戶端的默認端口。因爲不少客戶端都使用這個端口號,這個配置一旦設好,最好保持不變。
③ xdebug.remote_enable=1
Type: boolean, Default value: 0
This switch controls whether Xdebug should try to contact a debug client which is listening on the host and port as set with the settings xdebug.remote_host and xdebug.remote_port. If a connection can not be established the script will just continue as if this setting was 0.
此開關控制着Xdebug是否應該嘗試聯繫debug客戶端,這個客戶端正在監聽配置好的host和port. 若是這項配置爲0,則當沒法鏈接時,腳本會啥都不作,繼續執行。
④ xdebug.remote_autostart=1
Type: boolean, Default value: 0
Normally you need to use a specific HTTP GET/POST variable to start remote debugging (see Remote Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.
一般你須要使用一個特定的HTTP GET/POST 變量來啓動遠程bugging. 當此配置爲1時, Xdebug將永遠會試圖遠程debugging session,並嘗試鏈接一個客戶端,即使 GET/POST/COOKIE變量沒有出現。
3, 重啓php服務
sudo service php5.6-fpm restart
4, 在IDE中添加Debug的Host和Port,進入PhpStorm->File->Settings->Languages & Frameworks->Servers->+
保存後,此時能夠在具體的代碼文件頁面看到debug的標識,
在這裏單擊一下,有一個紅色的圓圈,表明在此處設置一個斷點(break point)
用Postman請求到這個文件,能夠看到在IDE下方,出現了不少信息
這些變量值直接展現出來了,而不要再另外去打斷點來查看。
固然,以上只是xdebug很小的一部分功能。更重要的是用來作性能分析,代碼邏輯優化等,這個過一段時間再來更新。