萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 開發環境搭建與除錯配置全攻略 (VS Code / PhpStorm + Xdebug 3)

現代工具鏈推薦 (2024+)

角色推薦方案備註
編輯器/IDEVS Code (免費) / PhpStorm (專業)PhpStorm 內建深度 PHP 理解、重構、測試運行
運行環境Docker (php:8.3-fpm + nginx) / Laravel Valet / XAMPPDocker 最接近生產、跨平台一致
除錯器Xdebug 3 (PHP 8.1+ 內建支援)取代 Zend Debugger,功能更強、配置更簡單
依賴管理Composer 2必備
靜態分析PHPStan Level 5+ / PsalmCI 整合,捕獲型別錯誤
代碼風格PHP-CS-Fixer (PSR-12) / Laravel Pint預提交鉤子自動修復

Docker 開發環境一鍵啟動 (docker-compose.yml)

version: '3.8'
services:
  php:
    image: php:8.3-fpm
    volumes:
      - ./:/var/www/html
      - ./php.ini:/usr/local/etc/php/php.ini
    environment:
      XDEBUG_MODE: debug
      XDEBUG_CLIENT_HOST: host.docker.internal # Windows/Mac
      XDEBUG_CLIENT_PORT: 9003
      XDEBUG_START_WITH_REQUEST: yes
    depends_on: [db, redis]

  nginx:
    image: nginx:alpine
    ports: ["8080:80"]
    volumes:
      - ./:/var/www/html
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on: [php]

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: app
    volumes: [db_data:/var/lib/mysql]
    ports: ["3306:3306"]

  redis:
    image: redis:7-alpine
    ports: ["6379:6379"]

volumes: { db_data: }

PHP.ini 關鍵配置 (開發環境)

; php.ini
display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php/error.log

; OPcache 開發環境關閉或設為重新驗證
opcache.enable = 1
opcache.validate_timestamps = 1
opcache.revalidate_freq = 0

; Xdebug 3 配置 (僅開發環境)
zend_extension=xdebug
xdebug.mode = debug,develop,trace
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003
xdebug.start_with_request = yes
xdebug.idekey = VSCODE
xdebug.log_level = 0
xdebug.max_nesting_level = 512

; 上傳/執行限制放寬
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 300
memory_limit = 512M

VS Code 除錯配置 (launch.json)

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for Xdebug",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "pathMappings": {
        "/var/www/html": "${workspaceFolder}"
      },
      "xdebugSettings": {
        "max_children": 100,
        "max_data": -1,
        "max_depth": 5
      }
    },
    {
      "name": "Run Current Script",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "runtimeArgs": ["-dxdebug.start_with_request=yes"]
    }
  ]
}

PhpStorm 零配置除錯

  1. File → Settings → PHP → Debug:Xdebug port 9003,勾選 "Can accept external connections"。
  2. File → Settings → PHP → Servers:新增 Server,Host=localhost,Port=8080,**Path mappings** 映射專案根目錄 → /var/www/html。
  3. 工具列點擊「Start Listening for PHP Debug Connections」 (電話圖示)。
  4. 瀏覽器安裝 Xdebug Helper 擴展,設定 IDE Key = PHPSTORM,點擊 Debug 圖標變綠。
  5. 在代碼行號左側打斷點,瀏覽器訪問頁面,PhpStorm 即會中斷。

常見除錯技巧

  • 條件斷點:右鍵斷點 → Edit Breakpoint → Condition: $user->id === 42。
  • Logpoints:不中斷只輸出日誌 (VS Code: 右鍵斷點 → Add Logpoint)。
  • Evaluate Expression:除錯控制台即時執行代碼片段。
  • Watch 變數:監控複雜表達式變化。
  • 異常中斷:VS Code "breakOnUncaughtExceptions": true;PhpStorm: Run → View Breakpoints → PHP Exception Breakpoints。
  • Profiling:xdebug.mode=profile 生成 cachegrind.out.*,用 QCacheGrind / Webgrind 分析效能熱點。

避坑指南

現象原因解決
斷點不生效/顯示灰色路徑映射錯誤檢查 pathMappings / PhpStorm Server mappings,容器內路徑必須一致
Xdebug 連不上 IDEclient_host 錯誤、防火牆阻擋 9003Docker 用 host.docker.internal;本機用 127.0.0.1;開放端口
變數顯示 "uninitialized" 或亂碼OPcache 優化移除變數 / 編碼不一致開發關閉 OPcache 或 validate_timestamps=1;確保檔案 UTF-8
請求超時/掛起max_execution_time 太短、Xdebug 開銷大調大 max_execution_time;關閉 xdebug.mode=profile/trace 除非需要
copyright © 萬盛學電腦網 all rights reserved