久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

適用于Linux的Windows子系統(tǒng)的完整開發(fā)人員指南2

 黃爸爸好 2021-04-03

This tutorial demonstrates how to install, manage, and use a full Linux environment on your Windows 10 PC with WSL2. You’ll be running a Linux kernel and executing native applications while seamlessly sharing files and services with Windows code editors and browsers.

本教程演示了如何在具有WSL2的Windows 10 PC上安裝,,管理和使用完整的Linux環(huán)境,。 您將運(yùn)行Linux內(nèi)核并執(zhí)行本機(jī)應(yīng)用程序,,同時(shí)與Windows代碼編輯器和瀏覽器無縫共享文件和服務(wù),。

為什么要使用Linux,? (Why Use Linux?)

Your host almost certainly uses Linux. It’s fast, open source, and runs the majority of web software including:

您的主機(jī)幾乎可以肯定使用Linux。 它是快速,,開放源代碼,并且運(yùn)行大多數(shù)Web軟件,,包括:

  • servers (Apache, NGINX, LiteSpeed, etc.) 服務(wù)器(Apache,,NGINX,LiteSpeed等)
  • language runtimes (PHP, Python, Node.js, Ruby, etc.) 語言運(yùn)行時(shí)(PHP,,Python,,Node.js,Ruby等)
  • utility libraries (image manipulation, ZIP creation, emailers, etc.) 實(shí)用程序庫(圖像處理,,ZIP創(chuàng)建,,電子郵件程序等)
  • databases (MySQL, MongoDB, Redis, etc.) 數(shù)據(jù)庫(MySQL,MongoDB,,Redis等)
  • other dependencies (Elastic Search, RabbitMQ, proxies, etc.) 其他依賴項(xiàng)(彈性搜索,,RabbitMQ,,代理等)

Some of these dependencies may be available on Windows and macOS, but installing and maintaining identical versions is difficult. They’re often slower and you may encounter subtle differences which will not become apparent until you deploy.

其中一些依賴項(xiàng)可能在Windows和macOS上可用,但是很難安裝和維護(hù)相同的版本,。 它們通常較慢,,并且您可能會遇到細(xì)微的差異,直到您進(jìn)行部署,,這些差異才會變得明顯,。

Some developers install Linux on their desktop, but that may not be viable if you regularly require non-Linux software such as Microsoft or Adobe products. Alternative options:

一些開發(fā)人員在其桌面上安裝Linux,但是如果您經(jīng)常需要非Linux軟件(例如Microsoft或Adobe產(chǎn)品),,則可能不可行,。 替代選項(xiàng):

  1. Use two PCs or dual boot on a single device. Switching between systems can be cumbersome. 在單個(gè)設(shè)備上使用兩臺PC或雙啟動。 系統(tǒng)之間的切換可能很麻煩,。
  2. Run a Linux virtual machine (VM) using Hyper-V, VirtualBox, Parallels, WMware, or Vagrant. This requires considerable OS resources and reasonable IT knowledge. Sharing files between systems can be awkward. 使用Hyper-V,,VirtualBox,Parallels,,WMware或Vagrant運(yùn)行Linux虛擬機(jī)(VM),。 這需要大量的OS資源和合理的IT知識。 在系統(tǒng)之間共享文件可能很麻煩,。
  3. Run web applications in Docker containers. It’s another topic to learn and Docker itself runs natively on Linux. Both the Windows and macOS editions use Linux below the surface. 在Docker容器中運(yùn)行Web應(yīng)用程序,。 這是另一個(gè)要學(xué)習(xí)的話題,Docker本身可以在Linux上本地運(yùn)行,。 Windows和macOS版本都在底層使用Linux,。

什么是Linux的Windows子系統(tǒng)? (What is the Windows Subsystem for Linux?)

The Windows Subsystem for Linux (WSL) lets developers run a Linux environment directly on Windows 10. It’s an unmodified OS, but highly integrated into Windows and without the overhead of a virtual machine.

Windows Linux子系統(tǒng)(WSL)使開發(fā)人員可以在Windows 10上直接運(yùn)行Linux環(huán)境,。這是未經(jīng)修改的OS,,但高度集成到Windows中,并且沒有虛擬機(jī)的開銷,。

WSL1 was groundbreaking and translated Linux system calls to Windows equivalents. This could be slow and several features weren’t available. WSL2 uses Hyper-V virtual machine technology so Linux compatibility is excellent and file access is up to 20x faster than before.

WSL1具有開創(chuàng)性,,并將Linux系統(tǒng)調(diào)用轉(zhuǎn)換為Windows等效項(xiàng)。 這可能很慢,,并且某些功能不可用,。 WSL2使用Hyper-V虛擬機(jī)技術(shù),因此Linux兼容性出色,,文件訪問速度比以前快20倍,。

