Windows on Linux(WSL)上的Web开发和高级技术

I've posted several times on the Windows Subsystem for Linux that allows you to run Linux on Windows 10 without a VM. Check out my YouTube on Editing code and files on Windows Subsystem for Linux on Windows 10. There's just one rule. You can mess with Windows files from Linux but you can't mess with Linux files from Windows. Otherwise, go crazy and enjoy. Here's some of my previous posts you should check out:

我已经在Linux的Windows子系统上发布了好几次,它允许您在没有VM的Windows 10上运行Linux。 在Windows 10Linux子系统上,查看我的YouTube编辑Windows子系统上的代码和文件。 只有一条规则。 您可以使用Linux中的Windows文件,但不能使用Windows中Linux文件。 否则,发疯享受。 这是我以前的一些帖子,您应该查看一下:

WSL is pretty fantastic although its disk access is slower than native Linux, I find myself using it every day. If you want to setup Linux on your Windows 10 machine, just turn it on, then head over to the Windows Store and search for "Linux."

尽管WSL的磁盘访问速度比本机Linux慢,但它却非常棒,我发现自己每天都在使用它。 如果要在Windows 10计算机上设置Linux,只需将其打开,然后转到Windows应用商店并搜索“ Linux”。

You can turn on Linux on Windows 10 by typing "Windows Features" and checking "Windows Subsystem for Linux." Then get a Linux from the Windows Store.

您可以通过键入“ Windows功能”并选中“ Linux的Windows子系统”在Windows 10上打开Linux。 然后从Windows应用商店获得Linux。

If you prefer to use PowerShell and do it in one line, just do this from an Admin PowerShell prompt:

如果您更喜欢使用PowerShell并一行执行,则只需在Admin PowerShell提示符下执行即可:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Then go get any one (or more!) of these from the Store:

然后从商店中获取其中任何一个(或多个!):

When you're in a Windows shell like PowerShell or CMD you might want to run Linux and/or jump comfortably between shells. You can do that in a few ways. The best and recommended way is running "wsl.exe" as that will start up your default distro. You can also just type the name of the distro. So I can type "ubuntu" and get in there directly.

当您使用Windows Shell(如PowerShell或CMD)时,您可能希望运行Linux和/或在各个Shell之间轻松地跳转。 您可以通过几种方式来做到这一点。 最好的推荐方法是运行“ wsl.exe”,因为这将启动您的默认发行版。 您也可以只键入发行版的名称。 所以我可以输入“ ubuntu”并直接进入那里。

You can type "bash" but that's not recommended if you've changed shells. If you've set up zsh or fish and type bash, it's gonna still try to run bash.

您可以键入“ bash”,但是如果您已经更改了外壳,则不建议这样做。 如果您已经设置了zsh或fish并键入bash,它将仍然尝试运行bash。

Here I've typed wslconfig and you can see I've got both Ubuntu and Debian installed, with Ubuntu as the default when I type "wsl."

在这里,我输入了wslconfig,您可以看到我已经安装了Ubuntu和Debian,当我输入“ wsl”时,Ubuntu是默认的。

C:\Users\scott>wslconfig /list
Windows Subsystem for Linux Distributions:
Ubuntu-18.04 (Default)
Debian

Now that I know how to run wsl from anywhere I can even pipe stuff in and out it Linux from outside. For example here I am in cmd.exe but I'm calling commands in Linux, that come out, then back in, etc. You can mix and match however you'd like!

现在,我知道了如何从任何地方运行wsl,甚至可以从外部通过管道将内容输入和输出Linux。 例如,这里我在cmd.exe中,但是我在Linux中调用命令,这些命令先出来,然后再返回,等等。您可以随意混合和匹配!

C:\dev>type hello.sh
echo Hello
C:\dev>wsl cat /mnt/c/dev/hello.sh | wsl fromdos | wsl /bin/sh
Hello

This means even when I'm in CMD or PowerShell I can use Linux commands that are convenient or familiar to me. For example, here I'm piping a Windows Update log file into a the Linux command sha1sum command. Note the use of - to accept standard input - even though that input is from Windows!

这意味着,即使在CMD或PowerShell中,我也可以使用方便或熟悉Linux命令。 例如,这里我将Windows Update日志文件传送到Linux命令sha1sum命令中。 请注意使用-接受标准输入-即使该输入来自Windows!

C:\Users\scott\Desktop>type WindowsUpdate.log | wsl sha1sum -
3b48adce8f6c9cb816e8845d824dacc0440ca1b8 -

Sweet. There's a number of nice advanced techniques if you want to make your WSL installations smarter AND automatically configured.  You can make a file in /etc/wsl.conf to affect your DNS, metadata and driving mounting.

