温馨提示×

phpstorm centos调试如何设

小樊
36
2026-10-20 10:52:36
栏目: 编程语言

下面以 PhpStorm + Xdebug 在 CentOS 上调试 PHP 为例,给你一个完整、可落地的配置步骤(常用场景:本地代码 → CentOS 上跑 PHP)。


一、CentOS 上安装 Xdebug

1. 查看 PHP 版本

php -v

2. 安装 Xdebug(以 PHP 7.4 为例)

yum install php-pear php-devel
pecl install xdebug

如果是 PHP 8.x,同样用 pecl install xdebug


二、配置 php.ini(关键)

找到 php.ini:

php --ini

在末尾添加(或修改):

[xdebug]
zend_extension=/usr/lib64/php/modules/xdebug.so

xdebug.mode=debug
xdebug.start_with_request=trigger
xdebug.client_host=192.168.1.100   ; 你电脑的 IP(PhpStorm 所在机器)
xdebug.client_port=9003
xdebug.idekey=PHPSTORM

⚠️ client_host 不是 CentOS IP,而是你 Windows / Mac 的 IP

重启 PHP:

systemctl restart php-fpm
# 或
systemctl restart httpd

验证:

php -m | grep xdebug

三、PhpStorm 配置

1. 配置 PHP 解释器

Settings → PHP → CLI Interpreter
→ + → From Docker / Remote / Local
(如果是远程 CentOS,用 SSH Interpreter)

2. Debug 端口

Settings → PHP → Debug
→ Xdebug → Debug port: 9003

3. DBGP Proxy(可选)

Settings → PHP → Debug → DBGp Proxy
IDE key: PHPSTORM
Host: CentOS IP
Port: 9003

四、设置调试入口

方式一:浏览器触发(最常用)

  1. 安装浏览器插件
    • Chrome:Xdebug Helper
  2. 设置 IDE Key = PHPSTORM
  3. 在 PhpStorm 点 电话图标(Listen for Debug Connections)
  4. 刷新页面 → 自动断点

方式二:Postman / curl

curl "http://centos-ip/test.php?XDEBUG_TRIGGER=1"

或在 Header 加:

Cookie: XDEBUG_SESSION=PHPSTORM

五、路径映射(非常重要)

Run → Edit Configurations
→ PHP Web Page
→ Server
→ 设置:
   CentOS路径: /var/www/html
   Local路径: D:/project

否则会显示:

“Cannot find file in project”


六、常见问题

1. 连不上

  • 防火墙:
firewall-cmd --add-port=9003/tcp --permanent
firewall-cmd --reload
  • SELinux:
setsebool -P httpd_can_network_connect 1

2. 断点不生效

  • 没开 Listen
  • 路径映射错
  • xdebug.mode 不是 debug

七、最小可用配置总结

CentOS php.ini

xdebug.mode=debug
xdebug.client_host=你的IP
xdebug.client_port=9003

PhpStorm

  • Listen 打开
  • 浏览器 Xdebug Helper
  • 打断点

如果你愿意,可以告诉我:

  • PHP 版本
  • PHP-FPM / Apache
  • 本地 Windows 还是 Mac
  • 是否 Docker

我可以给你精确到命令的配置方案。

0 踩