Windows 10要求 (Windows 10 Requirements)

WSL2 is compatible with the Home, Pro, or Server editions of Windows but not Windows 10 S (although you can often upgrade to the Home edition for free).

WSL2與Windows的Home,Pro或Server版本兼容,,但與Windows 10 S 兼容(盡管您通常可以免費(fèi)升級到Home版本),。

The Windows May 2020 update is essential — that’s version 2004 or above. Choose the Settings cog from the Start menu, then select System followed by About. The Windows specifications are shown at the bottom of the panel:

Windows 2020年5月更新非常重要-版本2004或更高版本,。 從開始菜單中選擇設(shè)置齒輪,,然后選擇系統(tǒng) ,然后選擇關(guān)于 ,。 Windows規(guī)格顯示在面板底部:

Windows specifications

The update has been slowly rolling out since the end of May 2020. If you have a version below 2004, you may be able to trigger the update by clicking the Check for updates button in Settings, then Update & Security.

更新自2020年五月的結(jié)束已經(jīng)慢慢鋪開如果你有2004下方的版本,,您可以通過點(diǎn)擊設(shè)置 檢查更新按鈕,則更新和安全觸發(fā)更新,。

If 2004 remains stubbornly unavailable, you may be able to download and install it manually from microsoft.com/software-download/windows10

如果2004年仍然無法使用,,則可以從microsoft.com/software-download/windows10手動下載并安裝它

However, be aware some PCs report that version 2004 is currently unavailable in the Windows Update panel. You won’t be able to upgrade until Microsoft has released a fix for your device.

但是,請注意,,有些PC報(bào)告Windows Update面板中當(dāng)前不提供2004版,。 在Microsoft為您的設(shè)備發(fā)布修復(fù)程序之前,您將無法升級,。

電腦要求 (PC Requirements)

WSL2 (and Hyper-V) require hardware virtualization support to be enabled in your BIOS.

WSL2(和Hyper-V)要求在您的BIOS中啟用硬件虛擬化支持,。

WARNING: fiddling with your BIOS settings can trash your PC! Be careful when making changes. Consult your manufacturer’s help pages or search for online advice about your specific make and model.

警告: 擺弄BIOS設(shè)置可能會損壞您的PC! 進(jìn)行更改時(shí)要小心,。 請查閱制造商的幫助頁面,,或搜索有關(guān)特定品牌和型號的在線建議。

Hardware virtualization will be active on most devices, but you can check by rebooting, and opening the BIOS panels — typically by hitting the DEL, F2, or F10 key as the system starts. Look for Virtualization Technology, VTx or similar options. Ensure they are enabled, save, and reboot the device.

硬件虛擬化將在大多數(shù)設(shè)備上處于活動狀態(tài),,但是您可以通過重新啟動并打開BIOS面板進(jìn)行檢查-通常在系統(tǒng)啟動時(shí)按DEL ,, F2F10鍵。 尋找虛擬化技術(shù) ,, VTx或類似選項(xiàng),。 確保已啟用它們,保存并重新啟動設(shè)備,。

Finally, ensure your C: drive has at least 1GB of spare disk space — ideally more. You may be able to free up some space in Settings, System, Storage or using the Disk clean-up tool in Windows Administrative Tools.

最后,,確保您的C:驅(qū)動器至少有1GB的備用磁盤空間,最好是更多,。 您可能可以釋放“設(shè)置” ,,“ 系統(tǒng)” ,“ 存儲 ”中的某些空間,,或者使用Windows管理工具中的“ 磁盤清理”工具釋放空間,。

At the time of writing, WSL2 will install Linux to your C: drive, although it’s possible to move it elsewhere after installation.

在撰寫本文時(shí),WSL2會將Linux安裝到您的C:驅(qū)動器,,盡管可以在安裝后將其移動到其他位置 ,。

啟用WSL2 (Enable WSL2)

The Virtual Machine Platform and Windows Subsystem for Linux features must be enabled in Turn Windows features on or off. The panel can be accessed by hitting Start and typing “features” or from the Programs and Features icon in the classic Control Panel.

必須在“ 打開或關(guān)閉Windows功能”中啟用適用于Linux虛擬機(jī)平臺Windows子系統(tǒng)功能。 可以通過單擊開始并鍵入“功能”或從經(jīng)典控制面板中的“ 程序和功能”圖標(biāo)來訪問該面板,。

enable WSL2

Alternatively, you can execute the following commands in a Windows Powershell terminal run as an Administrator:

