菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
1951
21

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

原创
05/13 14:22
阅读数 1348

最好用的 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. 第一次尝试写类教程文章,有不足之处,欢迎大佬指教

发表评论

0/200
1951 点赞
21 评论
收藏
为你推荐 换一批