需求描述

目前我本地的开发环境采用vagrant + homestead,但是每次启动着实难受,而且占用系统资源过高。所以尝试采用Ubuntu子系统

步骤

启用windows功能

通过Win10任务栏中的Cortana搜索框搜索打开“启用或关闭Windows功能”,向下滚动列表,即可看到“适用于Linux的Windows子系统(Beta)”项。
启用windows功能.jpg

启用开发人员模式

进入“设置 - 更新和安全 - 针对开发人员”设置页面,选中“开发人员模式”。如图:
启用开发人员.jpg

启用Linux子系统

右键点击Win10开始按钮,选择“Windows PowerShell(管理员)”以管理员身份运行Windows PowerShell。
输入并回车运行以下命令:

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

下载并安装Ubuntu

打开应用商店 (Microsoft Store),搜索Ubuntu,安装并启动Ubuntu:
安装ubuntu.jpg
首次启动需要几分钟,然后根据提示设置账号密码:
首次启动.jpg

更换软件源

更换apt源:

cd /etc/apt/
sudo cp sources.list sources.list.bak && sudo vim sources.list

vim命令模式下输入ggdG清空默认源,按i后将以下阿里源填入,保存退出

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

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

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

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

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

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

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

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

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

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

执行以下命令更新:

sudo apt-get update

sudo apt-get upgrade

注意:一定要确保可以正常更新,无报错,否则可能出现其他一些问题。

开发环境配置

接下来可以使用宝塔面板快速搭建本地环境,也不用担心数据是否安全。而开发工具,像VSCODE支持直接使用WSL终端

1.配置LNMP:将此脚本保存并执行: https://github.com/muliuyun/wsl-ubuntu/blob/master/18.04-init.sh (sudo bash 18.04-init.sh)

2.修改php-fpm配置,方便开发
php-fpm配置文件路径:/etc/php/7.2/fpm/pool.d/www.conf
使用vi搜索:/max_children设置为50,搜索:/request_terminate_timeout设置为300(方便本地做一些特殊长时间请求)
搜索:/fpm.sock如果存在,复制listen = /run/php/php7.2-fpm.sock(如果没有,可能是listen =127.0.0.1:900)这个值为nginx配置中的fastcgi_pass需要与这里的一致。

3.配置nginx示例

server {
    listen 8080;
    server_name _ ;
    root /mnt/h/www/demo/public;

    index index.html index.htm index.php;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        root           /mnt/h/www/demo/public;
        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

4.配置开机自动启动php-fpm等(子系统不能设置开机自启)

cd
vim startservers.sh

进入编辑模式后,输入以下代码:

#!/bin/bash
# Edit this for your relevant services.
services=(php7.2-fpm nginx redis-server mysql)
for service in "${services[@]}"
do
         if (( $(ps -ef | grep -v grep | grep $service | wc -l) == 0 ))
         then
         # Uncomment to get messages. #
         #echo "Starting service ${service}"
         (sudo service $service start > /dev/null &)
         fi
done

保存退出,给与可执行权限:

chmod +x startservers.sh

startservers.sh添加到.bashrc

echo ./startservers.sh >> .bashrc

如果不需要每次都输入密码,可以:

$sudo sed -i '$awesen ALL=(ALL:ALL) NOPASSWD: ALL' /etc/sudoers # $a后的wesen换成你的用户名

5.设置mysql用户:
查看默认密码:

cat /etc/mysql/debian.cnf

# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = GJItmFhjEscArQXu
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = GJItmFhjEscArQXu
socket   = /var/run/mysqld/mysqld.sock

登陆mysql:

mysql -udebian-sys-maint -pGJItmFhjEscArQXu

让后创建用户和授权即可~
创建用户:

CREATE USER 'wesen'@'localhost' IDENTIFIED BY 'wesen';

授权:

GRANT ALL ON *.* TO 'wesen'@'localhost';

刷新:

flush privileges;
Last modification:October 9th, 2019 at 11:02 pm