温馨提示×

centos phpstorm如何调试

小樊
50
2026-09-27 02:09:52
栏目: 编程语言

在 CentOS 上使用 PhpStorm 调试 PHP,通常是通过 Xdebug 来实现。下面给你一套完整、可操作的步骤(以 PHP 7.x / 8.x + Xdebug 3 为例)。


一、在 CentOS 上安装 Xdebug

1. 安装 PHP 及开发包

yum install php php-cli php-devel

2. 安装 Xdebug

CentOS 推荐用 pecl:

yum install php-pear
pecl install xdebug

安装完成后会提示类似:

You should add "zend_extension=/usr/lib64/php/modules/xdebug.so" to php.ini

二、配置 php.ini

编辑 PHP 配置文件(路径可能不同):

php --ini

一般在:

/etc/php.ini

在末尾添加(Xdebug 3 示例):

[xdebug]
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.start_with_request=yes

如果你用 Docker / 虚拟机,
client_host 要改成 宿主机 IP

重启 PHP:

systemctl restart php-fpm
# 或
systemctl restart httpd

验证:

php -m | grep xdebug

三、配置 PhpStorm

1. 设置 PHP 解释器

  • File → Settings → PHP
  • CLI Interpreter → 选择 CentOS 的 PHP
  • 确认 Xdebug 已识别

2. 配置 Debug 端口

  • Settings → PHP → Debug
  • Debug port: 9003

3. 设置 Server

  • Settings → PHP → Servers
  • 新增:
    • Name: centos
    • Host: localhost 或服务器 IP
    • Port: 80
    • Debugger: Xdebug
    • ✅ Use path mappings(本地 ↔ 服务器路径)

四、启动调试

方式一:PhpStorm 监听(推荐)

  1. 点击右上角 电话图标(Start Listening for PHP Debug Connections)
  2. 浏览器访问 PHP 页面
  3. 在 PhpStorm 中打断点
  4. 自动进入调试

方式二:浏览器插件(可选)

安装:

  • Chrome:Xdebug Helper
  • 设置 IDE Key:PHPSTORM

访问页面即可触发调试


五、常见问题

1️⃣ 断点不生效

  • 检查 path mappings
  • 确认 xdebug.mode=debug
  • 确认端口一致(9003)

2️⃣ 虚拟机 / Docker

xdebug.client_host=192.168.x.x
xdebug.discover_client_host=0

3️⃣ 防火墙

firewall-cmd --add-port=9003/tcp --permanent
firewall-cmd --reload

六、最简速查版

pecl install xdebug
# php.ini
xdebug.mode=debug
xdebug.client_port=9003
# PhpStorm
监听 → 断点 → 访问页面

如果你用的是 Docker / LNMP / 宝塔 / 具体 PHP 版本,告诉我我可以直接给你对应配置。

0 踩