Skip to content

Instantly share code, notes, and snippets.

@aldyahsn
Created June 14, 2024 10:34
Show Gist options
  • Select an option

  • Save aldyahsn/4ba92246592e1e5707ef2c33709f9ab8 to your computer and use it in GitHub Desktop.

Select an option

Save aldyahsn/4ba92246592e1e5707ef2c33709f9ab8 to your computer and use it in GitHub Desktop.

Install PHP 8.2 and Xdebug in WSL

Add PHP 8.2 Repository and Install PHP 8.2:

sudo apt update
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.2 php8.2-fpm php8.2-cli php8.2-xdebug

Verify PHP and Xdebug Installation:

php -v
php -m | grep xdebug

Configure Xdebug

Locate php.ini file: You can find the php.ini file for the CLI and FPM by running:

php --ini

Edit php.ini file:

Open the appropriate php.ini file and add the following configuration:

zend_extension=xdebug.so

[xdebug]
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003

Restart PHP-FPM Service:

sudo service php8.2-fpm restart

Configure Visual Studio Code

Install PHP Debug Extension:

Open Visual Studio Code, go to the Extensions view, search for PHP Debug, and install the extension by Felix Becker.

Create or Edit launch.json:

Open your project in Visual Studio Code and create or edit the launch.json file:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for Xdebug",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "pathMappings": {
        "/srv/riset-php": "${workspaceFolder}"
      },
      "log": true
    }
  ]
}

Start Debugging

  • Start the Debugger in Visual Studio Code:

Go to the Debug view in Visual Studio Code, select Listen for Xdebug from the dropdown menu, and click on the green play button to start listening for Xdebug.

  • Set Breakpoints:

You can set breakpoints in your PHP code in Visual Studio Code.

  • Access Your Application:

Open your web browser and navigate to your application (e.g., http://localhost). When the code execution reaches a breakpoint, it will pause, and you can inspect variables and step through the code in Visual Studio Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment