Setting up a Windows PHP development environment

3 Minute(s) to read

Setting up a local development environment for PHP on Windows has proven to be tricky if you're using an existing web-server package such as XAMPP or WAMP.

For my own personal configuration I use a combination of the following:

  • NGINX
    • A high-availability web server with integrated load-balancing features.
  • PHP 7.2
    • Server-sided scripting language interpreter.
  • MariaDB
    • Database server.

Tools

Local PHP development on Windows (10+) is best suited to an IDE like Visual Studio Code. It's free, open-source, and has great out-of-the-box support for PHP, with even better and community supported extensions for the language.

Download Visual Studio Code

Extensions

Next you are going to want to install some extensions for Visual Studio Code. I recommend installing these for PHP syntax highlighting, linting, and debugging.

Debugging PHP on Windows (10+) works best with the following extensions for both PHP, and Visual Studio Code.

felixfbecker.php-debug
felixfbecker.php-intellisense
ikappas.phpcs
junstyle.php-cs-fixer
linyang95.php-symbols

You can get the extensions from the following links:

PHP

XDebug

I strongly recommend using XDebug, as Visual Studio Code can support breakpoint debugging through the usage of an extension that is available here.

Create a basic script with the following code.

<?php

echo phpinfo();

Navigate to the page, and make sure that the script executes.

Copy the entire page contents and then paste it into this useful tool. The page will then inform you of which version of XDebug you should use for your installation of PHP.

Next, place the downloaded DLL into the following directory.

Change your php.ini configuration file to reflect the following.

extension=curl
extension=fileinfo
zend_extension=php_xdebug-2.6.1-7.2-vc15-x86_64.dll
extension=gd2
extension=gettext
;extension=gmp
extension=intl
;extension=imap
;extension=interbase
;extension=ldap
extension=mbstring
extension=exif; Must be after mbstring as it depends on it
extension=mysqli
;extension=oci8_12c  ; Use with Oracle Database 12c Instant Client
extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
extension=pdo_odbc
;extension=pdo_pgsql
extension=pdo_sqlite
;extension=pgsql
;extension=shmop

You will likely also want to change the following parameter.

extension_dir = "./ext"

Visual Studio Code

You will likely want to make use of this configuration for your launch.json file. The following configuration gives you three debugging profiles.

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "PHP: XDebug (Listen)",
            "type": "php",
            "request": "launch",
            "port": 9001
        },
        {
            "name": "PHP: XDebug (File)",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9001
        }
    ]
}

Environment

After installing your integrated development environment (IDE), and additional extensions to go with them, you will want to configure them using the following environment and configuration variables.

Variables

Several of the recommended PHP extensions make use of the PHP command-line for linting, code inspection, and syntax highlighting. Their default behaviour is to make use of the PHP client that is available from the path environment variable.

XDebug

You will want to place the following parameters at the end of your *.ini based configuration file for PHP.

These parameters ensure that you have XDEBUG configured, enabled, and listening for incoming debugger connections from Visual Studio Code. 

[XDebug]
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9001
xdebug.remote_handler=dbgp
xdebug.remote_autostart=1


Comments