PHP Development Setup on Windows with WSL2
Daniel Opitz
01 Dec 2024
It looks like XAMPP is no longer actively maintained, which makes this a good time to rethink how we set up PHP development environments on Windows.
If you’ve been frustrated with the performance of Docker, you are not alone. I think many find it too slow for this kind of work.
A much better option is Windows Subsystem for Linux (WSL2) . It’s fast, optimized, and offers a clean way to run a modern LAMP stack.
Here is how to get started with WSL2 and set up a PHP development environment that’s efficient and easy to use.
Setting Up WSL2
First, install WSL2 by running the following in command:
wsl --install
Follow the prompts to create a username and password.
Once inside your WSL2 environment, update and upgrade the system:
sudo apt update
sudo apt upgrade
Install Apache and PHP
sudo apt install apache2
Add a repository for the latest PHP versions:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Automate PHP Installation with a Script
Let’s create a simple script to install PHP and configure the environment:
- Create a new file:
touch phpsetup.sh
vim phpsetup.sh
- Add this content to the file:
Press INSERT
to enter in edit mode.
#!/usr/bin/env bash
apt update
apt install vim unzip -y
apt install libapache2-mod-php8.4 php8.4 php8.4-cgi -y
apt install mysql-client libmysqlclient-dev -y
apt install php8.4-mysql php8.4-sqlite3 -y
apt install php8.4-mbstring php8.4-curl php8.4-intl php8.4-gd php8.4-zip php8.4-bz2 -y
apt install php8.4-dom php8.4-xml php8.4-soap php8.4-xdebug
a2enmod php8.4
a2enmod rewrite
a2enmod actions
sed -i '170,174 s/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf
service apache2 restart
cd ~
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
composer self-update
-
Save and exit (
ESC
, then:wq
). -
Make the script executable and run it:
sudo chmod +x phpsetup.sh
sudo ./phpsetup.sh
For development only: give full write access:
sudo chmod -R a+rwx /var/www/html/
Optional: Install net-tools:
apt install net-tools
Testing your Setup
Visit the WSL host in your browser: http://wsl.localhost/
You should see the Apache demo page.
Accessing Files on WSL
You can access the file system from Windows directly at the \\wsl.localhost
and \\wsl$
path.
\\wsl.localhost
Accessing Windows files on WSL
To mount a Windows drive into a WSL (Windows Subsystem for Linux) instance, you can use the built-in capabilities of WSL2, which automatically makes Windows drives accessible.
By default, WSL automatically mounts all Windows drives under /mnt/<DriveLetter>
.
Open your WSL terminal and navigate to the desired drive:
cd /mnt/c
ls
Mounting the Apache DocumentRoot to a Windows drive
WSL lets you serve files directly from your Windows drive, while executing PHP scripts within the WSL environment (with Apache).
Make sure the path /var/www/html
does not exists. In this case I rename the existing folder.
sudo mv /var/www/html /var/www/html_backup
The ln -s command creates a symbolic link:
sudo ln -s /mnt/c/xampp/htdocs /var/www/html
Change to Windows path accordingly.
Once the symbolic link is set up in WSL, you can access the Windows directory C:\xampp\htdocs
from WSL at /var/www/html
.
Visit the WSL host in your browser to check the result: http://wsl.localhost/
Some useful WSL commands
Start WSL
wsl
Shutdown all WSL virtual machines.
wsl --shutdown
List installed Linux distributions with status:
wsl --list --verbose