或者,,您可以在以管理員身份運(yùn)行的Windows Powershell終端中執(zhí)行以下命令:

  1. dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  2. dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Reboot Windows 10, then enable WSL2 as the default by entering the following command in a Windows Powershell or command prompt:

重新啟動Windows 10,然后在Windows Powershell或命令提示符中輸入以下命令,將WSL2設(shè)置為默認(rèn)值:

wsl --set-default-version 2

下載Linux (Download Linux)

You can install any number of Linux distros from the Microsoft Store accessed in the Start menu. This includes Ubuntu, Debian, SUSE, Kali, and Alpine. Enter “Linux” in the search box:

您可以從“開始”菜單中訪問的Microsoft Store安裝任何數(shù)量的Linux發(fā)行版,。 其中包括Ubuntu,,Debian,SUSE,,Kali和Alpine,。 在搜索框中輸入“ Linux”

Microsoft Store

Ubuntu is a good choice unless you have specific requirements. The latest edition will be installed if you click the Ubuntu icon followed the Get button.

除非您有特定要求,否則Ubuntu是一個(gè)不錯(cuò)的選擇,。 如果單擊“ 獲取”按鈕后的Ubuntu圖標(biāo),,將安裝最新版本。

The download could take a while depending on your network speed.

下載可能需要一段時(shí)間,,具體取決于您的網(wǎng)絡(luò)速度,。

啟動Linux (Launch Linux)

Following download, click the Launch button on the Microsoft Store app, or the new Ubuntu icon in the Start menu. This will complete the installation process and can take several minutes.

下載后,單擊Microsoft Store應(yīng)用程序上的“ 啟動”按鈕,,或單擊“開始”菜單中的新Ubuntu圖標(biāo),。 這將完成安裝過程,可能需要幾分鐘,。

Note: the first time you launch a WSL2 distro, you may see a message in the terminal about a kernel update. Copy the link into your browser, then download and install the update. You will need to launch Ubuntu again to continue with the installation.

注意:第一次啟動WSL2發(fā)行版時(shí),,您可能會在終端中看到有關(guān)內(nèi)核更新的消息。 將鏈接復(fù)制到瀏覽器中,,然后下載并安裝更新,。 您將需要再次啟動Ubuntu才能繼續(xù)安裝。

You’ll be prompted to enter a username and password. These are the credentials for Linux administration: they are completely separate from your Windows username and password (although choosing the same ones may be practical).

系統(tǒng)將提示您輸入用戶名和密碼,。 這些是Linux管理的憑據(jù): 它們與Windows用戶名和密碼完全分開 (盡管選擇相同的用戶名和密碼是可行的),。

Linux will eventually be ready and your terminal will show content similar to this:

Linux最終將準(zhǔn)備就緒,您的終端將顯示類似以下內(nèi)容:

  1. Installing, this may take a few minutes...
  2. Please create a default UNIX user account. The username does not need to match your Windows username.
  3. For more information visit: https:///wslusers
  4. Enter new UNIX username: ******
  5. New password:
  6. Retype new password:
  7. passwd: password updated successfully
  8. Installation successful!
  9. To run a command as administrator (user 'root'), use 'sudo <command>'.
  10. See 'man sudo_root' for details.
  11. Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.19.104-microsoft-standard x86_64)
  12. * Documentation: https://help.
  13. * Management: https://landscape.
  14. * Support: https:///advantage
  15. System information as of Mon Jul 6 12:07:38 BST 2020
  16. System load: 0.27 Processes: 8
  17. Usage of /: 0.4% of 250.98GB Users logged in: 0
  18. Memory usage: 0% IPv4 address for eth0: 172.21.232.173
  19. Swap usage: 0%
  20. 0 updates can be installed immediately.
  21. 0 of these updates are security updates.
  22. The list of available updates is more than a week old.
  23. To check for new updates run: sudo apt update

更新Linux (Update Linux)

There are likely to be several Linux updates. To update Ubuntu, enter the following commands in the Linux terminal:

可能會有幾個(gè)Linux更新,。 要更新Ubuntu,,請?jiān)贚inux終端中輸入以下命令:

  1. sudo apt-get update
  2. sudo apt-get upgrade
  3. sudo apt-get dist-upgrade
  4. sudo apt-get autoremove

Other Linux distros will have a similar process, but check the documentation for assistance.

其他Linux發(fā)行版將具有類似的過程,但請查看文檔以獲取幫助,。

在WSL1和WSL2之間切換 (Switch Between WSL1 and WSL2)

Existing WSL1 distros can be converted to WSL2. Entering the following command in a Windows Powershell terminal to display the installed Linux distros and their WSL version:

