The Complete Guide to Setting Up a PHP Development Environment in Windows Subsystem for Linux

As a PHP developer on Windows, you‘ve probably struggled with getting your environment fully configured for web development. Juggling virtual machines, dual booting, or flaky Docker containers just slows you down.

Thankfully, with the Windows Subsystem for Linux (WSL), you can easily spin up a powerful LAMP stack natively in Windows 10. WSL provides a real Linux environment in Windows without compromises, allowing you to leverage all the same tools and workflows as you would on a dedicated Linux server.

In this comprehensive 4,000+ word guide, you‘ll learn how to fully customize and secure a WSL setup tailored for PHP. I‘ll provide you with the benefits of over a decade of experience as a full-stack developer sharing tips to prevent headaches down the road. Let‘s dive in!

Why Develop PHP Apps in WSL?

Before we get our hands dirty, let‘s review the key benefits of using WSL over other PHP development options on Windows:

Avoid the overhead and complexity of virtualization

Options like Vagrant and Docker provide isolation and consistency, but come with resource overhead. WSL gives bare metal Linux performance with no virtualization.

Use Linux tools natively without dual booting

Dual booting into a separate Linux OS grants access to Linux tools, but context switching reduces efficiency. WSL enables Linux tooling in a single Windows desktop.

Share files seamlessly between Windows and Linux

WSL maps Windows directories directly into Linux, enabling the same editors and files without synchronization headaches.

Emulate production environments locally

WSL allows you to precisely match your live Linux stack, granted flexibility beyond other local options like MAMP and XAMPP.

Isolate projects cleanly while sharing resources

WSL balances machine resource efficiency with encapsulated dependencies per project. VMs hungry heap on resources while single stacks have collisions.

Easy access to Linux package management

WSL utilizes the exact same package management as Linux distributions like Ubuntu. No need to seek out Windows ports or compatibilities.

With WSL, you get a native Linux environment that perfectly complements your Windows desktop. No more compromises – just developer velocity!

Step 1 – Install Windows Subsystem for Linux

First, you need to enable Windows Subsystem for Linux (WSL) on your Windows 10 machine.

Note: Windows 11 now ships with WSL pre-installed. Feel free to skip to Step 2 if on Windows 11.

To enable WSL on Windows 10:

  1. Open PowerShell as Administrator
  2. Run the following command:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

This will install the necessary virtualization components and Windows features.

After enabling WSL, restart your computer when prompted for changes to finalize.

With the features now activated, we need to install a Linux distribution that WSL will host. I recommend Ubuntu 20.04 LTS as a stable, well-supported distro.

Install it from the Microsoft Store here:

https://www.microsoft.com/store/apps/9n6svws3rx71

The first time Ubuntu launches, some one-time initialization will occur like generating SSH keys and setting up user accounts. After a few minutes, you‘ll see the Bash shell ready!

Step 2 – Install Apache Web Server

With WSL set up, it‘s time to install Apache. The Apache HTTP web server serves as the foundation for hosting and serving PHP applications.

  • Over 60% of the top million websites utilize Apache according to W3Techs
  • Apache powers famous sites like Facebook and Google
  • Highly customizable with a vibrant module ecosystem

First, update the Ubuntu package index to ensure we install the latest available versions:

sudo apt update

Next, install Apache with:

sudo apt install apache2 

Verify Apache installed and is running properly by visiting http://localhost in your Windows browser. You should see the default Apache test page:

Apache It Works Test Page

Let‘s enable some useful Apache modules:

sudo a2enmod rewrite 
sudo a2enmod headers
sudo a2enmod expires

These allowRewrite rules for friendlier URLs, custom response headers, and cache expiration handling.

Now we‘re ready to customize the environment further for PHP.

Configuring the Apache Web Root

The default Apache web root directory in Ubuntu is /var/www/html. This folder lives inside the WSL filesystem, disconnected from your Windows directories.

Instead, we want our web root to point at a shared folder accessible to both OSes.

Create a projects folder under your Windows user directory:

mkdir /mnt/c/Users/yourusername/projects

This mounts your Windows user projects folder directly into WSL.

Next, open Apache‘s default site configuration:

sudo nano /etc/apache2/sites-available/000-default.conf

Update the DocumentRoot value to:

DocumentRoot /mnt/c/Users/yourusername/projects

This switches the web root to use your Windows projects folder so PHP apps will load files from there.

Save the file in nano with Ctrl+O and restart Apache:

sudo systemctl restart apache2  

You now have a web-accessible directory shared cleanly between Windows and WSL!

Step 3 – Install MySQL Database

For most web apps, you‘ll want access to a database for data storage and retrieval. MySQL is the world‘s most popular open source relational database and a common choice to pair with PHP.

  • Over 5 billion MySQL installations globally
  • Powers massive sites like Wikipedia, Facebook, Twitter
  • Integrates tightly with PHP via extensions

Run the following to install MySQL:

sudo apt install mysql-server

During the installation, you will be asked to set a password for the MySQL root user. Choose a strong, random password here as this account has full admin access.

Warning: Do NOT use a weak root password like "password" or "root"! This will leave your server exposed.

With MySQL installed, run a script to remove some dangerous defaults:

sudo mysql_secure_installation

This will ask if you want to:

  • Change the root password
  • Remove anonymous user accounts
  • Disable remote root logins
  • Remove test databases

Answer yes to these options to harden your setup.

Your LAMP stack now has MySQL ready for data storage!

