Setting Up a Local PHP Development Environment on Windows
Setting up a local development environment for PHP on a Windows machine, especially when you’re using web-server packages like XAMPP or WAMP, can be a bit challenging. In this guide, we’ll walk you through the process of creating a robust PHP development environment on Windows. We’ll cover the following topics:
-
Choosing the Right Components
To set up a local PHP development environment on Windows, it’s essential to select the right components. Here are the key components we recommend:
- NGINX: A high-availability web server with integrated load-balancing features.
- PHP 7.2: A server-sided scripting language interpreter.
- MariaDB: A database server.
- Tools: For Windows 10 and above, an integrated development environment (IDE) like Visual Studio Code is ideal. It’s free, open-source, and offers excellent support for PHP, along with a vibrant community providing useful extensions.
-
Installing Visual Studio Code
Before diving into PHP development, you should install Visual Studio Code, a powerful and versatile code editor. It’s not only free but also offers great out-of-the-box support for PHP, with a wide range of community-supported extensions for the language.
You can download Visual Studio Code here.
-
Configuring Visual Studio Code Extensions
To enhance your PHP development experience in Visual Studio Code, you’ll need to install some essential extensions for syntax highlighting, linting, and debugging. We recommend the following extensions:
- felixfbecker.php-debug: Enables breakpoint debugging with the XDebug PHP extension.
- felixfbecker.php-intellisense: Provides syntax and symbol parsing support for efficient code navigation.
- ikappas.phpcs: Helps with PHP code style checking.
- junstyle.php-cs-fixer: Provides PHP code formatting support.
- linyang95.php-symbols: Debugging symbols used by the PHP interpreter.
You can easily find and install these extensions directly from Visual Studio Code.
-
Configuring XDebug
XDebug is a crucial tool for debugging PHP code in Visual Studio Code. To configure it correctly, follow these steps:
- Create a basic PHP script with the following code:
- Access this script via your web server to ensure it executes correctly.
- Copy the entire page contents and paste it into this tool. The tool will recommend the appropriate version of XDebug for your PHP installation.
- Download the recommended XDebug DLL file and place it in the appropriate directory.
- Modify your php.ini configuration file to include the XDebug extension. Here’s an example of how to do it:
extension=curl
extension=fileinfo
zend_extension=php_xdebug-2.6.1-7.2-vc15-x86_64.dll
extension=gd2
extension=gettext
extension=intl
extension=mbstring
extension=exif
extension=mysqli
extension=openssl
extension=pdo_mysql
extension=pdo_odbc
extension=pdo_sqlite
-
Configuring Visual Studio Code for XDebug
To enable debugging in Visual Studio Code, you should configure your launch.json
file. The following configuration provides three debugging profiles:
{
"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
}
]
}
-
Setting Environment Variables
Some PHP extensions rely on the PHP command-line interface for tasks like linting and code inspection. These extensions typically use the PHP client available in the system’s PATH environment variable.
-
Configuring XDebug
For XDebug, ensure that the following parameters are included at the end of your PHP configuration file (*.ini
):
[XDebug]
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_port=9001
With these steps, you’ll have a well-configured
PHP development environment on your Windows machine, allowing you to code, debug, and test your PHP applications effectively. Whether you’re a seasoned developer or just getting started with PHP, having a robust local development environment is crucial for a productive workflow.