[WSL] PHP Development Environment by “Windows Subsystem for Linux” (Part 2)

This article address some common problems/caveats when using WSL as your daily development environment.

Access Windows File under WSL

WSL use a plugin called DrvFs to mount Windows drive

Auto-mount all Windows drive to,/mnt/ for example /mnt/c

For more detailed explanation: https://blogs.msdn.microsoft.com/wsl/2016/06/15/wsl-file-system-support/

Access Linux File under Windows

DO NOT, under ANY circumstances, create and/or modify Linux files using Windows apps, tools, scripts, consoles, etc.
Also note: Opening files using some Windows tools may read-lock the opened files and/or folders, preventing updates to file contents and/or metadata, essentially resulting in corrupted files/folders.

Recommendation: Start an SSH server under WSL, and access it via FileZilla / SSHFS

Share Windows file to WSL

The easiest and most convenience way is to share Windows file to WSL, for Example: Create a folder in C:\var\www\

The folder will be visible in WSL as /mnt/c/var/www/

Then ln -s /mnt/c/var/www /var/www

Fix Permission under DrvFs always being 0777

Since Build 17063, WSL includes an improvement that handles permissions under DrvFs (i.e. Windows drive) properly.
But did not enable it by default.

Create /etc/wsl.conf and add this content to enable metadata mount option by default.

options = "metadata"

Reference: https://blogs.msdn.microsoft.com/commandline/2018/01/12/chmod-chown-wsl-improvements/

Reference: https://blogs.msdn.microsoft.com/commandline/2018/02/07/automatically-configuring-wsl/

PHPStorm Bash Integration

Settings > Tools > Terminal
Shell Path: C:\Windows\System32\bash.exe

PHPStorm NPM Integration

PHPStorm 2018.2 and above support triggering Node.JS from WSL

Settings > Languages and Frameworks > Node.JS and NPM

Node Interpreter: Add WSL > Linux Distro: Ubuntu 18.04, Path: /usr/bin/node

Package Manager: /usr/share/npm

Add Git Prompt

tiger@DESKTOP-PC /var/www/project1 (master)

Add the following lines to ~/.bash_profile

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

Git Repository Line Ending Problem

If you checkout a Git repository in Windows, some files end in \r\n may cause problem (Especially .sh files)

If you’re on a Linux or Mac system that uses LF line endings, then you don’t want Git to automatically convert them when you check out files; however, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf to input:

Run this in WSL and checkout your repository using Git in WSL again.

git config --global core.autocrlf input

Git Repository Permission Problem

Another problem, if you checkout a repository in WSL, your IDE/git in Windows may always considered files as changed (Permission changed)

Run this in Windows side to make Git ignore permission change.

(First line remove default true for working copy, Second line set false for Windows side)

git config --unset core.filemode
git config --global core.filemode false

Apache H00076: Failed to enable APR_TCP_DEFER_ACCEPT

Although it does not harm anything, add this two lines to /etc/apache2/apache2.conf

AcceptFilter https none
AcceptFilter http none

Blue Color too dark on WSL bash

The blue text on black background barely readable, there are options, and repositories for this setting.

My personal favorite theme makes WSL bash looks similar to native Ubuntu bash terminal.

SSHd – Could not load host key: /etc/ssh/ssh_host_rsa_key

sudo killall sshd
sudo ssh-keygen -A
sudo /etc/init.d/ssh start

Bash Disable Bell on Tab Key

Add the following line to ~/.inputrc

set bell-style none

Other FAQ


One Reply to “[WSL] PHP Development Environment by “Windows Subsystem for Linux” (Part 2)”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.