Configure Xdebug
Xdebug is an extension for debugging your PHP. Although you can use an IDE of your choice, the following explains how to configure Xdebug and PhpStorm to debug in your local environment.
To enable Xdebug, you must configure a file in your Git repository, configure your IDE, and set up port forwarding. You can configure some settings in the magento.app.yaml
file. After editing, push the Git changes across all Starter environments and Pro integration environments to enable Xdebug. Xdebug is already available in Pro Staging & Production environments.
Once configured, you can debug CLI commands, web requests, and code. Remember that all cloud infrastructure environments are read-only. Clone the code to your local development environment to perform debugging. For Pro Staging and Production environments, see additional instructions for Xdebug.
Requirements
To run and use Xdebug, you need the SSH URL for the environment. You can locate the information through the Cloud Console or your Cloud Onboarding UI.
Configure Xdebug
To configure Xdebug, follow these steps:
Get started with a branch
To add Xdebug, Adobe recommends working in a development branch.
Enable Xdebug in your environment
You can enable Xdebug directly to all Starter environments and Pro integration environments. This configuration step is not required for Pro Production & Staging environments. See Debug for Pro Staging and Production.
To enable Xdebug for your project, add xdebug
to the runtime:extensions
section of the .magento.app.yaml
file.
To enable Xdebug:
-
In your local terminal, open the
.magento.app.yaml
file in a text editor. -
In the
runtime
section, underextensions
, addxdebug
. For example:code language-yaml runtime: extensions: - redis - xsl - newrelic - sodium - xdebug
-
Save your changes to the
.magento.app.yaml
file and exit the text editor. -
Add, commit, and push the changes to redeploy the environment.
code language-bash git add -A
code language-bash git commit -m "Add xdebug"
code language-bash git push origin <environment-ID>
When deployed to Starter environments and Pro integration environments, Xdebug is now available. Continue configuring your IDE. For PhpStorm, see Configure PhpStorm.
Configure PhpStorm
The PhpStorm IDE must be configured to properly work with Xdebug.
To configure PhpStorm to work with Xdebug:
-
In your PhpStorm project, open the Settings panel.
- macOS—Select PhpStorm > Preferences.
- Windows/Linux—Select File > Settings.
-
In the Settings panel, expand and locate the Languages & Frameworks > PHP > Servers section.
-
Click the + to add a server configuration. The project name is in gray at the top.
-
[Optional] Configure the following settings for the new server configuration. See No debug server configured in the PHPStorm documentation.
- Name—Enter the same as the hostname. This value must match the value for the
PHP_IDE_CONFIG
variable in Debug CLI commands to use CLI for debugging. - Host—Enter the hostname.
- Port—Enter
443
. - Debugger—Select
Xdebug
.
- Name—Enter the same as the hostname. This value must match the value for the
-
Select Use path mappings. In the File/Directory pane, the root of the project for the
serverName
displays. -
In the Absolute path on the server column, click the Edit icon and add a setting based on the environment.
-
For all Starter environments and Pro integration environments, the remote path is
/app
. -
For Pro Staging and Production environments:
- Production:
/app/<project_code>/
- Staging:
/app/<project_code>_stg/
- Production:
-
-
Change the Xdebug port to 9000 in the Languages & Frameworks > PHP > Debug > Xdebug > Debug Port panel.
-
Click Apply.
Set up port forwarding
Map the XDEBUG
connection from the server to your local system. To do any type of debugging, you must forward port 9000 from your Adobe Commerce on cloud infrastructure server to your local machine. See one of the following sections:
Port forwarding on Mac or UNIX®
To set up port forwarding on a Mac or in a UNIX® environment:
-
Open a terminal.
-
Use SSH to establish the connection.
code language-bash ssh -R 9000:localhost:9000 <ssh url>
Use the
-v
(verbose) option so that whenever a socket is connected to the port that is being forwarded it shows in the terminal.If an “unable to connect” or “could not listen to port on remote” error is displayed, there could be another active SSH session persisting on the server that is occupying port 9000. If that connection is not being used, you can terminate it.
To troubleshoot the connection:
-
Use SSH to log in to the remote integration, Staging, or Production environment.
-
View a list of SSH sessions:
who
-
View existing SSH sessions by user. Be careful to not affect a user other than yourself!
- integration: usernames are similar to
dd2q5ct7mhgus
- Staging: usernames are similar to
dd2q5ct7mhgus_stg
- Production: usernames are similar to
dd2q5ct7mhgus
- integration: usernames are similar to
-
For a user session that is older than yours, find the pseudo-terminal (PTS) value, such as
pts/0
. -
Kill the process ID (PID) corresponding to the PTS value.
code language-bash ps aux | grep ssh kill <PID>
Sample response:
code language-terminal dd2q5ct7mhgus 5504 0.0 0.0 82612 3664 ? S 18:45 0:00 sshd: dd2q5ct7mhgus@pts/0
To terminate the connection, enter a kill command with the process ID (PID).
code language-bash kill 3664
Port forwarding on Windows
To set up port forwarding (SSH tunneling) on Windows, you must configure your Windows terminal application. This example steps through creating an SSH tunnel using Putty. You can use other applications such as Cygwin. For more information on other applications, see the vendor documentation provided with those applications.
To set up an SSH tunnel on Windows using Putty:
-
If you have not already done so, download Putty.
-
Start Putty.
-
In the Category pane, click Session.
-
Enter the following information:
- Hostname (or IP address) field: Enter the SSH URL for your Cloud server
- Port field: Enter
22
-
In the Category pane, click Connection > SSH > Tunnels.
-
Enter the following information:
- Source port field: Enter
9000
- Destination field: Enter
127.0.0.1:9000
- Click Remote
- Source port field: Enter
-
Click Add.
-
In the Category pane, click Session.
-
In the Saved Sessions field, enter a name for this SSH tunnel.
-
Click Save.
-
To test the SSH tunnel, click Load, then click Open.
If an “unable to connect” error displays, verify the following:
- All Putty settings are correct
- You are running Putty on the machine on which your private Adobe Commerce on cloud infrastructure SSH keys are located
SSH access to Xdebug environments
For initiating debugging, performing setup, and more, you need the SSH commands for accessing the environments. You can get this information through the Cloud Console and your project spreadsheet.
For Starter environments and Pro integration environments, you can use the following magento-cloud
CLI command to SSH into those environments:
magento-cloud environment:ssh --pipe -e <environment-ID>
To use Xdebug, SSH to the environment as follows:
ssh -R <xdebug listen port>:<host>:<xdebug listen port> <SSH-URL>
For example,
ssh -R 9000:localhost:9000 pwga8A0bhuk7o-mybranch@ssh.us.magentosite.cloud
Debug for Pro Staging and Production
To use Xdebug specifically on Pro plan Staging and Production environment, you create a separate SSH tunnel and web session only you have access to. This usage differs from typical access, only providing access to you and not to all users.
You need the following:
-
SSH commands for accessing the environments. You can get this information through the Cloud Console or your Cloud Onboarding UI.
-
The
xdebug_key
value set when configuring the Staging and Pro environments.The
xdebug_key
can be found by using SSH to log in to the primary node and executing:code language-bash cat /etc/platform/*/nginx.conf | grep xdebug.sock | head -n1
To set up an SSH tunnel to a Staging or Production environment:
-
Open a terminal.
-
Clean up all SSH sessions for each web node of the cluster.
code language-bash ssh USERNAME@CLUSTER.ent.magento.cloud 'rm /run/platform/USERNAME/xdebug.sock'
-
Set up the SSH tunnel for Xdebug for each web node of the cluster.
code language-bash ssh -R /run/platform/USERNAME/xdebug.sock:localhost:9000 -N USERNAME@CLUSTER.ent.magento.cloud
To start debugging using the environment URL:
-
Enable remote debugging; visit the site in the browser and append the following to the URL where
KEY
is value forxdebug_key
.code language-http ?XDEBUG_SESSION_START=KEY
This step sets the cookie that sends browser requests to trigger Xdebug.
-
Complete your debugging with Xdebug.
-
When you are ready to end the session, use the following command to remove the cookie and end debugging through the browser where
KEY
is value forxdebug_key
.code language-http ?XDEBUG_SESSION_STOP=KEY
note note NOTE The XDEBUG_SESSION_START
passed byPOST
requests are not supported.
Debug CLI commands
This section walks through debugging CLI commands.
To debug CLI commands:
-
SSH into the server you want to debug using CLI commands.
-
Create the following environment variables:
code language-bash export XDEBUG_CONFIG='PHPSTORM'
code language-bash export PHP_IDE_CONFIG="serverName=<name of the server that is configured in PHPSTORM>"
These variables are removed when the SSH session ends.
-
Begin debugging
On Starter environments and Pro integration environments, run the CLI command to debug.
You may add runtime options, for example:code language-bash php -d xdebug.profiler_enable=On -d xdebug.max_nesting_level=9999 bin/magento cache:clean
On Pro Staging and Production environments, you must specify the path to the Xdebug PHP configuration file when debugging CLI commands, for example:
code language-bash php -c /etc/platform/USERNAME/php.xdebug.ini bin/magento cache:clean
Debug web requests
The following steps help you debug web requests.
-
On the Extension menu, click Debug to enable.
-
Right click, select the options menu, and set the IDE key to PHPSTORM.
-
Install the Xdebug client on the browser. Configure and enable it.
Example: Chrome setup
This section discusses how to use Xdebug in Chrome using the Xdebug Helper extension. For information about Xdebug tools for other browsers, consult the browser documentation.
To use Xdebug Helper with Chrome:
-
Create an SSH tunnel to the Cloud server.
-
Install the Xdebug Helper extension from the Chrome store.
-
Enable the extension in Chrome as shown in the following figure.
-
In Chrome, right-click the green helper icon in the Chrome toolbar.
-
From the pop-up menu, click Options.
-
From the IDE Key list, click PhpStorm.
-
Click Save.
-
Open your PhpStorm project.
-
In the top navigation bar, click the Start listening icon.
If the navigation bar is not displayed, click View > Navigation Bar.
-
In the PhpStorm navigation pane, double-click the PHP file to test.
Debug local code
Because of the read-only environments, you must pull code to the local workstation from an environment or specific Git branch to perform debugging.
The method you choose is up to you. You have the following options:
-
Check out code from Git and run
composer install
This method works unless
composer.json
references packages in private repositories to which you do not have access. This method results in getting the entire Adobe Commerce codebase. -
Copy the
vendor
,app
,pub
,lib
, andsetup
directoriesThis method results in your having all code you can possibly test. Depending on how many static assets you have, it could result in a long transfer with a large volume of files.
-
Copy the
vendor
directory onlyBecause most of the code is in the
vendor
directory, this method is likely to result in good testing, although are not testing the entire codebase.
To compress files and copy them to your local machine:
-
Use SSH to log in to the remote environment.
-
Compress the files.
code language-bash tar -czf /tmp/<file-name>.tgz <directory list>
For example, to compress the
vendor
directory only:code language-bash tar -czf /tmp/vendor.tgz vendor
-
On your local environment, use PhpStorm to compress the files.
code language-bash cd <phpstorm project root dir>
code language-bash rsync <SSH-URL>:/tmp/<file-name>.tgz .
code language-bash tar xzf <file-name>.tgz