甜。 如果您想使WSL安装更加智能并自动配置,可以使用许多不错的高级技术。 您可以在/etc/wsl.conf中创建文件以影响您的DNS,元数据和驱动安装。

When you are in a WSL shell, your Windows drive (your main drive) is at /mnt/c. So here is my Windows desktop as viewed from WSL:

在WSL Shell中时,您的Windows驱动器(您的主驱动器)位于/ mnt / c。 因此,这是从WSL中查看的Windows桌面:

screenfetch in WSL

I most of my dev work in /mnt/d/github for example. That way I can use VS Code from Windows but run Node/Ruby/Go/Whatever from WSL.

例如,我的大部分开发工作都在/ mnt / d / github中。 这样,我可以从Windows使用VS Code,但可以从WSL运行Node / Ruby / Go / Whatever。

I keep my files on my Windows drive, edit them in VS Code, but run things in WSL. Again, never use Windows utilities to reach into and/or edit files on the WSL/Linux subsystem. Also, always been conscious of your CR/LF situation, and be real conscious if you're going to run git in both Windows and WSL.

我将文件保留在Windows驱动器上,在VS Code中进行编辑,但在WSL中运行。 同样,切勿使用Windows实用程序来访问和/或编辑WSL / Linux子系统上的文件。 另外,请始终注意CR / LF的情况,如果要在Windows和WSL中都运行git ,请始终保持清醒的意识

Here's VS Code at the top, WSL/Ubuntu running Node at the bottom, and the local node app running in Edge on Windows on the lower right. We are sharing file systems and network port space:

这是顶部的VS代码,底部是运行节点的WSL / Ubuntu,右下角是在Windows的Edge中运行的本地节点应用程序。 我们正在共享文件系统和网络端口空间:

Cross platform Web Dev

You can even share environment variables between WSL and Windows with a special environment variable called WSLENV. This is pretty advanced but super powerful. Read this carefully. You make a environment variable that is a list of names of other variables that you want translated between environments.

您甚至可以使用称为WSLENV的特殊环境变量在WSL和Windows之间共享环境变量。 这是相当先进的,但功能强大。 请仔细阅读。 您创建一个环境变量,该变量是要在环境之间转换的其他变量的名称的列表。

That means you can do something like this. I'm in WSL and I have an environment variable that points to a location on the filesystem. I need it to be correct in both worlds.

这意味着您可以执行以下操作。 我在WSL中,并且有一个环境变量指向文件系统上的某个位置。 我需要在两个方面都是正确的。

scott@IRONHEART:/mnt/d$ export MYLINUXPATH=/mnt/d/github/expresstest
scott@IRONHEART:/mnt/d$ export WSLENV=MYLINUXPATH/p
scott@IRONHEART:/mnt/d$ cmd.exe
D:\>echo %MYLINUXPATH%
D:\github\expresstest

Read that carefully. It's awesome and it's very configurable.

仔细阅读。 太棒了,它是可配置的

There's lots of users of WSL and many have assembled great lists of resources like Awesome-WSL by Hayden.

WSL的用户很多,并且聚集了很多资源,例如Hayden的Awesome-WSL

It's also worth pointing out that WSL is just now one console you can choose from. There's PowerShell, CMD.exe, and a half dozen Linuxes. You can even make your own custom Linux Distro for your company if you like. And there's a whole world of 3rd party Consoles that sit on top of/replace conhost.exe so you can have consoles with tabs, cool fonts, ones based on web tech, whatever! You can even choose WSL/bash as your default shell in Visual Studio Code if you'd like with Ctrl+~.

还值得指出的是,WSL只是您可以选择的一个控制台。 有PowerShell,CMD.exe和六个Linux。 如果愿意,您甚至可以为您的公司制作自己的自定义Linux Distro。 在/替换conhost.exe之上一个第三方控制台的世界,因此您可以拥有带有选项卡,炫酷字体,基于Web技术的控制台等! 如果要使用Ctrl +〜,甚至可以在Visual Studio Code中选择WSL / bash作为默认外壳。

Hope this gets you started with Linux on Windows. What did I miss? Sound off in the comments.

希望这可以帮助您开始使用Windows上Linux。 我错过了什么? 在评论中听起来不错。

Sponsor: Preview the latest JetBrains Rider with its Assembly Explorer, Git Submodules, SQL language injections, integrated performance profiler and more advanced Unity support.

赞助商:预览最新的JetBrains Rider,包括其Assembly Explorer,Git子模块,SQL语言注入,集成的性能分析器以及更高级的Unity支持。

翻译自: https://www.hanselman.com/blog/web-development-and-advanced-techniques-with-linux-on-windows-wsl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值