PhpStorm配置WSL中PHP解释器需选Remote Interpreter→WSL Configuration,Distribution选对应发行版,PHP路径填/usr/bin/php(Linux格式),并正确设置Xdebug client_host为WSL2主机IP。

PhpStorm 能直接调用 WSL 中的 PHP、Composer、Node.js 等工具,但默认不自动识别 —— 关键在于正确配置工具链路径和解释器位置,而不是靠“自动检测”。
WSL 中的 PHP/Composer 路径怎么填?
PhpStorm 不会自动把 /usr/bin/php 映射成 Windows 可执行路径。你必须显式告诉它:这个 Linux 二进制文件,要通过 wsl.exe 启动。
- 在
Settings → Languages & Frameworks → PHP中,点击...设置解释器 - 选择
Remote Interpreter → WSL Configuration(不是 “Local” 或 “Docker”) - “Distribution” 下拉框选已安装的发行版(如
Ubuntu-24.04),它会自动填充wsl.exe -d Ubuntu-24.04 - PHP 路径填
/usr/bin/php(注意是 Linux 路径格式,不是C:\Windows\System32\wsl.exe) - Composer 路径同理填
/usr/bin/composer或/home/username/.composer/vendor/bin/composer
填错最常见表现:Cannot resolve PHP executable 或 Connection refused —— 本质是 PhpStorm 尝试用 Windows 方式去执行 Linux 路径,失败后没报具体原因。
为什么 PhpStorm 里运行脚本提示 “No such file or directory”?
错误信息像这样:/bin/sh: /mnt/c/Users/name/project/script.sh: No such file or directory,说明脚本本身在 Windows 文件系统(C:\)下,但 WSL 没有执行权限或 shebang 解析失败。
Linux AI 服务器 — 将 Linux 服务器转换为本地 AI 推理集群。使用 systemd、NVIDIA CUDA 实现无头 Linux AI,零 GUI 开销。
立即学习“PHP免费学习笔记(深入)”;
- 确保脚本第一行是有效 shebang,例如:
#!/usr/bin/env bash(不能是#!/bin/bash如果 WSL 中该路径不存在) - 不要把项目放在
C:\根目录或 OneDrive 同步目录下 —— WSL 对 NTFS 上的可执行位支持不稳定 - 推荐把项目放 WSL 内部路径,比如
/home/username/myproject;若必须用 Windows 路径,需在 WSL 中手动chmod +x script.sh - PhpStorm 的 “Run Configuration” 中,“Working directory” 建议设为
$ProjectFileDir$,并勾选Use path mappings,否则路径传递会出错
PHP Debug(Xdebug)在 WSL 下连不上 PhpStorm?
不是 Xdebug 配置错了,而是网络通信被阻断 —— WSL2 默认使用虚拟网卡,localhost 在 WSL 里指向自己,不是 Windows 主机。
- Xdebug 配置中,
xdebug.client_host必须设为host.docker.internal(仅限 WSL2 + Docker 场景)或更通用的172.28.0.1(需查 WSL2 主机 IP) - 更稳的方式:在 WSL 中运行
cat /etc/resolv.conf | grep nameserver,取nameserver后的 IP(通常是172.x.x.1),填入xdebug.client_host - PhpStorm 中
Settings → Languages & Frameworks → PHP → Debug的 “Debug port” 要与xdebug.client_port一致(默认 9003) - 别忘了在 WSL 的
php.ini里启用xdebug.mode=debug,且禁用xdebug.start_with_request=yes(避免所有请求都触发调试)
最容易被忽略的是:WSL2 的防火墙规则默认放行 9003 端口,但某些企业版 Windows 组策略会拦截,此时需手动在 PowerShell(管理员)中运行:New-NetFirewallRule -DisplayName "Allow Xdebug WSL2" -Direction Inbound -Protocol TCP -LocalPort 9003 -Action Allow。

















