首先确保CentOS服务器已安装PHP、PHP-FPM及开发工具,然后通过以下命令安装Xdebug:
# 安装必要依赖
sudo yum install gcc php-devel php-pear autoconf -y
# 下载并编译Xdebug(以2.5.0为例,建议使用最新稳定版)
wget http://xdebug.org/files/xdebug-2.5.0.tgz
tar xvf xdebug-2.5.0.tgz
cd xdebug-2.5.0
phpize
./configure --enable-xdebug
make
sudo cp modules/xdebug.so /usr/lib64/php/modules/xdebug.so
编辑php.ini文件(路径可通过php --ini确认),添加Xdebug配置:
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9003 # 建议使用非默认端口避免冲突
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=1 # 自动启动调试会话
xdebug.remote_host=127.0.0.1 # 若PhpStorm在本地,设为本地IP;若跨网络需设为本地公网IP
重启PHP-FPM使配置生效:
sudo systemctl restart php-fpm
File -> Settings -> Languages & Frameworks -> PHP。CLI Interpreter右侧齿轮图标,选择Add。SSH Interpreter,输入CentOS服务器的IP、用户名及密码(或密钥)。Interpreter栏指定PHP路径(如/usr/bin/php),点击Finish。Settings -> Languages & Frameworks -> PHP -> Servers。+添加新服务器,填写:
CentOS-Remote);80或443);Use path mappings,将本地项目目录映射到服务器对应目录(如本地/home/user/project→服务器/var/www/html/project)。Settings -> Languages & Frameworks -> PHP -> Debug。Debug port与php.ini中的xdebug.remote_port一致(如9003)。Shift+F9)开始监听调试连接。http://your-server-ip),触发断点后PhpStorm会自动进入调试模式,支持单步执行、查看变量等功能。若服务器与本地不在同一网络,需通过SSH隧道转发端口:
Source Port:9003(本地端口);Destination:your-server-ip:9003(服务器端口);Add,然后连接SSH。php.ini中的xdebug.remote_host为本地公网IP,重启PHP-FPM。9003)的流量:sudo firewall-cmd --add-port=9003/tcp --permanent
sudo firewall-cmd --reload
Path mappings必须正确配置,否则无法定位服务器文件。9003端口被占用,可修改php.ini中的xdebug.remote_port为其他端口(如9004),并同步调整PhpStorm的Debug port。