如何在Windows Subsystem for Linux(WSL)上设置PHP开发环境

by András Magyar

由AndrásMagyar

如何在Windows Subsystem for Linux(WSL)上设置PHP开发环境 (How to set up a PHP development environment on Windows Subsystem for Linux (WSL))

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堆栈)。

先决条件 (Prerequisites)

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教程 )。

步骤1:在Windows上安装bash (Step 1: installing bash on Windows)

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堆栈进行开发。

步骤2:安装Apache HTTP服务器 (Step 2: installing an Apache HTTP server)

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!

Start the Apache HTTP server:

启动Apache HTTP服务器:

sudo service apache2 start

Open http://localhost/ in your browser and you should see the “It works” title.

在浏览器中打开http:// localhost / ,您应该看到“ It works”标题。

Don’t forget to enable Apache modules that are necessary for you. For example, you can enable mod_rewrite:

不要忘记启用您需要的Apache模块。 例如,您可以启用mod_rewrite:

sudo a2enmod rewritesudo service apache2 restart

步骤3:安装MariaDB服务器 (Step 3: installing the MariaDB server)

Add a repo that contains the latest MariaDB packages:

添加一个包含最新MariaDB软件包的仓库:

sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://ams2.mirrors.digitalocean.com/mariadb/repo/10.2/ubuntu xenial main'

Install MariaDB:

安装MariaDB:

sudo apt-get updatesudo apt-get install mariadb-server

You will be prompted to create a root password during the installation. Choose a secure password and remember it, because you will need it later.

在安装过程中,系统将提示您创建root密码。 选择一个安全的密码并记住它,因为稍后您将需要它。

Start MariaDB:

启动MariaDB:

sudo service mysql start

Run the following script (this changes some of the less secure default options):

运行以下脚本(这将更改一些不太安全的默认选项):

mysql_secure_installation

步骤4:安装PHP (Step 4: installing PHP)

Add PPA for the latest PHP:

为最新PHP添加PPA:

sudo add-apt-repository ppa:ondrej/phpsudo apt-get update

Install PHP 7.1 packages:

安装PHP 7.1软件包:

sudo apt-get install php7.1 libapache2-mod-php7.1 php7.1-mcrypt php7.1-mysql php7.1-mbstring php7.1-gettext php7.1-xml php7.1-json php7.1-curl php7.1-zip

We have to restart Apache:

我们必须重新启动Apache:

sudo service apache2 restart

Create an info.php file in your project folder with the following content:

在项目文件夹中创建一个info.php文件,其内容如下:

Open http://localhost/info.php in your browser. If PHP works correctly, you should see the following:

在浏览器中打开http://localhost/info.php 。 如果PHP可以正常工作,则应该看到以下内容:

步骤5:安装phpMyAdmin (Step 5: installing phpMyAdmin)

phpMyAdmin is a free and open source administration tool for MySQL and MariaDB.

phpMyAdmin是针对MySQL和MariaDB的免费开放源代码管理工具。

With phpMyAdmin, you can easily create/manage your databases using a web interface.

使用phpMyAdmin,您可以使用Web界面轻松创建/管理数据库。

sudo apt-get install phpmyadmin
  • When the first prompt appears, press Space, Tab, and then Enter to select Apache.

    当出现第一个提示时,按Space,Tab键,然后按Enter选择Apache。
  • Select yes when asked to use dbconfig-common to set up the database.

    当要求使用dbconfig-common设置数据库时,选择“是”。
  • Provide your MariaDB root password

    提供您的MariaDB root密码
  • Choose a password for the phpMyAdmin application itself

    选择phpMyAdmin应用程序本身的密码

Enable the necessary PHP extensions:

启用必要PHP扩展:

sudo phpenmod mcryptsudo phpenmod mbstring

Restart Apache:

重新启动Apache:

sudo service apache2 restart

Now you can access phpMyAdmin on the following URL: http://localhost/phpmyadmin/You can login using the root username and the root password you set up during the MariaDB installation.

现在,您可以通过以下URL访问phpMyAdmin: http:// localhost / phpmyadmin /您可以使用在MariaDB安装期间设置的根用户名和根密码登录。

步骤6:安装Composer (Step 6: installing Composer)

Composer is a package manager for PHP. It allows you to install/update the libraries your project depends on. If you are a PHP developer you probably use composer.

Composer是PHP的软件包管理器。 它允许您安装/更新项目所依赖的库。 如果您是PHP开发人员,则可能使用composer。

Visit Composer’s download page and follow the instructions in the Command-line installation section. After Composer has installed successfully, you can install it globally:

访问Composer的下载页面,并按照“命令行安装”部分中的说明进行操作。 成功安装Composer之后,可以在全局范围内安装它:

sudo mv composer.phar /usr/local/bin/composer

Now it can be run from any location by typing:

现在,可以通过键入以下内容在任何位置运行它:

composer

步骤7:安装Git: (Step 7: installing Git:)

Git is a version control system which is primarily used for source code management. Learn more about Git here.

Git是一个版本控制系统,主要用于源代码管理。 在此处了解有关Git的更多信息 。

You can install it by running the following command:

您可以通过运行以下命令来安装它:

sudo apt-get install git

Before you use Git (and if you aren’t familiar with it), please read the “How To Set Up Git” section from the How To Install Git on Ubuntu 16.04 tutorial.

在使用Git(如果您不熟悉Git)之前,请阅读“如何在Ubuntu 16.04上安装Git”教程中的“如何设置Git”部分。

