PHP in Gitpod

Gitpod supports PHP right out of the box, but more advanced features such as debugging can be configured, so here is how to do it!

Example Repositories

Repository Description Try it
symfony-demo A PHP/Symfony reference application following best practices Open in Gitpod
magento2gitpod Magento 2 optimized setup for Gitpod: Nginx, MySQL, PHP 7.2, PHP-FPM and more Open in Gitpod
koel A personal music streaming server that works Open in Gitpod
drupal Drupal 9 with MySQL, suitable for site building and contrib module development Open in Gitpod

Debugging PHP in Gitpod

PHP debugging example

The PHP Debug extension allows debugging PHP applications from within Gitpod.

To get this extension for your project, you must do two things:

First, you must create a .gitpod.Dockerfile for your repository:

FROM gitpod/workspace-full

RUN sudo apt-get update -q     && sudo apt-get install -y php-dev

RUN wget http://xdebug.org/files/xdebug-2.9.1.tgz     && tar -xvzf xdebug-2.9.1.tgz     && cd xdebug-2.9.1     && phpize     && ./configure     && make     && sudo mkdir -p /usr/lib/php/20190902     && sudo cp modules/xdebug.so /usr/lib/php/20190902     && sudo bash -c "echo -e '\nzend_extension = /usr/lib/php/20190902/xdebug.so\n[XDebug]\nxdebug.remote_enable = 1\nxdebug.remote_autostart = 1\n' >> /etc/php/7.4/cli/php.ini"

Second, reference the above Dockerfile in a .gitpod.yml file, and then also install the extension, like so:

image:
  file: .gitpod.Dockerfile
# This is to get rid of the annoying popup feel free to leave this out
ports:
  - port: 9000
    onOpen: ignore
vscode:
  extensions:
    - felixfbecker.php-debug@1.13.0:WX8Y3EpQk3zgahy41yJtNQ==

Next create a new directory called .theia and in that directory add a file called launch.json and add the following content.

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "php",
      "name": "Launch",
      "request": "launch",
      "stopOnEntry": true,
      "program": "${file}",
      "cwd": "${workspaceRoot}",
      "externalConsole": false
    }
  ]
}

Then have fun debugging PHP

Finally, here is a full example repository containing the complete Gitpod PHP debug configuration described above. You can try it by clicking here:

Open in Gitpod

PECL Package Manager

Gitpod’s default workspace image also comes with the PECL package manager pre-installed. To install packages with it, you must use sudo pecl install <EXTENSION> in your repository’s .gitpod.Dockerfile, e.g. like so:

FROM gitpod/workspace-full

RUN sudo pecl channel-update pecl.php.net &&     sudo pecl install <EXTENSION>

where <EXTENSION> is the PHP extension you want to install, e.g. xdebug.

Further Reading

Was this helpful?