Setting up a LEMP stack on Windows using Windows Subsystem for Linux (WSL) is a powerful way to develop and test web applications locally. The LEMP stack, which includes Linux, Nginx, MySQL (or MariaDB), and PHP, provides a robust environment similar to many production servers. This guide walks you through the process step-by-step, ensuring you can get your web server up and running efficiently on your Windows machine. Before starting, ensure you have Windows 10 or 11 with WSL installed and a compatible Linux distribution like Ubuntu.
What You’ll Need
- Windows 10 or 11 with WSL enabled
- WSL 2 installed and set as default
- A Linux distribution installed via Microsoft Store (preferably Ubuntu)
- Basic familiarity with command-line operations
Step-by-Step
- Enable WSL and Install a Linux Distribution
If you haven’t already, enable WSL by opening PowerShell as Administrator and running:dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Then, restart your PC. Next, install WSL 2 as default:
wsl --set-default-version 2
Finally, install Ubuntu from the Microsoft Store and launch it to complete initial setup.
- Update Your Linux Packages
Once inside your Linux terminal, update package lists:sudo apt update && sudo apt upgrade -y
- Install Nginx
To install Nginx, run:sudo apt install nginx -y
After installation, start Nginx:
sudo service nginx start
Verify by visiting http://localhost in your browser. You should see the default Nginx welcome page.
- Install MySQL or MariaDB
For MySQL:sudo apt install mysql-server -y
Or for MariaDB, which is a drop-in replacement:
sudo apt install mariadb-server -y
Secure your database installation:
sudo mysql_secure_installation
- Install PHP and Necessary Extensions
To install PHP along with common modules:sudo apt install php php-fpm php-mysql php-cli php-curl php-gd php-xml -y
Configure PHP-FPM to work with Nginx (usually it’s enabled by default). Test PHP:
echo '' | sudo tee /var/www/html/info.php
Visit http://localhost/info.php to verify PHP is working.
- Configure Nginx to Use PHP
Edit the default server block:sudo nano /etc/nginx/sites-available/default
Ensure the server is configured to pass PHP files to PHP-FPM. For example:
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php-fpm.sock; }Save changes and reload Nginx:
sudo systemctl reload nginx
- Test Your LEMP Stack
Open your browser and navigate to http://localhost. You should see the Nginx default page. Visit http://localhost/info.php to confirm PHP is functioning properly and check your database connection by creating test scripts.
Common Mistakes
- Not enabling WSL 2 properly — ensure you have installed WSL 2 and set it as default before installing your Linux distribution.
- Forgetting to start services — always start your web server and database services after installation.
- Misconfiguring Nginx or PHP-FPM — double-check configuration files for typos or incorrect paths.
- Not updating packages — always run update commands after fresh installation to get the latest versions.
- Using the wrong IP address — on WSL, localhost should work, but if not, check WSL IP with `hostname -I` and adjust accordingly.
Tips
- Use `nano` or `vim` to edit configuration files for better control.
- Set up a firewall rule if you want external access to your server — but be cautious with security.
- Consider installing PHPMyAdmin for easier database management, but only do so if security is properly configured.
- Regularly update your packages to keep your environment secure and stable.
- Bookmark your configuration files for quick access and future modifications.
Troubleshooting
If your LEMP stack is not working as expected, follow these troubleshooting steps:
- Services not starting or not responding: Ensure you have started Nginx and MySQL services with `sudo service nginx start` and `sudo service mysql start`. Use `sudo systemctl status nginx` and `sudo systemctl status mysql` to check their status.
- Cannot access localhost in browser: Verify WSL is running correctly. Check if your Windows firewall is blocking port 80. You can temporarily disable the firewall for testing.
- PHP info page not loading: Confirm PHP-FPM is running (`sudo systemctl status php-fpm`) and that Nginx is configured to pass PHP files to PHP-FPM.
- Database connection errors: Check your database user privileges and ensure your scripts are pointing to the correct server and credentials.
- Configuration errors in Nginx or PHP: Validate configuration syntax with `nginx -t` and `php-fpm -t`. Correct any errors reported.
When in doubt, consult logs located in `/var/log/nginx/`, `/var/log/mysql/`, and `/var/log/php/` for detailed error messages.
Frequently Asked Questions
1. Can I run the LEMP stack on Windows without WSL?
No, Windows natively does not support Linux-based stacks. WSL provides a Linux environment on Windows, making it possible.
2. How do I access my website from other devices on my network?
You need to configure your Windows firewall to allow incoming connections and use your Windows machine’s IP address instead of localhost.
3. Is it safe to run a LEMP stack on my Windows machine?
If properly configured and secured, it is safe for development. Avoid exposing it to the internet without proper security measures.
4. How do I upgrade PHP or Nginx later?
Use `apt` to update packages: `sudo apt update && sudo apt upgrade nginx php*`. Remember to restart services after upgrades.
5. Can I install other PHP modules or tools?
Yes, use `apt` to install additional modules, e.g., `sudo apt install php-curl` or `php-mbstring` as needed for your projects.
6. How do I remove the LEMP stack if I no longer need it?
Uninstall packages with `sudo apt remove nginx mysql-server php*` and delete configuration files if necessary.
By following this guide, you can set up a functional LEMP stack on your Windows machine using WSL, enabling local development and testing in a Linux-like environment. Remember to keep your environment secure and regularly update your components for the best experience.
Related
- How to Hide Virus & Threat Protection Section in Windows 11 Security
- How to Hide the Toolbar on a MacBook Air Easily
- How to Hide the Time and Date on Your Windows 11 Taskbar