最好的 Linux 桌面版---- Windows 10 安装体验

最好用的 Linux 桌面版---- Windows 10

简介

适用于 Windows 的 Linux 子系统(英语:Windows Subsystem for Linux,简称 WSL )是一个为在 Windows 10 和 Windows Server 2019 上能够原生运行 Linux 二进制可执行文件(ELF 格式)的兼容层。( WSL 发布有一段时间了,本人也是上周看了 WSL2 的宣传才知道有这么个好东西

安装

打开 Windows 10 应用商店搜索 Linux

clipboard.png

搜索结果中有可用的 Linux 各个版本,选择一个进行安装即可。

启用 WSL 功能

用管理员模式打开 PowerShell,执行命令:Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux 执行完命令需要重启电脑

重启完成后在 Windows 菜单中搜索刚刚安装的 Linux 程序,点击打开,WSL 进入初始化,初始化完成后即可开始开发之旅

安装 Nginx PHP

之前一直用的 centOS 系统,Windows 10 上没有,所以安装的 Ubuntu 18

安装 Nginx

【2019-05-15 更新】推荐使用 apt 直接下载安装,可以使用 service 命令启动

安装依赖(以下依赖是 Nginx 和 PHP 所需的一些依赖,对 apt 的包不熟,找了一下午)


apt-get install build-essential

apt-get install libpcre3-dev

apt-get install zlib1g-dev

apt-get install libssl-dev

apt-get install libgd-dev

apt-get install libxml2 libxml2-dev libbz2-dev libcurl4 libcurl4-gnutls-dev

apt-get install libreadline-dev

apt-get install libxslt1-dev

下载解压 Nginx 编译包


# 如果没有 wget 就先 apt install wget 一下

wget -q http://nginx.org/download/nginx-1.15.0.tar.gz

tar -zxvf nginx-1.15.0.tar.gz

添加 Nginx 用户


groupadd nginx

useradd nginx -s /sbin/nologin -g nginx -M

编译安装


cd nginx-1.15.0

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-pcre

make
make install

如果在编辑过程中提示缺少什么依赖,把报错信息复制到谷歌就会得到依赖包,不过谷歌一般搜到的都是 centOS 的 yum 包,可以去 这个网站 进行搜索 apt 的包名

编译完后建立软连接

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

# 输入 nginx -t
nginx -t

# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

【2019-05-16 更新】Ubuntu 可以使用 service,如果是用 apt 下载的,系统自带了 service ,如果是编译安装,就需要自己去 /etc/init.d/ 文件夹下创建一个 nginx.service 的文件,然后使用 chkconfig 加入服务。

nginx 开启、重启、停止的命令

# 启动
nginx

# 重启
nginx -s reload

# 停止
nginx -s quit

# 查看 nginx 进程
ps -ef | grep nginx

启动后查看 http://localhost ,显示 nginx 页面即代表 nginx 安装成功

安装 PHP

下载解压 PHP 编译包

# 下载的 7.1 版本的,如果有需要,可以去 PHP 官网下载其他版本
wget -q http://jp2.php.net/distributions/php-7.1.26.tar.gz
tar -zxvf php-7.1.26.tar.gz

配置、编译安装

# 以下配置是大部分 PHP 扩展,可以根据需要配置初始化 PHP 扩展,后续新增扩展也很方便
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/lib/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache

# 安装
make -j4 //据说 -j4 会快一点
make install

# 配置 ini
cp php.ini-development /usr/local/php/lib/etc/php.ini

# 加入环境变量
echo  "export PATH=$PATH:/usr/local/php/bin"  >> /etc/profile
source /etc/profile

和安装 Nginx 一样,如果缺少什么依赖,谷歌一下(面向谷歌编程

配置 php-fpm

如果上述步骤没有问题了,接下来就开始配置 php-fpm 了

# 复制 php-fpm.conf
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

# 复制 www.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

【2019-05-16 更新】 unix socket 可以使用,使用端口会有报错信息,但是使用文件 socket 没有报错。

启动 PHP-FPM

【2019-05-16 更新】将编译包下的 sapi/fpm/init.d.php-fpm 移动到 /etc/init.d/ 目录下,并重命名为 php-fpm ,然后使用
chkconfig 就可以加入服务,使用 service 来操作 php-fpm

ubuntu 使用 update-rc.d php-fpm defaults 加入服务,如果没用先给 php-fpm 可执行的权限:chmod u+x /etc/init.d/php-fpm

创建项目

# /mnt 是 windows 的“我的电脑”目录,/mnt/d 就是 D 盘
cd /mnt/d

# 安装 composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/bin/composer

# 创建 www 目录
mkdir www

# 创建 laravel 项目
cd www
composer create-project --prefer-dist laravel/laravel laravel

# 更改权限和所属人
chmod 0755 -R laravel/storage
chown -R nginx.nginx laravel/

配置 nginx

vim /usr/local/nginx/conf/nginx.conf

在这个文件尾部(最后一个 } 前面)加入

# 这个是每个项目的 nginx 配置,根据个人喜好放置
include /etc/nginx/conf.d/*.conf;

配置项目的 nginx

mkdir -p /etc/nginx/conf.d
touch /etc/nginx/conf.d/laravel.conf
vim /etc/nginx/conf.d/laravel.conf

laravel.conf

server {
    listen 81; ## listen for ipv4
    root        /mnt/d/www/laravel/public/ ;
    index index.php;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

重启 nginx

nginx -s reload

查看 http://localhost:81 ,显示 laravel 初始页面即代表成功。

总结

  1. WSL 替代了虚拟机和双系统,用于开发应该是足够了,特别是子系统和主系统公用 IP 和文件夹,十分方便,免去了配置映射关系这一步

  2. WSL 目前还在完善中,听说年中要发布 WSL2 测试版,看宣传也是非常酷炫

  3. 作为 Windows 的忠实拥趸,不得不说一句:世界上最好的 Linux 桌面版 ---- Windows 10 ,那么世界上最好的语言是_____

  4. 第一次尝试写类教程文章,有不足之处,欢迎大佬指教

本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 4年前 自动加精
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 21

前武汉第一万花 的文章还是牛啤酒

4年前 评论

厉害 :+1: :+1: :+1:

4年前 评论

看起来不错呀,还没用上win10

4年前 评论

:+1:

4年前 评论

可以哟,现在已经在用了

4年前 评论
wanghan

按照博客安装好了ubuntu18,进入ubuntu链接公司的服务器,只有黑白两色,有点太难看了,怎么才能变成彩色的呢

4年前 评论
lmaster

说实话 2019 年,微软在开源的推动上做了很多事
这几年微软对开源这个态度的转变也越来越大
期待 .net 5.0 和 vs code

4年前 评论

前一段时间看到过这个,懒得去折腾就没仔细看

4年前 评论

系统重启后还是要手动开启nginx和php服务吗?

4年前 评论
sunrise丶 4年前
Zhangzhuow (作者) 2年前
抄底斗罗

世界上最好的语言是laravel :stuck_out_tongue_winking_eye:

4年前 评论

wsl中的apache和mysql会和win10内装的apache和mysql冲突吧?
wsl中的apache和mysql启动时,win10内的就得换端口才能启动了。

4年前 评论

@MuYan 跟主系统公用同一个 IP,端口肯定是冲突的

4年前 评论

是时候卸载掉虚拟机了

4年前 评论

最经也在捣鼓 wsl 但搭建环境过程中一直不顺,就暂时放弃了.
看到楼主的博文,立马开始尝试,但还是遇到安装依赖相关问题,帮忙看下下什么情况
下面是运行 apt-get install build-essential 失败截图

file

4年前 评论
VeryCool

等2出来吧!1有点残缺!

4年前 评论

目前请准备一个大一点的C盘

4年前 评论

@alaraveler 尝试更新一下 apt 的源:apt-get update,或者换阿里的源试试

4年前 评论

分享个好东西给你们,轻松配置环境 https://github.com/zoang/wsl-ubuntu

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!