Recreating PHP Development Environment with Windows Subsystem for Linux (WSL)


Working with PHP on Windows has its challenges, especially when it comes to instant application testing and line-by-line debugging. In the past, setting up a PHP development environment on Windows involved complex configurations using tools like XAMPP and Eclipse. However, with the advent of Windows Subsystem for Linux (WSL) in Windows 10, it’s now possible to streamline the process and run Apache, PHP, and MySQL with greater ease. In this guide, we’ll explore how to set up a PHP development environment on WSL for efficient PHP scripting and debugging.

Setting Up WSL for PHP Development

Before diving into PHP development on WSL, you need to install and configure WSL itself. Follow these steps:

1. Install Ubuntu via Windows Store
  • To get started, download a Linux distribution from the Windows Store. We recommend choosing Ubuntu for this purpose.
  • Enable WSL in the Windows features and restart your computer.
2. Launch Ubuntu and Set Up Credentials
  • After the restart, launch Ubuntu from the Start menu.
  • During the initial setup, create a username and password for your Ubuntu environment.

Updating and Installing LAMP Stack

Once you have Ubuntu up and running within WSL, proceed to set up the LAMP (Linux, Apache, MySQL, PHP) stack for PHP development:

3. Update Ubuntu Packages
  • Open the terminal within Ubuntu and update the package list by running the following commands:
    sudo apt-get update
    sudo apt-get upgrade
4. Install LAMP Stack
  • Install the LAMP stack components with a single command:
    sudo apt-get install lamp-server^

    (Note: The ^ symbol at the end is intentional.)

5. Verify Apache Configuration
  • To ensure that everything is functioning correctly, create a PHP info file by running the following command:
    sudo echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
  • Restart the Apache web server:
    sudo /etc/init.d/apache2 restart
  • Open a web browser on your Windows side and navigate to http://localhost/phpinfo.php to confirm that Apache and PHP are operational.

Configuring PHP Development Environment

With your LAMP stack in place, it’s time to set up your PHP development environment for efficient scripting and debugging.

6. Enabling Windows File Access
  • To edit PHP files from Windows, create a symbolic link within the Apache home directory. Suppose your PHP website is located at c:\websites\mysite. Run the following command to create the link:
    ln -s /mnt/c/websites/mysite /var/www/html/mysite
  • Now, you can access your site at http://localhost/mysite/, with PHP fully functional.
7. Enabling PHP Error Reporting
  • Ensure that PHP displays error messages by replacing the default php.ini with the development version. On Ubuntu (PHP version 7.0), this can be done with these commands:
    sudo cp /usr/lib/php/7.0/php.ini-development /etc/php/7.0/apache2/php.ini
    sudo /etc/init.d/apache2 restart
  • You should now see PHP error messages, aiding in debugging.
8. Starting Apache
  • Keep in mind that Apache in WSL only runs during the session. To start Apache before working on your PHP projects, open Ubuntu and run:
    sudo apache2ctl start

Line-by-Line Debugging with Visual Studio Code

For comprehensive debugging capabilities, you can utilize Visual Studio Code with the PHP Debug extension. Here’s how to set it up:

9. Install PHP Xdebug
  • Install the Xdebug extension for PHP on the Ubuntu side:
    sudo apt-get install php-xdebug
  • Restart Apache for the changes to take effect:
    sudo apache2ctl restart
10. Configure Visual Studio Code

In Visual Studio Code, install the PHP Debug extension. – Within Ubuntu, create a path mapping in your PHP project settings to bridge the gap between Linux and Windows file paths. Access the launch.json file in your project and add a mapping like this: json "pathMappings": { "/mnt/c/websites/mysite": "${workspaceFolder}" } – Set breakpoints in your PHP code within Visual Studio Code, start debugging, and open your PHP page in the browser.

Final Thoughts

Using Windows Subsystem for Linux (WSL) to set up your PHP development environment offers several advantages, including lightweight convenience. However, it’s important to note that WSL is not as isolated as a traditional virtual machine (VM). Therefore, you might encounter unique issues related to WSL. Additionally, some users have reported occasional crashes in other Windows applications when using WSL.

Nonetheless, WSL is a non-intrusive solution that allows you to develop and debug PHP applications seamlessly on Windows. It’s worth exploring and evaluating for your PHP development needs.

© 2013 - 2024 Foreignerds. All Rights Reserved