在CentOS中使用PhpStorm进行远程调试PHP代码,可以按照以下步骤进行配置:
首先,确保你的CentOS系统上已经安装了PHP和PHP-FPM。然后,安装Xdebug扩展。
# 安装必要的开发工具和库
sudo yum install gcc php-devel php-pear autoconf
# 下载并解压Xdebug
wget http://xdebug.org/files/xdebug-2.5.0.tgz
tar xvf xdebug-2.5.0.tgz
cd xdebug-2.5.0
# 配置并编译安装Xdebug
phpize
./configure --enable-xdebug
make
sudo cp modules/xdebug.so /usr/lib64/php/modules/xdebug.so
# 编辑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=9003" | 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
打开PhpStorm,配置远程调试设置。
打开PhpStorm设置:在PhpStorm中,选择 File
> Settings
(Windows/Linux)或 PhpStorm
> Preferences
(macOS)。
配置PHP解释器:在左侧面板中,选择 Languages & Frameworks
> PHP
> CLI Interpreter
。点击右侧的齿轮图标,然后选择 Add
。输入一个名称,选择 SSH Interpreter
,然后输入你的CentOS服务器的IP地址、用户名和密码。在 Interpreter
部分,选择 Path to PHP executable
。这应该是你的CentOS服务器上的PHP可执行文件路径,例如 /usr/bin/php
。在 Additional Options
部分,添加以下参数:
-xdebug.start_with_request=yes
-xdebug.client_host=your_local_ip
-xdebug.client_port=9003
将 your_local_ip
替换为你的本地计算机IP地址。
配置调试服务器:在左侧面板中,选择 Run
> Edit Configurations
。点击左上角的加号按钮,选择 PHP Web Page
。在右侧配置面板中,填写以下信息:
http://your_server_ip:9003
。Xdebug
。启动监听:在PhpStorm的工具栏中,点击电话听筒图标(远程调试按钮),启动远程调试监听。
在你的CentOS服务器上,通过浏览器访问你的应用程序。确保URL中包含 XDEBUG_SESSION_START=PHPSTORM
参数,例如:
http://your_server_ip_or_domain/your_application?XDEBUG_SESSION_START=PHPSTORM
这将启动一个调试会话。当代码执行到断点时,PhpStorm将会捕获并允许你进行调试。
在调试过程中,你可以使用PhpStorm的调试工具栏上的按钮来控制程序的执行,如继续执行(F8)、单步执行(F7)、逐过程执行(Shift+F8)等。你还可以查看变量的值和调用栈,以便分析和调试代码。
通过以上步骤,你就可以在CentOS上使用PhpStorm进行远程调试PHP代码了。如果有任何问题,请检查配置文件和端口设置是否正确。