美文网首页
WSL搭建nginx+php+mysql环境

WSL搭建nginx+php+mysql环境

作者: 段义纬 | 来源:发表于2019-03-04 11:31 被阅读0次

    1、安装:(参考:https://www.cnblogs.com/JettTang/p/8186315.html

    • 管理员权限运行powershell并运行下面的命令:
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
    
    • Microsoft Store下载Linux,我用的是Ubuntu,安装完成后可在开始菜单里找到快捷方式并启动,第一次运行需要等待安装并设置用户名、密码。

    2、初始化

    • 更换源:

    查看Ubuntu版本:

    lsb_release -a
    

    浏览器访问:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/,找到对应版本的源并复制,18.04的源:

    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    
    # 预发布软件源,不建议启用
    # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
    

    切到Ubuntu

    cd /etc/apt
    sudo su root
    cp sources.list sources.list.bak
    vim sources.list # ggVG,dd,ESC,i,鼠标右键,Esc,:x,Enter
    

    最好重启下电脑:

    sudo apt-get update
    sudo apt install git nginx mysql-server redis-server
    sudo add-apt-repository -y ppa:ondrej/php
    sudo apt-get update -y
    sudo apt install php7.1 php7.1-fpm php7.1-mbstring php7.1-xml php7.1-json php7.1-mysql php7.1-curl php7.1-zip php7.1-dev php7.1-gd php7.1-bcmath
    
    # 安装composer
    kael@PC-201902071414:~$ mkdir /mnt/d/Ubuntu
    kael@PC-201902071414:~$ cd /mnt/d/Ubuntu/
    kael@PC-201902071414:/mnt/d/Ubuntu$ wget https://getcomposer.org/composer.phar
    --2019-02-10 13:30:47--  https://getcomposer.org/composer.phar
    Resolving getcomposer.org (getcomposer.org)... 54.36.53.46, 2001:41d0:302:1100::8:104f
    Connecting to getcomposer.org (getcomposer.org)|54.36.53.46|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 1912030 (1.8M) [application/octet-stream]
    Saving to: ‘composer.phar’
    
    composer.phar                 100%[=================================================>]   1.82M  29.7KB/s    in 76s
    
    2019-02-10 13:32:08 (24.5 KB/s) - ‘composer.phar’ saved [1912030/1912030]
    
    kael@PC-201902071414:/mnt/d/Ubuntu$ mv composer.phar composer
    kael@PC-201902071414:/mnt/d/Ubuntu$ chmod +x composer
    kael@PC-201902071414:/mnt/d/Ubuntu$ sudo mv composer /usr/local/bin
    [sudo] password for kael:
    kael@PC-201902071414:/mnt/d/Ubuntu$ composer
    kael@PC-201902071414:/mnt/d/Ubuntu$ sudo composer global self-update
    kael@PC-201902071414:/mnt/d/Ubuntu$ composer config -g repo.packagist composer https://packagist.laravel-china.org # 或者:composer config -g repo.packagist composer https://packagist.phpcomposer.com
    

    3、自启动服务(参考:https://blog.csdn.net/donglynn/article/details/53505495)

    kael@PC-201902071414:~$ vim auto_init.sh # 内容如下,需要修改:PW的值,php版本
    #!/bin/sh
    PW=your-password 
    echo 'Starting nginx'
    echo $PW | sudo -S service nginx start > /dev/null && echo 'Nginx Started'
    echo 'Starting mysql'
    echo $PW | sudo -S service mysql start > /dev/null && echo 'Mysql Started'
    echo 'Starting fpm'
    echo $PW | sudo -S service php7.1-fpm start > /dev/null && echo 'Php7.1 fpm Started'
    echo 'Starting redis'
    echo $PW | sudo -S service redis-server start > /dev/null && echo 'Redis-server Started'
    $SHELL
    kael@PC-201902071414:~$ bash  -c 'bash auto_init.sh'
    Starting nginx
    [sudo] password for kael: Nginx Started
    starting Mysql
    Mysql Started
    starting fpm
    Php7.1 fpm Started
    

    4、解决nginx编译php巨慢的方法:
    查看错误日志(应该是/var/log/nginx/error.log)如下:
    upstream timed out (110: Connection timed out) while reading upstream, client: 127.0.0.1, server: foo.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "foo.com"
    具体方法:

    image.png
    没错,只要在nginx配置中添加一句fastcgi_buffering off;

    相关文章

      网友评论

          本文标题:WSL搭建nginx+php+mysql环境

          本文链接:https://www.haomeiwen.com/subject/osnteqtx.html