WSL 下优雅地 Coding

时间:2022-07-28
本文章向大家介绍WSL 下优雅地 Coding,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

# WSL 简介

WSL:Windows Subsystem for Linux,Windows 系统下的 Linux 子系统

本文所用 WSL 版本为 1.0

# WSL 安装

打开 控制面板 => 卸载程序 => 启用或关闭 Windows 功能

勾选 适用于 Linux 的 Windows 子系统,确定后等待安装并重启电脑

重启后,打开 Microsoft Store ,找到 Ubuntu 18.04 LTS 并安装

# Ubuntu 初始化

安装成功后打开

等待几十秒后,即可设置用户名和密码,建议设置 root 密码

$ sudo passwd root

1

sudo 每次都需要输入密码,过于麻烦,去掉(任性.jpg)

$ sudo visudo

1

修改如下内容,Ctrl+o 后回车保存,Ctrl+x 退出编辑即可

·
·
·
# %admin ALL=(ALL) ALL 这里注释掉,新增下面一条
%admin ALL=(ALL) NOPASSWD:ALL
·
·
·
# %sudo ALL=(ALL:ALL) ALL 这里注释掉,新增下面一条
%sudo ALL=(ALL:ALL) NOPASSWD:ALL
·
·
·

1 2 3 4 5 6 7 8 9 10 11 12 13

# 默认使用 root 账号登入

ubuntu1804.exe config --default-user root

1

# 使用密钥登入

应该存在的 3 个文件

  • authorized_keys
  • id_rsa.pub
  • id_rsa

编辑 sshd_config 配置

$ sudo vim /etc/ssh/sshd_config

1

内容如下:

·
·
·
# SSH 端口
Port 8022
·
PubkeyAuthentication yes
·
AuthorizedKeysFile .ssh/authorized_keys
·
# 禁止密码登入
PasswordAuthentication no
·
ChallengeResponseAuthentication no
·
·
·

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

# 更换 apt 源和 pip 源(阿里云)

# apt 源

备份 apt 原文件

$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

1

查看系统代号

$ lsb_release -c
Codename:       bionic

1 2

编辑源列表文件

$ sudo vim /etc/apt/sources.list

1

输入 dG 清空内容,复制保存为以下内容

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

1 2 3 4 5 6 7 8 9 10

更新软件列表

$ sudo apt-get update

1

更新软件包

$ sudo apt-get upgrade

1

# pip 源

创建 pip 配置文件

$ mkdir ~/.pip && vim ~/.pip/pip.conf

1

复制保存以下内容

[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/

1 2 3

# 安装宝塔面板

懒人必备

$ wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh

1

安装成功后,方便管理软件(Nginx / PHP / Tomcat / Docker / MongoDB / Memcached / Redis / GitLab / ...)

# 创建项目

新建网站

修改配置(伪静态 / 运行目录 / 等等)

修改 hosts

访问结果

# 简单的 Swoole 例子

在宝塔中快速安装 swoole

C:workspacework_man 目录下新建文件 server.php

使用 putty 连接 ubuntu,打开两个连接

连接 ①,执行 server.php,创建一个 TCP 服务器

$ cd /mnt/c/workspace/work_man/
$ php server.php

1 2

连接 ②,发送消息

$ telnet 127.0.0.1 9501

1

# 开机自启

# ubuntu 中新增脚本

$ sudo vim /etc/init.wsl

1

内容如下:

#! /bin/sh
service ssh $1
service mysqld $1
service bt $1

1 2 3 4

添加执行权限

$ sudo chmod +x /etc/init.wsl

1

# Windows 中新增脚本

打开 开机自启目录%AppData%MicrosoftWindowsStart MenuProgramsStartup

快捷打开:Win+R,输入 shell:startup 回车

新增脚本文件 wsl.vbs,内容如下:

Set ws = CreateObject("Wscript.Shell")
ws.run "ubuntu1804 run sudo /etc/init.wsl start", vbhide

1 2

允许该脚本开机自启,杀毒软件中添加信任

# PHPStorm 配置

# Terminal 配置

FileSettingsToolsTerminalShell path

C:WindowsSystem32wsl.exe
// 或者 Ubuntu 18.04

1 2

# Node 配置

FileSettingsLanguages & FrameworksTypescript

Ubuntu 18.04 /usr/local/bin/node

1

# Yarn 的安装

配置下载仓库

$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

1 2

安装(更新至最新版本)

$ sudo apt-get update && sudo apt-get install yarn

1

更换镜像源

$ yarn config set registry "https://registry.npm.taobao.org"

1

# 已踩的坑

# 死活连不上的 SSH

重装 openssh-server

$ sudo apt-get purge openssh-server
$ sudo apt-get install openssh-server

1 2

# Unix Socket

WSL 中的 Unix Socket 似乎有点问题,修改 Nginx 配置

·
·
·
fastcgi_buffering off;
·
·
·

1 2 3 4 5 6 7

# 莫名其妙的 502

原因未知,求大佬告知,临时解决方法:宝塔中安装 PHP 守护

还是不行的话,只能重启 php-fpm 服务了

# 权限争夺

  • 不要在编辑器中 删除 文件或文件夹!!!两个系统会出现争权行为,导致文件或文件夹出现不可访问问题(实际已经删除,只是电脑还会显示,重启电脑可解决)
  • 正确删除操作:在 我的电脑 里删除