能否转发Xdebug连接?跨虚拟主机调试隧道搭建求助
Absolutely, this scenario is totally feasible! The key is to override Xdebug's default behavior of connecting back to the request initiator (H1) and force it to target your local Windows machine instead. Here's a step-by-step breakdown to make it work:
1. Adjust H2's Xdebug Configuration (php.ini)
Since H2 is handling the incoming cURL request from H1, we need to modify its PHP settings to point Xdebug directly to your local Windows IP. Open H2's php.ini (make sure you edit the one used by its web server, not CLI unless specifically needed) and update these values:
xdebug.remote_enable = 1 xdebug.remote_host = YOUR_LOCAL_WINDOWS_LAN_IP # e.g., 192.168.1.100 xdebug.remote_port = 9000 # Match your PhpStorm Xdebug port xdebug.remote_autostart = 1 # Auto-trigger debugging without manual session parameters xdebug.remote_connect_back = 0 # Critical: Disable auto-connect to the request initiator (H1)
After saving, restart H2's web server (Apache/Nginx) to apply the changes.
2. Configure Local Firewall Access
Make sure your Windows 10 firewall allows incoming connections on port 9000 (or whatever port you set for Xdebug). Create an inbound rule to permit traffic from your Linux VM's IP range to this port—this ensures H2's Xdebug can reach your local machine.
3. Set Up PhpStorm for Remote Debugging
- Enable Debug Listening: Click the "Start Listening for PHP Debug Connections" icon (looks like a telephone) in PhpStorm's toolbar to activate debugging mode.
- Configure Server Mapping:
- Go to
File > Settings > Languages & Frameworks > PHP > Servers - Add a new server:
- Name: Any descriptive name (e.g., "H2 Remote Server")
- Host: H2's domain or IP address (the exact value you use in your cURL request)
- Port: H2's web port (80 for HTTP, 443 for HTTPS)
- Debugger: Select "Xdebug"
- Check "Use path mappings", then map H2's Symfony project root directory (e.g.,
/var/www/h2) to the corresponding directory on your local Windows machine. This ensures PhpStorm can match remote code to your local files for accurate breakpoint handling.
- Go to
4. Test the Setup
- Set a breakpoint in H2's Symfony code (e.g., inside a controller method that handles the cURL request endpoint).
- On H1, run your CLI command that triggers the cURL request to H2 (e.g.,
curl http://h2.example.com/api/your-target-endpoint). - PhpStorm should immediately catch the debugging session, and you can step through H2's code as if you were triggering the request directly from Postman.
Quick Checks to Avoid Issues
- Verify your Linux VM can reach your local Windows IP: Run
ping YOUR_LOCAL_WINDOWS_IPfrom the VM to confirm connectivity. - Double-check H2's Xdebug settings: Create a
phpinfo.phpfile in H2's web root and load it in a browser to ensure the modifiedphp.inivalues are active. - Since you're using PHP 7.2, Xdebug 2.9.x is the latest compatible version—stick to the config parameters above (Xdebug 3 uses different syntax, which isn't relevant here).
内容的提问来源于stack exchange,提问作者Christian Rosick

