Setup php on wsl with vscode
To get PHP installed on my WSL2 instance running Ubuntu 20.04.3 and code editing from VS Code on Windows side:
sudo apt install php7.4 php7.4-curl php7.4-xml php7.4-zip php7.4-mbstring php7.4-gd php7.4-xdebug sudo apt install php7.4-dev
Remote - WSL open any folder in WSL, which I already had installed
VSCode Preferences, Settings. Search for PHP settings (there are some linters ready to go - need path for php). I’ve not set these up.
- disabled PHP Suggest Basic
- disabled PHP Validate Enable
https://code.visualstudio.com/docs/languages/php suggests running
PHP Debug with XDebug for debugging download
phpinfo(); and ran debug in VSCode
Copied output to https://xdebug.org/wizard
Install XDebug on WSL
wget tar -xvzf xdebug-3.1.5.tgz ## had all the extensions I needed tar -xvzf xdebug-3.1.5.tgz cd xdebug-3.1.5 phpize ./configure make sudo cp modules/xdebug.so /usr/lib/php/20190902 ## zend_extension = xdebug sudo vim /etc/php/7.4/cli/conf.d/20-xdebug.ini
Note - the error
Cannot load Xdebug - it was already loaded was due to me adding extra lines a the bottom of
php.ini which I didn’t need:
/etc/php/7.4/cli and also in
# didn't need these added to bottom of php.ini zend_extension="/usr/lib/php/20190902/xdebug.so" xdebug.mode = debug xdebug.start_with_request = yes
I can get a debug console working, but expected a browser window to work, which hasn’t so far.
F5 doesn’t seem to start debugging, but the icon does. Error has gone away as I took out the php.ini settings.
I got enough working to debug my issue. https://www.cloudways.com/blog/php-debug/ contains deeper debugging.
Rudimentary logging to say the least, but it works out of the box :-)
// shows up in /var/log/apache2/error.log error_log('Your message here');
<?php echo "hello"; // apparently best practise to not do a php closing tag anymore // end of file