Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Install Xdebug PHP and Setup PHPStorm via CLI without Apache

Vinh Jacker | 11-11-2024

How to Install Xdebug PHP and Setup PHPStorm via CLI without Apache

When developing PHP applications, debugging is essential for identifying and fixing issues. Xdebug will be a useful tool that helps you to debug your PHP code effectively.

In this article, we’ll explore how to install Xdebug and set up PHPStorm via CLI without Apache. Let’s dive in now!

Step 1: PHP and Xdebug installation

  • The first step is to confirm whether PHP has already been installed on your system. If it’s not, you’ll need to install it, which varies depending on your operating system. For instance, Ubuntu users can run this code:
sudo apt update
sudo apt install php8.3
  • The next step is to install the Xdebug extension compatible with your PHP version. Here is how:
    sudo apt-get install php{php_version}-xdebug
    

    Ex: sudo apt-get install php8.3-xdebug

  • Verify the Xdebug installation by running:

php -v

Check Xdebug

  • Xdebug is shown in that version.

Step 2: Xdebug in xdebug.ini configuration

Open the xdebug.ini file for editing

  • Choose one of these commands to open the xdebug file.
    sudo gedit /etc/php/8.3/mods-available/xdebug.ini
    or
    sudo nano /etc/php/8.3/mods-available/xdebug.ini
    

    Copy and paste the content to a specific file

  • For Xdebug 2, you need to add the following configuration:
    [Xdebug]
    zend_extension="xdebug.so"
    xdebug.mode=debug,develop
    xdebug.start_with_request=default
    xdebug.idekey=PHPSTORM
    xdebug.client_port=9000 #run on port 9000
    xdebug.file_link_format=phpstorm://open?%f:%l
    
  • Insert the following configuration for Xdebug 3:
    #zend_extension=/usr/lib/php/20151012/xdebug.so
    xdebug.remote_autostart = 1
    xdebug.remote_enable = 1
    xdebug.remote_handler = dbgp
    xdebug.remote_host = 127.0.0.1
    xdebug.remote_log = /tmp/xdebug_remote.log
    xdebug.remote_mode = req
    xdebug.remote_port = 9000  #run on port 9000
    
  • Save the file.

  • Then, restart Apache with this content:

sudo service apache2 restart

Step 3: PhpStorm configuration

  • Open PhpStorm and navigate to Settings

  • Click on PHP.

Configure PhpStorm

  • Go to the Debug section and set the Xdebug port to 9000 (as specified in xdebug.ini).

Debug port

  • Click the call icon to turn on listening at the configured port.

Php Debug connections

Step 4: Xdebug testing

Create any php file

For example test.php. Be careful with name files.

<?php
$a = 1;
trigger_error($a);
echo $a;

Set Breakpoint

Set breakpoint

Run the newly created php file when there is XDEBUG_SESSION_START=”PHPSTORM”, the PHP server will receive this variable and send data through the configured port:

XDEBUG_SESSION_START="PHPSTORM" php test.php

Other way to use with Magento CLI

XDEBUG_SESSION_START="PHPSTORM" php bin/magento se:up

The result is right when the screen appears the content below:

Breakpoint result

Note: Possible Error Encountered

If Xdebug doesn’t run, use the command to check it first because the error will have a Message to check.

Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9000 (through xdebug.client_host/xdebug.client_port).

Error encountered

Looking at the message above, the issue lies in debugging the client, which is related to PHPStorm. The IDE is not responding to PHP at this time.

How to fix this issue:

  1. Check PHPStorm Options: Explore the options in PHPStorm related to debugging such as resolve breakpoint if it is not available on the current line (Xdebug 2.8+), force break at first line when no path mapping specified, and force break at first line when a script is outside the project and try to turn on these options.

  2. Restart PHPStorm: After making any changes, restart PHPStorm to see if the issue is resolved.

PHPStorm

In conclusion

By following these detailed 4 steps, you can install Xdebug and set up PHPStorm for CLI debugging without relying on Apache. Enjoy effective PHP debugging directly from the command line.

If you have any questions while following this tutorial, feel free to ask!

Table of content
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services