温馨提示×

phpstorm centos怎么调试

小樊
43
2026-10-14 16:56:19
栏目: 编程语言

在 CentOS 上使用 PhpStorm 调试 PHP,通常是通过 Xdebug 来实现。下面给你一套从 0 到能断点调试的完整步骤(适用于 PHP 7 / 8 + CentOS 7/8)。


一、CentOS 上安装 Xdebug

1. 查看 PHP 版本

php -v

2. 安装 Xdebug

方式一(推荐):通过 pecl

yum install php-pear php-devel gcc
pecl install xdebug

安装完成后会提示类似:

extension=xdebug.so

方式二(yum)

yum install php-xdebug

二、配置 php.ini

找到 php.ini:

php --ini

在末尾添加(示例,CentOS 常见路径):

[xdebug]
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.idekey=PHPSTORM

PHP 7.2+ 推荐使用 xdebug.mode=debug

重启 PHP:

systemctl restart php-fpm
# 或
systemctl restart httpd

验证:

php -m | grep xdebug

三、PhpStorm 配置

1. 设置 PHP 解释器

Settings → PHP → CLI Interpreter
→ 选择 CentOS 上的 php

2. 配置 Debug

Settings → PHP → Debug
Debug Port: 9003

3. 配置 Server

Settings → PHP → Servers
Name: centos
Host: localhost
Port: 80
Debugger: Xdebug

四、浏览器 / 命令行调试

✅ 浏览器调试(Web)

  1. 安装浏览器插件:
    • Xdebug Helper(Chrome)
  2. 选择 IDE Key = PHPSTORM
  3. PhpStorm 点 “Start Listening for PHP Debug Connections”
  4. 访问页面 → 自动断点

✅ 命令行调试(CLI)

export XDEBUG_MODE=debug
export XDEBUG_CONFIG="idekey=PHPSTORM"
php test.php

五、常见问题

1. 连不上 PhpStorm

  • 防火墙
firewall-cmd --add-port=9003/tcp --permanent
firewall-cmd --reload
  • 确认 PhpStorm 在监听

2. 断点不生效

  • 文件路径不一致(Server mapping)
  • start_with_request=yes

3. Docker / 虚拟机

xdebug.client_host=宿主机IP

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

  • PHP 版本
  • 是否用 Nginx / Apache
  • 是否 Docker / 虚拟机

我可以直接给你精确配置。

0 踩