1. 安装与准备
bin/phpstorm.sh启动;或使用snap install phpstorm --classic(推荐,便于后续更新)。安装完成后,确保PHPStorm具有访问网络和文件的权限。sudo apt install php php-cli php-mbstring php-xml)、Web服务器(Apache/Nginx)及SSH服务(sudo apt install openssh-server)。启动SSH服务:sudo systemctl start sshd,并设置开机自启:sudo systemctl enable sshd。2. 配置远程服务器连接(Deployment)
File > Settings > Build, Execution, Deployment > Deployment,点击+添加新配置,选择SFTP(常用传输协议)。Connection标签页填写服务器信息:
192.168.1.100);22,若修改需同步调整服务器配置);debian);Password(输入密码)或Key pair(推荐,提前将本地公钥~/.ssh/id_rsa.pub添加到服务器~/.ssh/authorized_keys)。Test SFTP connection验证连接,成功后会显示绿色勾选框。Mappings标签页,设置本地项目目录(如/home/user/my_project)与远程服务器目录(如/var/www/html/my_project)的映射,确保文件同步路径正确。3. 配置远程PHP解释器
File > Settings > Languages & Frameworks > PHP,点击CLI Interpreter右侧的齿轮图标,选择Add。SSH Interpreter,点击...选择之前配置的Deployment服务器(避免重复输入连接信息),或手动填写服务器信息。Interpreter字段,点击...浏览远程服务器上的PHP可执行文件路径(通常为/usr/bin/php,可通过which php命令获取)。/usr/local/bin/composer),便于依赖管理。OK保存,PHPStorm将通过SSH调用远程PHP解释器执行代码(如运行脚本、安装依赖)。4. 配置Xdebug远程调试
sudo apt install php-xdebug(根据PHP版本调整,如php7.4-xdebug、php8.1-xdebug)。php --ini找到php.ini文件路径(如/etc/php/8.1/cli/php.ini),添加以下配置:zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes ; 自动启动调试(可选:trigger/yes)
xdebug.client_host=192.168.1.100 ; 本地电脑IP(需与PHPStorm所在机器一致)
xdebug.client_port=9003 ; 调试端口(默认9003,需与PHPStorm配置一致)
xdebug.idekey=PHPSTORM ; IDE标识(需与PHPStorm设置一致)
sudo systemctl restart apache2;若使用Nginx+PHP-FPM,运行sudo systemctl restart php8.1-fpm。File > Settings > Languages & Frameworks > PHP > Servers,点击+添加服务器,填写:
Debian Server);192.168.1.100);80);Xdebug。Run > Edit Configurations,点击+添加PHP Remote Debug配置,选择刚才添加的服务器,点击OK。电话图标(Start Listening for PHP Debug Connections),在浏览器中访问远程项目时添加参数XDEBUG_SESSION=PHPSTORM(可通过Chrome插件“Xdebug helper”一键设置),刷新页面即可触发断点。5. 文件同步设置
Deployment配置中,切换至Options标签页,设置自动同步策略:
node_modules、.git)。Deployment > Upload to...,选择目标服务器即可。注意事项
ping命令测试);~/.ssh/id_rsa)权限为600,且公钥已添加到服务器~/.ssh/authorized_keys;client_host需填写本地电脑的实际IP(若服务器与本地在同一局域网,可使用局域网IP;若跨网络,需使用公网IP或配置SSH反向隧道);9003被占用,可在php.ini和PHPStorm中修改为其他端口(如9004),并确保防火墙允许该端口通过(sudo ufw allow 9003/tcp)。