温馨提示×

centos中phpstorm远程调试方法

小樊
48
2025-07-06 14:19:23
栏目: 编程语言

在CentOS中使用PhpStorm进行远程调试PHP代码,可以按照以下步骤进行配置:

1. 安装Xdebug扩展

首先,确保你的CentOS系统上已经安装了PHP和PHP-FPM。然后,安装Xdebug扩展。

# 安装必要的开发工具和库
sudo yum install gcc php-devel php-pear autoconf

# 下载并解压Xdebug
wget http://xdebug.org/files/xdebug-2.5.5.tgz
tar xvf xdebug-2.5.5.tgz
cd xdebug-2.5.5

# 配置并编译安装Xdebug
phpize
./configure --with-php-config=/usr/bin/php-config
make
sudo make install

# 编辑php.ini文件
echo "zend_extension=/usr/lib64/php/modules/xdebug.so" | sudo tee -a /etc/php.ini
echo "xdebug.remote_enable=1" | sudo tee -a /etc/php.ini
echo "xdebug.remote_host=127.0.0.1" | sudo tee -a /etc/php.ini
echo "xdebug.remote_port=9000" | sudo tee -a /etc/php.ini
echo "xdebug.idekey=PHPSTORM" | sudo tee -a /etc/php.ini
echo "xdebug.remote_autostart=1" | sudo tee -a /etc/php.ini

# 重启PHP-FPM
sudo systemctl restart php-fpm

2. 配置PhpStorm

打开PhpStorm,进入 File -> Settings(或 PhpStorm -> Preferences 如果你使用的是 macOS)。

配置PHP解释器和服务器

  • Languages & Frameworks -> PHP -> CLI Interpreter 中,选择与服务器上安装的PHP版本相匹配的解释器。
  • PHP -> Servers 中,点击 + 号,添加一个新的服务器配置。填写服务器名称、主机名(或IP地址)和端口(与php.ini中的 xdebug.remote_port 一致,例如9000)。

配置Deployment

  • Deployment 选项卡中,点击 + 号,选择 FTPSFTPFTPS,根据你的服务器配置选择合适的协议。
  • 配置远程服务器目录映射,将本地项目目录映射到远程服务器上的目录。

配置Debug

  • PHP -> Debug 中,确保 Debug port 与php.ini中的 xdebug.remote_port 一致。
  • 点击 OK 保存设置。

3. 设置断点和启动调试

在PhpStorm中,打开你想要调试的项目。

  • 在代码中设置断点。
  • 点击工具栏上的绿色虫子图标(或按下 Shift + F9)开始调试。

4. 使用SSH隧道(可选)

如果你的服务器和本地机器不在同一网络中,可以使用SSH隧道来安全地连接到服务器。在Windows上,可以使用PuTTY等工具创建SSH隧道。

例如,使用PuTTY创建SSH隧道:

  • 打开PuTTY,输入服务器的IP地址和SSH端口(通常是22)。
  • Connection -> SSH -> Tunnels 中,输入本地端口(例如9000)和远程端口(例如9000),然后点击 Add
  • 点击 Open 开始连接。

在PhpStorm中配置SSH隧道:

  • Deployment 选项卡中,勾选 Use path mappings
  • SSH/SSL 选项卡中,配置SSH隧道,输入本地端口和远程端口。

完成以上步骤后,你就可以在PhpStorm中进行远程调试了。

0