好久沒有做php開發,由於近期要維護php專案,在部署開發環境時錯誤頻出,如果可以除錯程式碼,解決問題就非常方便了。於是基於phpstorm+xdebug配置了可以除錯的開發環境,期間也查詢參考了很多別人的配置過程,發現很多不是很直觀或者有遺漏。現在把我的配置步驟記錄於此。
1. 安裝php+xdebug+nginx
brew install php71
brew install php71-memcached #專案需要,不需要可以不安裝
brew install php71-xdebug
brew install nginx
複製程式碼
2. 配置nginx
vim ~/homebrew/etc/nginx/servers/drone.conf
複製程式碼
# 常規配置,可根據自己專案調整
server {
listen 80;
# 按自己的需要配置訪問的域名
server_name drone-dev.husor.com;
root /data/wwwroot/drone/;
location ~* \.php {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
複製程式碼
3. 配置phpstorm+xdebug
- 在phpstorm的專案右上角選擇Edit Configrations...
- 新建一個PHP Web Application,如我這裡的drone
- 點選Server後的按鈕,新建一個Server,如我這裡的nginx

- 開啟phpstorm的Preferences,依次選擇Languages & Frameworks > PHP,配置前面安裝好的php


點選連結,開啟xdebug.ini
[xdebug]
; 預設zend_extension路徑已經配置好了
zend_extension="/Users/xxx/homebrew/opt/php71-xdebug/xdebug.so"
xdebug.idekey="macgdbp"
xdebug.remote_enable=1
xdebug.profiler_enable=1
xdebug.remote_host="127.0.0.1"
xdebug.remote_port=9001
xdebug.remote_handler="dbgp"
複製程式碼

Debug port和xdebug.ini中的remote_port一致
4. 啟動php+nginx
sudo brew services start nginx
brew services start php71
# 如果已經啟動過的,就重啟
複製程式碼
5. 除錯程式碼
- 點選專案右上角的除錯按鈕

- 在斷點處停下了

配置本身不難,我遇到的問題是沒有配置Debug port,remote_port配置錯誤。瞭解了這兩點,基本上可以一次成功