現(xiàn)有的WSL1發(fā)行版可以轉(zhuǎn)換為WSL2,。 在Windows Powershell終端中輸入以下命令以顯示已安裝的Linux發(fā)行版及其WSL版本:

  1. PS C:\> wsl --list --verbose
  2. NAME STATE VERSION
  3. * Ubuntu Running 1

(The * asterisk highlights the default Linux installation.)

(*號突出顯示了默認(rèn)的Linux安裝。)

To switch Ubuntu to WSL2, enter:

要將Ubuntu切換到WSL2,,請輸入:

wsl --set-version Ubuntu 2

Similarly, to switch back to WSL1, enter:

同樣,,要切換回WSL1,請輸入:

wsl --set-version Ubuntu 1

設(shè)置默認(rèn)的Linux發(fā)行版 (Set a Default Linux Distribution)

When you have multiple Linux distributions installed, one must be set as the default. It will be used when wsl is entered in a Windows Powershell terminal.

當(dāng)您安裝了多個(gè)Linux發(fā)行版時(shí),,必須將其設(shè)置為默認(rèn)發(fā)行版,。 在Windows Powershell終端中輸入wsl時(shí)將使用它。

To set the default distro, list your installations:

要設(shè)置默認(rèn)發(fā)行版,,請列出您的安裝:

wsl --list

and set a default with:

并設(shè)置默認(rèn)值:

wsl --setdefault <DistributionName>

Ubuntu example: wsl --setdefault Ubuntu.

Ubuntu示例: wsl --setdefault Ubuntu ,。

以特定用戶身份運(yùn)行Linux (Run Linux as a Specific User)

To run your default distribution as a specific user, enter the following command in a Windows Powershell terminal:

要以特定用戶身份運(yùn)行默認(rèn)分發(fā),,請?jiān)赪indows Powershell終端中輸入以下命令:

wsl --user <username>

A default user is defined during installation and you can create others. A root user is also created, but you should avoid using it under normal circumstances. It’s too easy to enter a dangerous command!

在安裝過程中定義了一個(gè)默認(rèn)用戶,您可以創(chuàng)建其他用戶,。 還創(chuàng)建了一個(gè)root用戶,,但在正常情況下應(yīng)避免使用它。 輸入危險(xiǎn)命令太容易了,!

移動您的Linux磁盤映像 (Move Your Linux Disk Image)

The Linux disk image is installed on your C: drive. Optionally, you can move it to another drive to free up space. The following instructions presume you’ll move it to D:\wsl.

Linux磁盤映像安裝在C:驅(qū)動器上。 (可選)您可以將其移動到另一個(gè)驅(qū)動器以釋放空間,。 以下說明假定您將其移至D:\wsl ,。

In a Windows Powershell terminal (not the Ubuntu terminal), enter wsl --list to view your Linux distributions, then export one by name to a back-up .tar file, e.g. D:\backup\ubuntu.tar:

在Windows Powershell終端( 不是 Ubuntu終端)中,輸入wsl --list以查看您的Linux發(fā)行版,,然后按名稱將其導(dǎo)出到備份的.tar文件,,例如D:\backup\ubuntu.tar

  1. mkdir D:\backup
  2. wsl --export Ubuntu D:\backup\ubuntu.tar

Unregister the same distribution to remove it from the C: drive:

注銷相同的發(fā)行版以將其從C:驅(qū)動器中刪除:

wsl --unregister Ubuntu

Enter wsl --list to verify the distribution has been removed.

輸入wsl --list以確認(rèn)分發(fā)已刪除。

Import the backup into a new WSL2 distribution at another location, such as D:\wsl:

將備份導(dǎo)入另一個(gè)位置的新WSL2發(fā)行版,,例如D:\wsl

  1. mkdir D:\wsl
  2. wsl --import Ubuntu D:\wsl\ D:\backup\ubuntu.tar

Verify it has been successfully created by entering wsl --list and launching the Ubuntu app from the Start menu.

通過輸入wsl --list并從“開始”菜單啟動Ubuntu應(yīng)用程序,,驗(yàn)證它已成功創(chuàng)建。

Unfortunately, Ubuntu will now use root as the default user. To use your own account, enter the following command:

不幸的是,,Ubuntu現(xiàn)在將使用root作為默認(rèn)用戶,。 要使用您自己的帳戶,請輸入以下命令:

ubuntu config --default-user <yourname>

where <yourname> is the username you defined during installation.

其中<yourname>是您在安裝過程中定義的用戶名,。

Presuming all has gone well, you can safely delete the backup file (D:\backup\ubuntu.tar).

