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),允许远程连接。