温馨提示×

centosphpstorm怎么调试

小樊
45
2026-09-06 01:23:36
栏目: 编程语言

CentOS 上使用 PhpStorm 调试 PHP,通常是通过 Xdebug 来实现。下面给你一套从零开始、可用的完整流程(适合本地或远程服务器)。


一、环境说明(示例)

  • 系统:CentOS 7 / 8
  • Web 服务:Nginx 或 Apache
  • PHP:PHP 7.x / 8.x
  • 调试工具:Xdebug
  • IDE:PhpStorm(Windows / macOS / Linux 均可)

二、安装 Xdebug(CentOS)

1. 安装 PHP 及对应开发包

yum install php php-cli php-devel -y

2. 安装 Xdebug

yum install php-pear gcc
pecl install xdebug

安装完成后会看到类似:

/usr/lib64/php/modules/xdebug.so

三、配置 Xdebug(php.ini)

1. 找到 php.ini

php --ini

2. 添加 Xdebug 配置

编辑 php.ini(或 /etc/php.d/xdebug.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   ; 你的 PhpStorm 所在电脑 IP
xdebug.client_port=9003
xdebug.idekey=PHPSTORM

⚠️ 如果是 Docker / 虚拟机,client_host 很重要

3. 重启 PHP

systemctl restart php-fpm
# 或
systemctl restart httpd

4. 验证

php -m | grep xdebug

四、配置 PhpStorm

1. 设置 PHP 解释器

  • File → Settings → PHP
  • CLI Interpreter → 添加
    • 本地:直接选 PHP
    • 远程:使用 SSH / Docker / WSL

2. 配置 Debug

  • Settings → PHP → Debug
  • 确认:
    • Debug Port: 9003
    • 勾选 Can accept external connections

3. 配置 Servers

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

五、启动调试

方式一:浏览器触发(推荐)

  1. PhpStorm 点 电话图标(Listen)
  2. 浏览器安装插件:
    • Xdebug Helper(Chrome)
  3. 打开网页并点 Debug

URL 也可手动加参数:

?XDEBUG_SESSION=PHPSTORM

方式二:PHP CLI 调试

export XDEBUG_SESSION=PHPSTORM
php test.php

六、常见问题

❌ 断点不生效

  • 没开 Listen
  • path mapping 错
  • client_host 不对

❌ 端口被占用

netstat -tunlp | grep 9003

❌ Docker 场景

  • client_host=host.docker.internal

七、快速检查清单 ✅

  • [ ] Xdebug 已加载
  • [ ] PhpStorm 在监听
  • [ ] IP / Port 正确
  • [ ] 路径映射正确

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

  • ✅ CentOS 版本
  • ✅ PHP 版本
  • ✅ 本地还是远程调试
  • ✅ Nginx / Apache / Docker

我可以给你量身定制的配置示例

0