假設(shè)一切順利,,您可以安全地刪除備份文件( D:\backup\ubuntu.tar )。

安裝Windows Terminal (Install Windows Terminal)

To launch the Linux terminal, you can use the Ubuntu icon, enter wsl or bash in Powershell, or use any third-party option such as Cmder, ConEmu, or Hyper. Profiles can be defined to launch %windir%\system32\bash.exe ~.

要啟動Linux終端,,可以使用Ubuntu圖標(biāo),,在Powershell中輸入wslbash ,或使用任何第三方選項(xiàng),,例如Cmder ,, ConEmuHyper 。 可以定義配置文件以啟動%windir%\system32\bash.exe ~ ,。

Another option is Windows Terminal, which is also available from the Microsoft Store or its repository at github.com/microsoft/terminal/.

另一個(gè)選項(xiàng)是Windows Terminal ,,它也可以從Microsoft Store或其github.com/microsoft/terminal/的存儲庫中獲得

Windows Terminal

Windows Terminal will automatically add your WSL2 Linux distros and offers a highly configurable range of options including tabs, split views, themes, transparency, and key bindings.

Windows Terminal會自動添加您的WSL2 Linux發(fā)行版,,并提供高度可配置的選項(xiàng)范圍,,包括選項(xiàng)卡,拆分視圖,,主題,,透明度和鍵綁定。

Options are defined in the settings.json file accessed from the Settings menu or Ctrl + , (comma). Default values and theme settings can be viewed in defaults.json, accessed by holding down Alt while clicking the Settings menu.

選項(xiàng)在定義settings.json設(shè)置菜單或Ctrl +,,(逗號)訪問的文件,。 可以在defaults.json查看默認(rèn)值和主題設(shè)置,方法是在單擊“ 設(shè)置”菜單的同時(shí)按住Alt鍵進(jìn)行訪問。

If you want the complete in-depth guide to installing, configuring, and using Windows Terminal, we’ve got you covered.

如果您想獲得有關(guān)安裝,,配置和使用Windows Terminal的完整的深入指南,,我們將幫助您。

? The Complete Guide to Windows Terminal

?Windows 終端機(jī)完整指南

從Windows訪問Linux文件 (Accessing Linux Files from Windows)

Linux files can be accessed at the network path \\wsl$\. This can be entered in the File Explorer address bar or any file open dialog.

可以在網(wǎng)絡(luò)路徑\\wsl$\訪問Linux文件,。 可以在“文件資源管理器”地址欄中或任何文件打開對話框中輸入,。

Your installed Linux distros are listed, so you can access the Ubuntu root directory at \\wsl$\Ubuntu. Your personal Linux files will typically be stored at:

列出了已安裝的Linux發(fā)行版,因此您可以訪問\\wsl$\Ubuntu的Ubuntu根目錄,。 您的個(gè)人Linux文件通常存儲在:

\\wsl$\Ubuntu\home\<yourname>

It’s best to use this as the starting folder in Windows Terminal. Open the Settings, and add the following configuration line to the Ubuntu profile:

最好將其用作Windows Terminal中的起始文件夾,。 打開“ 設(shè)置” ,然后將以下配置行添加到Ubuntu配置文件:

'startingDirectory': '//wsl$/Ubuntu/home/<yourname>/',

where <yourname> is the username you defined during installation.

其中<yourname>是您在安裝過程中定義的用戶名,。

映射網(wǎng)絡(luò)驅(qū)動器 (Map a Network Drive)

You can mount a network drive to \\wsl$\Ubuntu\ by opening \\wsl$\ in File Explorer, right-clicking the Ubuntu folder, and choosing Map network drive….

您可以通過在文件資源管理器中打開\\wsl$\ ,,將網(wǎng)絡(luò)驅(qū)動器安裝到\\wsl$\Ubuntu\ ,右鍵單擊Ubuntu文件夾,,然后選擇Map network drive… ,。

map Linux drive

Unfortunately, it’s not currently possible to map deeper subdirectories. You may also encounter mapped-drive issues with some applications.

不幸的是,當(dāng)前無法映射更深的子目錄,。 您可能還會在某些應(yīng)用程序中遇到映射驅(qū)動器問題,。

從Linux訪問Windows文件 (Accessing Windows Files from Linux)

Windows drives are mounted in the Linux /mnt/ directory. For example, your personal Users folder at C:\Users\<yourname> is available at:

Windows驅(qū)動器安裝在Linux /mnt/目錄中。 例如,,您的個(gè)人用戶文件夾C:\Users\<yourname>可在以下位置找到:

/mnt/c/Users/<yourname>

For ease of access, you can create a symbolic link to any Windows folder from the Ubuntu terminal, e.g. for C:\projects\code\:

