Ubuntu中PhpStorm遠程調試設置指南
在Ubuntu遠程服務器上,通過終端安裝Xdebug擴展(以Ubuntu 22.04+、PHP 8.1為例):
sudo apt update
sudo apt install php-xdebug
安裝完成后,Xdebug會自動集成到PHP環境中,但需進一步配置才能啟用遠程調試功能。
編輯PHP配置文件(根據PHP運行模式選擇對應文件,如Apache使用/etc/php/8.1/apache2/php.ini
,PHP-FPM使用/etc/php/8.1/fpm/php.ini
),在文件末尾添加以下配置:
[xdebug]
zend_extension=xdebug.so ; Xdebug擴展路徑(通常自動識別,無需手動指定)
xdebug.mode=debug ; 啟用調試模式
xdebug.client_host=127.0.0.1 ; 本地機器IP(PhpStorm所在設備)
xdebug.client_port=9003 ; 調試端口(默認9003,需與PhpStorm一致)
xdebug.start_with_request=yes ; 自動啟動調試(觸發請求時自動連接)
xdebug.idekey=PHPSTORM ; IDE標識(需與PhpStorm設置一致)
注意:若服務器有多個IP或需允許遠程連接,可將client_host
改為本地機器的實際IP(如192.168.1.100
)。
配置生效后,重啟Apache或Nginx服務:
# Apache
sudo systemctl restart apache2
# Nginx + PHP-FPM
sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm
File > Settings > PHP
(Windows/Linux)或PhpStorm > Preferences > PHP
(macOS)。Add
,選擇Remote
(遠程解釋器)。Interpreter
欄選擇SSH Credentials
,輸入遠程服務器的Host(IP/域名)、Port(默認22)、Username(登錄用戶名)。Password
或Key pair
),若用密鑰需指定私鑰文件路徑。Test Connection
驗證連接,成功后點擊OK
保存。File > Settings > PHP > Servers
,點擊**+**添加新服務器。Name
(如Remote Server
),Host
(遠程服務器IP/域名),Port
(HTTP端口,默認80;HTTPS為443)。Use path mappings
(路徑映射),將本地項目目錄與服務器項目目錄關聯(如本地/home/user/project
→服務器/var/www/html/project
)。OK
保存。Run > Edit Configurations
,點擊**+**添加PHP Remote Debug
配置。Server
(如Remote Server
),Debugger mode
保持Attach to remote host
(默認)。IDE key
為PHPSTORM
(與Xdebug配置一致)。OK
保存配置。XDEBUG_SESSION_START=PHPSTORM
參數(如http://example.com/index.php?XDEBUG_SESSION_START=PHPSTORM
)。Debug
模式,訪問頁面即可觸發。9003
端口被占用,可修改php.ini
中的client_port
和PhpStorm中的Debug port
為其他端口(如9007
)。9003
端口(sudo ufw allow 9003
),允許遠程連接。