Debug with XDebug
Understanding XDebug configuration, SSH tunnels, path mapping, and debugging workflows on Cloud.
Debug with XDebug
XDebug on Cloud Overview
What is XDebug?
Purpose
XDebug is a powerful debugging and profiling tool for PHP that allows you to:
- Set breakpoints in code
- Step through execution line by line
- Inspect variables and their values
- Track function calls and stack traces
- Profile performance bottlenecks
Critical Warning
It goes without saying that you shouldn't use this in production.
Why Not Production?
- Significant performance overhead
- Security risk (exposes code paths)
- Can cause timeouts and memory issues
- Affects all users on the environment
Best Practice: Debug on Integration Branch
Instead, sync data and code from the master branch to a new integration branch and debug there.
Workflow:
- Create new integration branch from master
- Sync database from production/staging
- Configure XDebug on integration branch
- Debug issues safely
- Fix code and deploy to production
Configuration Requirements
What You'll Need
This requires a good deal of configuration, but it is quite worth it if you have major problems to troubleshoot.
| Component | Purpose | Configuration Required |
|---|---|---|
| Cloud Environment | XDebug PHP extension enabled | .magento.app.yaml modification |
| PHPStorm | IDE debugging interface | Path mapping, server config |
| SSH Tunnel | Connect IDE to Cloud | Port forwarding setup |
| Browser Extension | Trigger debug sessions | Xdebug Helper, IDE key |
Step 1: Include XDebug in PHP Extensions
Enable XDebug Extension
First Step
You must include xdebug in the PHP extensions and deploy.
Modify .magento.app.yaml
runtime:
extensions:
- redis
- xsl
- newrelic
- sodium
- xdebug
Important Note:
Remember that these changes affect everything but Pro staging and production environments.
- Starter: All environments (master, integration branches)
- Pro: Integration environments only
- Pro Staging/Production: NOT affected (good for safety!)
Deploy Changes
git add .magento.app.yaml
git commit -m "Enable XDebug for debugging"
git push origin integration-debug
After deployment, XDebug extension will be available in the environment. This triggers a full rebuild of the environment.
Step 2: Configure PHPStorm
IDE Path Mapping Setup
Second Step
You must configure PHPStorm with proper path mapping.
Path Mapping Basics
What is Path Mapping?
Path mapping basically is mapping the root project directory to the /app directory.
Directory Mapping:
- Starter & Pro Integration: Local root →
/app - Pro Production: Local root →
/app/{PROJECT} - Pro Staging: Local root →
/app/{PROJECT}_stg
PHPStorm Configuration Steps
1. Add Server Configuration
- Go to Settings → PHP → Servers
- Click + to add new server
- Enter server details:
- Name: Cloud Debug (or your choice)
- Host: Your Cloud environment URL
- Port: 80 or 443
- Debugger: Xdebug
- Check "Use path mappings"
2. Configure Path Mappings
| Environment | Local Path | Remote Path |
|---|---|---|
| Starter / Pro Integration | /path/to/local/project | /app |
| Pro Production | /path/to/local/project | /app/abc123xyz |
| Pro Staging | /path/to/local/project | /app/abc123xyz_stg |
3. Set Debug Configuration
- Go to Settings → PHP → Debug
- Set Debug port: 9000 (or your chosen port)
- Ensure "Can accept external connections" is checked
- Set Max simultaneous connections: 5
Step 3: Establish SSH Tunnel
Port Forwarding Setup
Third Step
You must establish the SSH tunnel to connect PHPStorm to Cloud.
SSH Tunnel Command
ssh -R 9000:localhost:9090 ssh-path--mymagento@ssh.us-4.magento.cloud
Command Breakdown:
ssh -R- Reverse port forwarding9000- Port number on the server (Cloud environment)localhost:9090- Local port number (as configured in PHPStorm)ssh-path--mymagento@ssh.us-4.magento.cloud- Cloud SSH connection string
Port Numbers Explained
Understanding the Ports:
- First port (9000): Port on Cloud environment where XDebug sends data
- Second port (9090): Local port where PHPStorm listens
- Must match PHPStorm debug configuration
- Tunnels XDebug data from Cloud to your local IDE
Getting SSH Connection String
# Get SSH access information
magento-cloud ssh --info -e integration-debug
# Or connect directly
magento-cloud ssh -e integration-debug
Full Example
# Example for integration branch
ssh -R 9000:localhost:9090 abcdefg123-integration-xyz123--mymagento@ssh.us-4.magento.cloud
# Keep this terminal window open during debugging session!
Important!
Keep the SSH tunnel terminal window OPEN during your entire debugging session. Closing it will break the connection.
Step 4: Install Xdebug Helper & Set IDE Key
Browser Extension Setup
Fourth Step
Install the Xdebug helper (and set the correct IDE key).
1. Install Browser Extension
Xdebug Helper Extensions:
- Chrome: Xdebug Helper
- Firefox: Xdebug Helper
- Edge: Xdebug Helper for Microsoft Edge
Search for "Xdebug Helper" in your browser's extension store.
2. Configure IDE Key
Set IDE Key in Extension:
- Click Xdebug Helper icon in browser
- Click Options
- Set IDE key:
PHPSTORM - Save settings
Common IDE Keys:
- PHPStorm: PHPSTORM
- VS Code: VSCODE
- Sublime: sublime.xdebug
3. Enable Listening in PHPStorm
Enable Listening for PHP Connections
- In PHPStorm, click the "Start Listening for PHP Debug Connections" button (phone icon in toolbar)
- Icon should turn green when listening
- Set breakpoint: Click in gutter next to line number
- Configure to "start at first line" if needed
Step 5: Enable XDebug in Browser
Start Debugging Session
Final Step
Enable Xdebug in the browser and refresh the page.
Activate Debugging
- Navigate to the Cloud environment URL in browser
- Click Xdebug Helper icon in toolbar
- Select "Debug" (turns icon green)
- Refresh the page
- PHPStorm should now catch the breakpoint!
Success Indicators:
- PHPStorm opens debugger panel
- Shows current file and line
- Variables panel displays values
- Can step through code (F8, F7, F9)
Complete Step-by-Step Summary
Five Steps to XDebug on Cloud
| Step | Action | Key Details |
|---|---|---|
| 1 | Include xdebug in PHP extensions | Modify .magento.app.yaml, deploy (not on Pro staging/prod) |
| 2 | Configure PHPStorm | Path mapping: root → /app (or /app/{PROJECT} for Pro) |
| 3 | Establish SSH tunnel | ssh -R 9000:localhost:9090 [SSH_PATH] |
| 4 | Install Xdebug helper | Set IDE key (PHPSTORM), enable listening |
| 5 | Enable in browser | Activate debug mode, refresh page |
Troubleshooting Resources
Official Documentation
Further Reading:
- View logs for troubleshooting
- Project structure
- Pro Project log locations
- Starter Project log locations
- Troubleshooting
Common Issues and Solutions
Debugging XDebug Setup
Issue 1: PHPStorm Not Catching Breakpoint
Possible Causes:
- SSH tunnel not established or closed
- Wrong port numbers in tunnel or PHPStorm
- Path mapping incorrect
- IDE key mismatch
Solution:
- Verify SSH tunnel is active (keep terminal open)
- Check ports: 9000 on server, 9090 in PHPStorm
- Verify path mapping: local root → /app
- Confirm IDE key is PHPSTORM
- Restart listening in PHPStorm
Issue 2: XDebug Not Enabled After Deploy
Solution:
- Verify .magento.app.yaml includes xdebug in extensions
- Confirm deployment completed successfully
- SSH to environment and check:
php -m | grep xdebug - If not listed, redeploy with blank commit
Issue 3: Connection Timeouts
Solution:
- XDebug can slow execution significantly
- Increase max_execution_time if needed
- Don't debug heavy processes (imports, reindex)
- Use on integration only, never production
Best Practices
XDebug Usage Guidelines
Do's
- Use integration branch: Never on production
- Sync production data: Debug with real data
- Keep SSH tunnel open: During entire session
- Remove XDebug after debugging: Don't leave enabled
- Document configuration: For team members
- Test fixes without XDebug: Verify performance
Don'ts
- Don't use in production: Performance and security risk
- Don't leave enabled: Remove after debugging
- Don't debug heavy operations: Will timeout
- Don't forget path mapping: Won't work without it
- Don't close SSH tunnel: Breaks connection
Practical Experience
Hands-On Exercise
Required Practice:
Configure and execute a Xdebug session
Exercise Steps:
- Create new integration branch for debugging
- Add xdebug to .magento.app.yaml extensions
- Deploy and wait for completion
- Configure PHPStorm with path mapping
- Establish SSH tunnel
- Install browser extension and set IDE key
- Set breakpoint in controller or block class
- Enable listening in PHPStorm
- Enable debug in browser
- Navigate to page and hit breakpoint
- Step through code, inspect variables
- Fix issue and verify
Exam Tips
Key Points to Remember
- Don't use in production: Performance and security risk
- Debug on integration branch: Sync data from master/production
- Step 1: Include xdebug in PHP extensions (.magento.app.yaml)
- Affects integration only: Not Pro staging/production
- Step 2: Configure PHPStorm with path mapping
- Path mapping: Root → /app (or /app/{PROJECT} for Pro)
- Pro staging: /app/{PROJECT}_stg
- Step 3: Establish SSH tunnel (ssh -R)
- Port numbers: First = server (9000), Second = local (9090)
- Step 4: Install Xdebug helper, set IDE key (PHPSTORM)
- Step 5: Enable in browser, refresh page
- Keep SSH tunnel open: During entire debug session
- Five steps: Extensions, PHPStorm, SSH tunnel, Helper, Browser