為了便于訪問,,您可以從Ubuntu終端創(chuàng)建指向任何Windows文件夾的符號鏈接,例如C:\projects\code\

  1. cd ~
  2. ln -s /mnt/c/projects/code/

A code folder will appear in your home directory. Navigate to it using cd ~/code and you will actually be in /mnt/c/projects/code/, which maps directly to C:\projects\code\.

code文件夾將出現(xiàn)在您的主目錄中,。 使用cd ~/code導(dǎo)航到它,,您實(shí)際上將位于/mnt/c/projects/code/ ,它直接映射到C:\projects\code\ ,。

Accessing Windows files from Linux is considerably slower than using the native Linux file system. Where possible, create projects within the Linux file space, typically within your home folder (/home/<yourname>/ or ~).

從Linux訪問Windows文件比使用本機(jī)Linux文件系統(tǒng)要慢得多,。 盡可能在Linux文件空間中創(chuàng)建項(xiàng)目,通常在您的主文件夾( /home/<yourname>/~ )中創(chuàng)建項(xiàng)目,。

從Windows運(yùn)行Linux命令 (Running Linux Commands from Windows)

Any Linux (bash) shell command can be run from a Windows Powershell or command line terminal using wsl:

可以使用wsl從Windows Powershell或命令行終端運(yùn)行任何Linux(bash)shell命令:

wsl <linux-command>

For example: wsl ls -la lists the full contents and details of files in a Windows folder.

例如: wsl ls -la列出Windows文件夾中文件的全部內(nèi)容和詳細(xì)信息,。

從Linux運(yùn)行Windows應(yīng)用程序 (Running Windows Applications from Linux)

Any Windows executable can be launched from Linux (it’s normally necessary to specify the .exe extension). For example, to open the current Linux directory in File Explorer, enter:

可以從Linux啟動任何Windows可執(zhí)行文件(通常需要指定.exe擴(kuò)展名)。 例如,,要在文件資源管理器中打開當(dāng)前的Linux目錄,,請輸入:

explorer.exe .

or edit .bashrc in Notepad:

或在記事本中編輯.bashrc

notepad.exe ~/.bashrc

or open the a specific project directory in VS Code:

或在VS Code中打開特定的項(xiàng)目目錄:

code ~/projects/mywebsite

安裝應(yīng)用程序 (Installing Applications)

Always remember you’re running two operating systems. They may be highly integrated, but there are situations when you want an application installed in one or both.

永遠(yuǎn)記住您正在運(yùn)行兩個(gè)操作系統(tǒng)。 它們可能是高度集成的,,但是在某些情況下,,您可能希望將應(yīng)用程序安裝在一個(gè)或兩個(gè)中。

It may be practical to use Git from either Windows or Linux. The Windows edition is installed by downloading an executable, but Git on Ubuntu is installed using:

從Windows或Linux使用Git可能是實(shí)際的,。 Windows版本是通過下載可執(zhí)行文件來安裝的,,而Ubuntu上的Git是使用以下命令安裝的:

  1. sudo apt-get update
  2. sudo apt-get install git-all

Similarly, you may want to test Node.js applications in both Windows and Linux. Again, Windows has an installer, but Node.js 14.x is installed in Ubuntu using the commands:

同樣,,您可能要在Windows和Linux中測試Node.js應(yīng)用程序。 同樣,, Windows有一個(gè)安裝程序 ,,但是使用以下命令在Ubuntu中安裝了Node.js 14.x:

  1. sudo apt-get install build-essential
  2. curl -sL https://deb./setup_14.x | sudo -E bash -
  3. sudo apt-get install -y nodejs

git, node and npm commands will now work in either environment.

gitnodenpm命令現(xiàn)在將在兩種環(huán)境中npm使用,。

安裝Docker (Installing Docker)

One exception to the “install twice” rule is Docker!

“兩次安裝”規(guī)則的一個(gè)例外是Docker,!

Docker runs applications in isolated Linux containers which are conceptually similar to lightweight virtual machines with an OS, single application, and its dependencies. Using Docker for web development provides a consistent environment which is easy to install and works on any device.

Docker在隔離的Linux容器中運(yùn)行應(yīng)用程序,這些容器在概念上類似于具有OS,,單個(gè)應(yīng)用程序及其依存關(guān)系的輕型虛擬機(jī),。 使用Docker進(jìn)行Web開發(fā)可提供一致的環(huán)境,該環(huán)境易于安裝并且可在任何設(shè)備上運(yùn)行,。

Docker Desktop for Windows is fully WSL2-enabled:

