by András Magyar
由AndrásMagyar
PHP development on Windows has some disadvantages. But, Microsoft now offers a great option for PHP developers who work on Windows: The Windows Subsystem for Linux (WSL). WSL is a compatibility layer for running Linux binary executables (in ELF format) natively on Windows 10. Microsoft says:
Windows上PHP开发有一些缺点。 但是,Microsoft现在为在Windows上工作PHP开发人员提供了一个不错的选择:Linux的Windows子系统(WSL)。 WSL是一个兼容性层,用于在Windows 10上本地运行Linux二进制可执行文件(ELF格式)。
“This is primarily a tool for developers — especially web developers and those who work on or with open source projects”.
“这主要是针对开发人员的工具,尤其是Web开发人员以及从事开源项目或与之合作的人员。”
We can run a Linux environment directly on Windows without the overhead of a virtual machine.
我们可以直接在Windows上运行Linux环境,而无需增加虚拟机的开销。
Note: This article is not only for the Windows Insiders. These methods will work on the latest stable releases of Windows 10 as well.
注意:本文不仅适用于Windows Insiders。 这些方法也将适用于Windows 10的最新稳定版本。
In this tutorial, we will set up a LAMP stack (Ubuntu 16.04, Apache, PHP 7.1, MariaDB) on WSL for development. You can set up other stacks (for example, a LEMP stack) with similar methods.
在本教程中,我们将在WSL上设置一个LAMP堆栈(Ubuntu 16.04,Apache,PHP 7.1,MariaDB)进行开发。 您可以使用类似的方法设置其他堆栈(例如LEMP堆栈)。
Before you begin this guide, you will need the following:
在开始本指南之前,您需要满足以下条件:
A 64-bit version of Windows 10 with the Creators Update or later.
具有Creators Update或更高版本的Windows 10的64位版本。
familiarity with Linux/bash (If you would like to get familiar with the command-line, you can read this DigitalOcean tutorial).
熟悉Linux / bash(如果您想熟悉命令行,可以阅读DigitalOcean教程 )。
First, you will need WSL installed on your computer.
首先,您需要在计算机上安装WSL。
You can install more Linux distributions from the Microsoft Store (Ubuntu, openSUSE, SUSE Linux Enterprise Server 12). But, in this tutorial, we will set up the LAMP stack on Ubuntu, so you need to select Ubuntu.
您可以从Microsoft商店(Ubuntu,openSUSE,SUSE Linux Enterprise Server 12)安装更多Linux发行版。 但是,在本教程中,我们将在Ubuntu上设置LAMP堆栈, 因此您需要选择Ubuntu 。
Microsoft has a great tutorial on how to install WSL, please follow the instructions of the article.
Microsoft有一个很好的教程,介绍如何安装WSL, 请按照本文的说明进行操作 。
If you have successfully installed Bash on Ubuntu on Windows, let’s install and configure a simple LAMP stack for development.
如果您已经在Windows的Ubuntu上成功安装了Bash,那么让我们安装并配置一个简单的LAMP堆栈进行开发。
We want to install the latest stable version of Apache, but official Ubuntu repositories don’t contain the latest release.
我们想安装Apache的最新稳定版本,但是正式的Ubuntu存储库不包含最新版本。
We need to add a PPA for Apache packages. A Personal Package Archive (PPA) is a repository that allows third-party developers to build and distribute packages for Ubuntu. Ondřej Surý’s PPA offers the latest Apache/PHP packages for Ubuntu.
我们需要为Apache软件包添加一个PPA。 个人软件包档案(PPA)是一个存储库,允许第三方开发人员为Ubuntu构建和分发软件包。 OndřejSurý的PPA为Ubuntu提供了最新的Apache / PHP软件包。
To add the PPA, run the following command in the WSL bash:
要添加PPA,请在WSL bash中运行以下命令:
sudo add-apt-repository ppa:ondrej/apache2
Once the PPA is configured, update the local package index:
一旦配置了PPA,请更新本地软件包索引:
sudo apt-get update
Install Apache:
安装Apache:
sudo apt-get install apache2
Create a project folder for your web applications. This folder should be outside of the WSL filesystem. I recommend you to use your Documents folder.
为您的Web应用程序创建一个项目文件夹。 该文件夹应该在WSL文件系统之外。 我建议您使用“文档”文件夹。
The following command will create a server folder inside your Documents directory. Please replace YOUR WINDOWS USERNAME with your Windows username.
以下命令将在您的Documents目录中创建一个服务器文件夹。 请用您的Windows用户名替换您的WINDOWS USERNAME 。
sudo mkdir /mnt/c/Users/YOUR WINDOWS USERNAME/Documents/server
Create a symbolic link to the selected folder.
创建到所选文件夹的符号链接。
sudo ln -s /mnt/c/Users/YOUR WINDOWS USERNAME/Documents/server /var/www/devroot
Open the Apache default virtual host configuration file:
打开Apache默认虚拟主机配置文件:
sudo nano /etc/apache2/sites-enabled/000-default.conf
Modify the document root to “/var/www/devroot”, which points to your project folder outside of WSL’s filesystem. Set ServerName
to localhost
(if the port 80 is reserved by a Windows application, replace 80 with an unused port):
将文档根目录修改为“ / var / www / devroot”,该目录指向WSL文件系统之外的项目文件夹。 将ServerName
设置为localhost
(如果Windows应用程序保留了端口80,则将80替换为未使用的端口):
ServerName localhost ServerAdmin webmaster@localhost DocumentRoot /var/www/devroot Options Indexes FollowSymLinks AllowOverride All Require all granted ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
When you are finished, save the file by pressing Ctrl-O, and hit Enter to confirm. Exit with Ctrl-X.
完成后,按Ctrl-O保存文件,然后按Enter确认。 使用Ctrl-X退出。
Open your favorite Windows editor/IDE, and create an “index.html” file in your project folder (C:\Users\ YOUR WINDOWS USERNAME\Documents\server) with the following content:
打开您喜欢的Windows编辑器/ IDE,并在项目文件夹(C:\ Users \ 您的WINDOWS USERNAME \ Documents \ server)中创建一个“ index.html”文件,其内容如下:
It works! <body> It works!