步骤8:在WSL上自动启动LAMP(可选) (Step 8: automatically start LAMP on WSL (optional))

Background tasks are currently not supported on WSL. When you close Bash your services (Apache and MariaDB) will stop.

WSL当前不支持后台任务。 当您关闭Bash时,您的服务(Apache和MariaDB)将停止。

Note for Windows Insiders: Background tasks are now supported on WSL starting with Windows Insider Build 17046 (for more details, you can read the following blog post: Background Task Support in WSL), but the auto start of services is still not available.

Windows Insiders注意:从Windows Insider Build 17046开始,WSL现在支持后台任务(有关更多详细信息,您可以阅读以下博客文章: WSL中的Background Task Support ),但是自动启动服务仍然不可用。

Unfortunately, automatically starting your services is a bit difficult.

不幸的是,自动启动服务有点困难。

Let’s configure autostarting!

让我们配置自动启动!

We need to start the services without typing your password.

我们需要在不输入密码的情况下启动服务。

Before you get started with this, please take a look at the following tutorial How To Edit the Sudoers File on Ubuntu and CentOS.

在开始此操作之前 ,请先阅读以下教程如何在Ubuntu和CentOS上编辑Sudoers文件 。

Run the following command:

运行以下命令:

sudo visudo -f /etc/sudoers.d/services

Copy and paste the following to the editor and then save:

将以下内容复制并粘贴到编辑器中,然后保存:

%sudo ALL=(root) NOPASSWD: /usr/sbin/service *%wheel ALL=(root) NOPASSWD: /usr/sbin/service *

This enables us to start the services (like Apache and MariaDB) without using our password.

这使我们无需使用密码即可启动服务(如Apache和MariaDB)。

Start Command Prompt (not the bash) as administrator and run:

以管理员身份启动命令提示符(不是bash)并运行:

SchTasks /Create /SC ONLOGON /TN "Start WSL LAMP" /TR "c:\Windows\System32\bash.exe -c 'sudo service apache2 start; sudo service mysql start; cd ~; bash'"

The above command creates a task that runs automatically when you login to Windows. It does the following:

上面的命令创建一个任务,该任务在您登录Windows时自动运行。 它执行以下操作:

  • Starts Apache

    启动Apache
  • Starts MariaDB

    启动MariaDB
  • Changes the directory to your home directory

    将目录更改为您的主目录

Don’t forget: when you close the terminal window, services will stop and you should restart them manually!

不要忘记:关闭终端窗口时,服务将停止,您应该手动重新启动它们!

步骤9:添加测试域(可选) (Step 9: add test domains (optional))

When you work on more web applications, multiple test domains will be helpful. For example, if you are working on myapp.com, you can access the local development version on http://myapp.test/ instead of http://localhost/myapp.

当您使用更多Web应用程序时,多个测试域将很有帮助。 例如,如果您使用的是myapp.com,则可以访问http://myapp.t est /而不是http:// localhost / myapp上的本地开发版本。

In the following, you can replace “myapp” with your web application’s name.Create a folder in your projects directory for your web application:

在下面,您可以用Web应用程序的名称替换“ myapp”。在您的项目目录中为Web应用程序创建一个文件夹:

sudo mkdir /mnt/c/Users/YOUR WINDOWS USERNAME/Documents/server/myapp

Add the virtual host file to Apache:

将虚拟主机文件添加到Apache:

sudo nano /etc/apache2/sites-available/myapp.test.conf

Save the following configuration to the new file (don’t forget to replace myapp with your application’s name).

将以下配置保存到新文件中(不要忘记将myapp替换为应用程序的名称)。

ServerName myapp.test
ServerAdmin webmaster@localhost DocumentRoot /var/www/devroot/myapp
 Options Indexes FollowSymLinks AllowOverride All Require all granted 
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined

Enable the new site:

启用新站点:

sudo a2ensite myapp.test

Restart Apache:

重新启动Apache:

sudo service apache2 restart

Finally, start Notepad or your favorite editor/IDE on Windows with admin privileges (Run as administrator) and open the hosts file. It is located in the c:\windows\system32\drivers\etc folder.

最后,在Windows上以管理员权限(以管理员身份运行 )启动记事本或您喜欢的编辑器/ IDE,然后打开hosts文件。 它位于c:\ windows \ system32 \ drivers \ etc文件夹中。

Add the following line to the end of the file and save it:

将以下行添加到文件末尾并保存:

127.0.0.1 myapp.test

Now you can access your web application on the http://myapp.test/ domain.You can also add more test domains with the same method.

现在,您可以在http://myapp.test/域上访问您的Web应用程序。您还可以使用相同的方法添加更多测试域。

结论 (Conclusion)

WSL does not replace Vagrant or Docker, and it is experimental. Automatically starting services is currently not supported on WSL, and this is one of the biggest problems with it at this moment. However, the Windows Subsystem for Linux is a great option for developers to use a native Linux shell on Windows. I think you should give it a try!

WSL不会替代Vagrant或Docker,它是试验性的。 WSL当前不支持自动启动服务,这是目前最大的问题之一。 但是,对于Linux开发人员来说,Windows子系统是在Windows上使用本机Linux Shell的绝佳选择。 我认为您应该尝试一下!

翻译自: https://www.freecodecamp.org/news/setup-a-php-development-environment-on-windows-subsystem-for-linux-wsl-9193ff28ae83/

你可能感兴趣的:(linux,java,docker,mysql,ubuntu)