適用于Windows的Docker桌面完全支持WSL2:

  • there’s no need to use Hyper-V, so Docker can be installed on Windows Home 無需使用Hyper-V,因此Docker可以安裝在Windows Home上
  • Docker starts and runs considerably faster when configured to use WSL2 配置為使用WSL2時(shí),,Docker啟動并運(yùn)行得更快
  • installing Docker Desktop on Windows enables docker and docker-compose in both Windows and Linux.

    在Windows上安裝Docker Desktop可以在Windows和Linux中啟用dockerdocker-compose ,。

Docker Desktop suggests you use WSL2 when it’s first launched. Alternatively, you can select Settings from the Docker system tray icon menu, then choose the General tab, check Use the WSL 2 based engine, and hit Apply & Restart.

Docker Desktop建議您在WSL2首次啟動時(shí)使用它。 或者,,您可以從Docker系統(tǒng)任務(wù)欄圖標(biāo)菜單中選擇設(shè)置 ,,然后選擇常規(guī)選項(xiàng)卡,選中使用基于WSL 2的引擎 ,,然后點(diǎn)擊Apply&Restart ,。

Docker WSL2 engine

Docker will use the default Linux distro, but you can also enable it in other installed distros from the WSL Integration panel in Settings, then Resources.

Docker將使用默認(rèn)的Linux發(fā)行版,但是您也可以在其他已安裝的發(fā)行版中,,從“ 設(shè)置”中的 WSL集成”面板,,然后單擊 資源 ”中啟用它。

使用Docker啟動Apache和PHP應(yīng)用程序 (Launching Apache and PHP Applications Using Docker)

It’s possible to install web dependencies such as Apache and PHP directly in your WSL2 Linux environment. However, Docker is safer, quicker, and more configurable: it allows you to run different versions of PHP on the same device at the same time.

可以直接在WSL2 Linux環(huán)境中安裝Web依賴項(xiàng),,例如Apache和PHP,。 但是,Docker更安全,,更快捷,,更可配置:它允許您在同一設(shè)備上同時(shí)運(yùn)行不同版本PHP。

In this example, you’ll create a small PHP file and run it using the latest release of PHP 7.4 in a Docker container. Ensure you’ve installed Docker Desktop for Windows and enabled WSL2 mode, then create a folder in your Linux environment with an index.php file. For example:

在此示例中,,您將創(chuàng)建一個(gè)小PHP文件,,并在Docker容器中使用最新版本PHP 7.4運(yùn)行它。 確保已經(jīng)安裝了適用于Windows的Docker桌面并啟用了WSL2模式,,然后在Linux環(huán)境中使用index.php文件創(chuàng)建一個(gè)文件夾,。 例如:

  1. mkdir ~/php
  2. cd ~/php
  3. touch index.php

Edit index.php, perhaps using Notepad or VS Code as shown above. Add the following content which outputs PHP status information:

編輯index.php ,也許使用記事本或VS Code,,如上所示 ,。 添加以下內(nèi)容,,輸出PHP狀態(tài)信息:

  1. <?php
  2. phpinfo();

Run the following docker command to start a PHP 7.4-enabled Apache web server in a container which mounts files in the host directory to /var/www/html in the container:

運(yùn)行以下docker命令以在容器中啟動啟用PHP 7.4的Apache Web服務(wù)器,該服務(wù)器將主機(jī)目錄中的文件安裝到容器中的/var/www/html

  1. docker run \
  2. -it --rm -p 8080:80 --name php \
  3. -v '$PWD':/var/www/html \
  4. php:7.4-apache

Note: this command would also work in Windows, although the reference to $PWD would need to be replaced by the full file path in Linux notation, e.g. /c/php/.

注意:此命令在Windows中也可以使用,,盡管對$PWD的引用需要用Linux表示法中的完整文件路徑替換,,例如/c/php/

Open https://localhost:8080/ in any Windows web browser to view the output generated by your PHP file:

在任何Windows Web瀏覽器中打開https://localhost:8080/以查看您PHP文件生成的輸出:

Docker container running Apache and PHP

To finish, press Ctrl + C in your Docker terminal to stop and delete the container.

要完成操作,,請?jiān)贒ocker終端中按Ctrl + C以停止并刪除容器,。

WSL2和localhost (WSL2 and localhost)

Applications running on Windows, WSL2 Linux, and Docker containers are always accessed from localhost or 127.0.0.1.

始終從localhost127.0.0.1訪問在Windows,WSL2 Linux和Docker容器上運(yùn)行的應(yīng)用程序,。

The PHP example above exposed port 8080, which was mapped to port 80 in the container. Similarly, if you have Node.js installed in Linux, you can cd to any directory and start a static file server:

