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
VS Code
Remote - WSL
open any folder in WSL, which I already had installed
Linter
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
Debug
https://code.visualstudio.com/docs/languages/php suggests running PHP Debug with XDebug
for debugging download
did a 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
php.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:
php.ini in /etc/php/7.4/cli
and also in /etc/php/7.4/apache
# 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
Browser window?
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.
PHP Errors
I got enough working to debug my issue. https://www.cloudways.com/blog/php-debug/ contains deeper debugging.
Logging
https://www.loggly.com/ultimate-guide/php-logging-basics/
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');
Formatting
<?php
echo "hello";
// apparently best practise to not do a php closing tag anymore
// end of file