首先确保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 xvzf xdebug-2.5.0.tgz
cd xdebug-2.5.0
# 编译安装Xdebug
phpize
./configure --enable-xdebug
make
sudo cp modules/xdebug.so /usr/lib64/php/modules/xdebug.so
配置php.ini文件:
编辑全局php.ini(通常位于/etc/php.ini),在文件末尾添加以下配置(关键参数说明见下文):
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1 # 若PhpStorm在本地,用127.0.0.1;若在远程,改为本地IP
xdebug.remote_port=9003 # 默认9000,建议改为9003(避免与PHP-FPM端口冲突)
xdebug.idekey=PHPSTORM # PhpStorm标识,需与后续配置一致
xdebug.remote_autostart=1 # 自动启动调试会话
xdebug.remote_handler=dbgp # 调试协议(固定值)
保存后重启PHP-FPM使配置生效:
sudo systemctl restart php-fpm
File -> Settings(macOS为PhpStorm -> Preferences)。Languages & Frameworks -> PHP,点击CLI Interpreter右侧的齿轮图标,选择Add。SSH Interpreter,输入CentOS服务器的IP地址、用户名(如root)和密码,点击Next。Interpreter栏点击...,选择服务器上的PHP可执行文件(通常为/usr/bin/php),点击Finish保存。Run -> Edit Configurations,点击左上角+号,选择PHP Web Page。Server栏点击+号,添加新服务器:
CentOS-Remote);Use path mappings,点击...设置路径映射(将本地项目目录与远程服务器项目目录关联,例如本地/home/user/project映射到远程/var/www/html/project)。OK保存服务器配置。Run -> Edit Configurations中,选择PHP Web Page配置,切换至PHP标签页:
xdebug.remote_port一致的端口(如9003);Servers标签页,确认Ide key与php.ini中的xdebug.idekey一致(PHPSTORM)。http://your_server_ip/your_script.php?XDEBUG_SESSION_START=PHPSTORM
或使用浏览器扩展(如Xdebug Helper for Chrome)开启调试模式。xdebug.remote_port(如9003)的入站流量,可通过以下命令开放端口:sudo firewall-cmd --zone=public --add-port=9003/tcp --permanent
sudo firewall-cmd --reload
xdebug.start_with_request=yes替代xdebug.remote_autostart)。xdebug.remote_log=/tmp/xdebug.log),查看日志确认连接状态。通过以上步骤,即可在CentOS环境下使用PhpStorm进行远程调试,实现断点暂停、变量查看、单步执行等功能。