上面PHP示例暴露了端口8080 ,,該端口已映射到容器中的端口80 。 同樣,,如果您在Linux中安裝了Node.js,,則可以cd到任何目錄并啟動靜態(tài)文件服務(wù)器:

npx small-static-server

Open http://localhost:8888/ in a browser to view files in that directory (index.html is returned by default).

在瀏覽器中打開http://localhost:8888/以查看該目錄中的文件(默認(rèn)返回index.html )。

Be aware that ports available in one OS may be unavailable in the other. Port 80 is often reserved by the Windows 10 Skype App and it cannot be changed (although the classic Desktop edition does permit it).

請注意,,一個(gè)操作系統(tǒng)中可用的端口可能在另一操作系統(tǒng)中不可用,。 端口80通常由Windows 10 Skype應(yīng)用程序保留,并且無法更改(盡管經(jīng)典的桌面版允許使用),。

SSL certificates that are either self-signed or locally trusted using tools such as mkcert can be used interchangeably in both Windows and Linux environments.

使用mkcert之類的工具進(jìn)行自我簽名或本地信任的SSL證書可以在Windows和Linux環(huán)境中互換使用,。

VS Code WSL2集成 (VS Code WSL2 Integration)

The WSL2 shell can be chosen as the default VS Code terminal by pressing Ctrl + Shift + P and typing/choosing Terminal: Select Default Shell, then selecting WSL Bash:

通過按Ctrl + Shift + P并鍵入/選擇Terminal,可以將WSL2 shell選作默認(rèn)的VS Code終端:選擇Default Shell ,,然后選擇WSL Bash

VS Code default terminal shell

Like other applications, VS Code can directly access Linux files at \\wsl$\ and debuggers running on localhost. There’s no need for further extensions but the following options permit easier Linux and Docker file management:

與其他應(yīng)用程序一樣,,VS Code可以直接訪問\\wsl$\ Linux文件以及在localhost運(yùn)行的調(diào)試器。 不需要進(jìn)一步擴(kuò)展,,但是以下選項(xiàng)使Linux和Docker文件管理更加輕松:

After installing Remote – WSL, you can navigate to any Linux directory and launch a WSL-connected instance of VS Code:

安裝Remote – WSL之后 ,,可以導(dǎo)航到任何Linux目錄并啟動與WSL連接的VS Code實(shí)例:

  1. cd ~/myproject
  2. code .

Alternatively, click the remote window icon in the bottom left of the application, then choose a connection option:

或者,單擊應(yīng)用程序左下方的遠(yuǎn)程窗口圖標(biāo),,然后選擇一個(gè)連接選項(xiàng):

VS Code remote window icon

Or use the new Remote Explorer icon in the Activity Bar. This allows you to choose a target type from the drop-down and connect accordingly:

或使用活動欄中的新的Remote Explorer圖標(biāo),。 這使您可以從下拉菜單中選擇目標(biāo)類型并進(jìn)行相應(yīng)的連接:

VS Code Remote Explorer

The remote instance of VS Code has a separate set of extensions and settings for Linux use. This permits WSL2 code debugging and is practical when you have a mix of language runtimes installed in either OS, an extension isn’t available in one system, or it requires different OS settings.

VS Code的遠(yuǎn)程實(shí)例具有單獨(dú)的一組擴(kuò)展名和設(shè)置供Linux使用。 這允許進(jìn)行WSL2代碼調(diào)試,,并且在兩種操作系統(tǒng)中都安裝了多種語言運(yùn)行時(shí),,一個(gè)系統(tǒng)中沒有擴(kuò)展名或需要不同的OS設(shè)置時(shí)非常實(shí)用。

The Extensions panel allows you to enable and configure extensions in the remote instance:

擴(kuò)展面板允許您在遠(yuǎn)程實(shí)例中啟用和配置擴(kuò)展:

VS Code extensions

The settings are stored in your Linux ~/.vscode-server directory.

設(shè)置存儲在~/.vscode-server目錄中,。

摘要 (Summary)

WSL2 will revolutionize your web development. It makes it easy to write code using Windows tools then run it in a Linux environment. While it was previously possible with virtual machines and Samba folder shares, WSL2 offers a simpler, faster, and highly integrated experience.

WSL2將徹底改變您的Web開發(fā),。 它使使用Windows工具編寫代碼然后在Linux環(huán)境中運(yùn)行代碼變得容易。 雖然以前使用虛擬機(jī)和Samba文件夾共享是可能的,,但WSL2提供了更簡單,,更快和高度集成的體驗(yàn)。

Further resources:

更多資源:

翻譯自: https://www./wsl2/

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多