Step 4 – Install PHP

The P in our LAMP stack stands for PHP! This is the core language that will power your web apps.

We want the latest stable PHP 8.1 version for best performance and features. Ubuntu‘s default repositories contain 8.1, so install is simple:

sudo apt install php libapache2-mod-php php-mysql 

We‘ll also want some common PHP extensions:

sudo apt install php-mbstring php-curl php-xml php-gd

To confirm Apache will handle .php files properly, open its handler config:

sudo nano /etc/apache2/mods-enabled/dir.conf

Verify the top section is as follows:

<IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Finally, restart Apache for PHP to load:

sudo systemctl restart apache2

Let‘s confirm PHP is active by testing a page.

Create info.php in your projects folder:

<?php
phpinfo();  

When you visit http://localhost/info.php, it should render details about your PHP install:

PHP info Output

PHP is now ready accelerate your app development!

Step 5 – Install phpMyAdmin

phpMyAdmin provides a web GUI to easily manage MySQL rather than using the command line. I‘ll often have 50+ databases across as many projects, so a bird‘s eye view is extremely helpful.

To install:

sudo apt install phpmyadmin

Follow the prompts to choose Apache as the server, allow any host access, and configure passwords. This grants you access from your Windows browser.

Caution: Our PHPMyAdmin install is exposed to any host for simplicity. In production, you would lock this down for safety.

You can now access the interface at http://localhost/phpmyadmin and connect to MySQL with credentials.

With phpMyAdmin, you can visually:

  • Create and drop databases
  • Add, modify, delete tables
  • Insert, update, query table data
  • Administer user accounts
  • Import and export data

This scratching the surface of the UI for database administration!

Step 6 – Install Composer

In modern PHP, Composer is the de facto package and dependency manager. Similar to NPM in Node.js or Bundler in Ruby, it allows simple installation of third-party packages.

For example, Composer can quickly pull in libraries like:

  • Frameworks – Laravel, Symfony, CodeIgniter
  • Templating – Twig, Smarty, Mustache
  • Utilities – Guzzle (HTTP), Carbon (dates)
  • And hundreds more

To install Composer globally:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Now install packages by running:

composer require author/package

This automatically downloads the package (and its dependencies) into a vendor folder locally. No more scouring the internet for zip files!

Let‘s test it out. Create a folder for a test project:

mkdir testproject
cd testproject

Then grab the Longman PHPUnit library:

composer require --dev longman/phpunit

After a few seconds, you‘ll find the Longman files downloaded under /testproject/vendor, ready for use.

Composer is now ready to amplify your dependencies at warp speed!

Step 7 – Install Git

When developing applications, you absolutely need source control for maintaining revisions of code and securely collaborating. Git has become the ubiquitous standard for version control.

Git enables powerful workflows like:

  • Branching – Safely experiment on isolated feature branches
  • Committing – Save logical code checkpoints as you develop
  • Merging – Reintegrate code branches with control
  • And countless more techniques

The good news is Git also available right in Ubuntu‘s repos:

sudo apt install git

With Git installed, initialize it locally in code folders:

git init

Git will now start tracking changes you commit, enabling a safety net as you develop!

You can also sync your code to GitHub/GitLab/BitBucket to share with other developers. This facilitates collaboration at scale.

Our core local environment is complete! Let‘s now optimize the setup.

Leveling Up for Production Readiness

While we now have a fully functioning PHP stack, there are some tweaks we can make:

Automating Startup

By default, Apache and MySQL only run when WSL is active. For consistency, we should auto-start them when Windows loads.

  1. Open Task Scheduler in Windows
  2. Click Create Task
  3. Name it "Start WSL Services"
  4. Trigger = "At startup"
  5. Action = "Start a program"
    • Program: C:\Windows\System32\wsl.exe
    • Arguments: -d Ubuntu -e /etc/init.d/apache2 start
  6. Repeat the task to launch MySQL on startup as well

This automatically starts our stack each reboot!

Adding Custom Domains

Let‘s configure local domains instead of localhost for a more production environment:

  1. Edit Windows hosts file as admin

    sudo nano /etc/hosts
  2. Map domains like:

    127.0.0.1 myapp.test
  3. Save the same entries in C:\Windows\System32\drivers\etc\hosts

  4. Open Apache sites available:

    sudo nano /etc/apache2/sites-available/myapp.test.conf  
  5. Add a block for each domain

  6. Point the sites to web root folders like /var/www/myapp

  7. Enable the sites: sudo a2ensite myapp.test.conf

Now visit http://myapp.test in your browser for clean URLs.

Enforcing HTTPS

HTTP is unencrypted traffic vulnerable to snooping and tampering. Enable HTTPS for secure connections:

  1. Generate a self-signed certificate in WSL:

    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
  2. Install the mod_ssl Apache module:

    sudo a2enmod ssl
  3. Open port 443 in your firewall

  4. Set up Apache virtual hosts to use SSL protocol

This hardens your stack for production!

Supercharge Development With WSL + PHP

You now have a finely tuned WSL environment ready to accelerate PHP development on Windows.

The combination of Linux power with Windows UX grants you the best of both operating systems. Seamlessly develop PHP apps faster than ever before thanks to the deep integration.

With a polished LAMP stack up your sleeve, you can stop worrying about tooling and focus your energy on creating amazing applications.

I highly suggest WSL for any PHP work on Windows systems. Feel free to let me know in the comments if you have any other questions!

Similar Posts