Contents

WSL搭建nginx+php+mysql环境

安装:

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

初始化

  • 更换源:

查看Ubuntu版本:

1
lsb_release -a

找apt源(阿里,163等):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#阿里云源
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

切到Ubuntu

1
2
3
4
cd /etc/apt
sudo su root
cp sources.list sources.list.bak
vim sources.list # ggVG,dd,ESC,i,鼠标右键,Esc,:x,Enter
1
2
3
4
5
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
  • tips : 如果sudo apt-get update时出现以下错误
1
2
3
4
5
6
7
8
9
Get:6 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Err:3 http://mirrors.aliyun.com/ubuntu bionic InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 3B4FE6ACC0B21F32
Get:7 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease [242 kB]
Get:8 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease [74.6 kB]
Err:4 http://mirrors.aliyun.com/ubuntu bionic-security InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 3B4FE6ACC0B21F32
Err:6 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 3B4FE6ACC0B21F32

则执行 (改下key)

1
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 安装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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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

解决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” 具体方法: https://static.duan1v.top/images/lcgkAhE4DU7LypM.png 没错,只要在nginx配置中添加一句fastcgi_buffering off;

发现网站目录不是www-data的,也会很慢,不过是104,更新一下目录所属用户即可

查看已安装的wsl

1
wsl --list --verbose

关闭开启的wsl(根据上面命令获取的wsl的name)

1
wsl -t wsl-name

网站文件无法更新权限及所属用户(组)

1
2
sudo umount /mnt/d
sudo mount -t drvfs D: /mnt/d -o metadata

Mysql 初始化用户

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
➜  ~ sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = mdUacQRZSC9AfTNV
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = mdUacQRZSC9AfTNV
socket   = /var/run/mysqld/mysqld.sock


➜  ~ mysql -udebian-sys-maint -pmdUacQRZSC9AfTNV

# 创建新用户,并赋予权限
create user 'dywily'@'localhost' identified by 'q123456we';
grant all privileges on *.* to 'dywily'@'localhost';
flush privileges;
coffee