Windows10下WSL搭建LAMP的最佳方案及跨环境代码编辑问题咨询
Hey there! I’ve been right where you are—switching from Laragon on Windows to WSL for development felt a bit clunky at first, but once you nail the workflow, it’s way smoother. Let’s break down your two main pain points:
Editing /var/www Files with Windows’ Atom
The core here is bridging WSL’s file system with Windows apps like Atom. You’ve got two reliable options:
Option 1: Access WSL Files Directly via Windows File Explorer
WSL exposes its file system through a network path. Open File Explorer and paste this into the address bar:
\\wsl$\<YourDistroName>\var\www
Replace <YourDistroName> with your WSL distro’s name (e.g., Ubuntu, Debian). Once you’re in that folder:
- Right-click any file and select "Open with Atom"
- Drag the entire
wwwfolder into Atom’s window to load the whole project
Option 2: Sync WSL’s /var/www with Your Existing Windows Code Folder
If you want to keep using your old C:/laragon/www directory (no file moving required), create a symbolic link in WSL that points to your Windows folder. Open your WSL terminal and run:
sudo ln -s /mnt/c/laragon/www /var/www
Now WSL’s /var/www acts as a shortcut to your Windows www folder. Edit files in C:/laragon/www with Atom like you always did, and WSL’s LAMP stack will read them directly. Fix permissions to avoid access issues:
sudo chown -R $USER:$USER /var/www
Best Practices for Setting Up LAMP in WSL
Stick with official distro packages for stability—here’s a step-by-step for Ubuntu (the most popular WSL distro):
1. Update Your Package List
First, make sure your system is up to date:
sudo apt update && sudo apt upgrade -y
2. Install Apache
sudo apt install apache2 -y
Start the service and set it to run on boot:
sudo systemctl start apache2 sudo systemctl enable apache2
Test it by visiting http://localhost in your Windows browser—you’ll see Apache’s default welcome page.
3. Install MySQL
sudo apt install mysql-server -y
Run the security script to set a root password and lock down your installation:
sudo mysql_secure_installation
Answer "Y" to most prompts for a secure setup. Access MySQL later with:
sudo mysql -u root -p
4. Install PHP & Required Modules
sudo apt install php libapache2-mod-php php-mysql -y
Restart Apache to apply the PHP module:
sudo systemctl restart apache2
Test PHP by creating an info file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Visit http://localhost/info.php—you’ll see the PHP info page if everything works.
5. Bonus: Set Up Virtual Hosts (Optional but Recommended)
For multiple projects, virtual hosts let you use custom domains like myproject.local:
- Create a project folder (e.g.,
/var/www/myproject) - Create a config file at
/etc/apache2/sites-available/myproject.conf:<VirtualHost *:80> ServerName myproject.local ServerAlias www.myproject.local DocumentRoot /var/www/myproject/public ErrorLog ${APACHE_LOG_DIR}/myproject.error.log CustomLog ${APACHE_LOG_DIR}/myproject.access.log combined </VirtualHost> - Enable the site and rewrite module:
sudo a2ensite myproject.conf sudo a2enmod rewrite sudo systemctl restart apache2 - Add the domain to your Windows
hostsfile (C:\Windows\System32\drivers\etc\hosts):127.0.0.1 myproject.local
Pro Tips
- Use WSL 2 instead of WSL 1—it’s way faster for file operations.
- If Laragon is using port 80, stop its Apache service first or change WSL’s Apache port in
/etc/apache2/ports.conf. - Manage databases with Windows apps like HeidiSQL—connect using
localhostand your MySQL credentials.
内容的提问来源于stack exchange,提问作者Fil

