您的位置:首页 > 运维架构 > Shell

WSL(Windows Subsystem for Linux, Bash On Windows)使用笔记

2016-12-07 14:29 1186 查看

1. 下载安装

ubuntu子系统是从windows应用商店下载的,由于网络等诸多原因可能无法顺利下载。此时解决的思路是,下载离线的安装包trusty-server-cloudimg-amd64-root.tar.gz,然后使用fiddler的autoresponse功能,把离线包作为http下载的返回。具体参见:http://jingyan.baidu.com/article/bea41d4386bbf2b4c51be6bc.html

2. Nginx无法启动的问题

apt install nginx
service nginx start


发现/var/log/nginx/error.log中有如下错误

2016/12/07 11:09:29 [emerg] 3565#0: bind() to [::]:80 failed (98: Address already in use)


看起来是80端口被占用了,使用netstat -ant| grep 80,发现这个命令在wsl下无法使用。wsl和windows外部的端口是共用的,在windows下 netstat -ano| findstr “80”,发现没有进程占用80端口。

事实上仔细看下上面的地址[::]:80,这是一个ipv6地址。因此解决的最简单办法是将/etc/nginx/sites-available/default中的ipv6禁用。

default_server ipv6only=on;


再次启动nginx,log中报错

[alert] 3807#0: ioctl(FIOASYNC) failed while spawning "worker process" (22: Invalid argument)


关闭nginx的master进程模式即可,这部分内容参考(http://simohayha.iteye.com/blog/467940 )在/etc/nginx/nignx.conf中,

master_process off;#新增一句
worker_processes 4;#之前就有的


详细的讨论看这里(https://github.com/Microsoft/BashOnWindows/issues/68

如果在windows CMD下运行
netstat -ano | findstr :80
发现80端口被占用,则结束对应进程。如果是系统进程占用,则可能是windows自带的http service占用,使用管理员权限的CMD运行
net stop http
,具体参见这里: http://flummox-engineering.blogspot.com/2015/08/apache-cannot-run-on-port-80-on-windows.html

3. 服务自启动

根据微软的说法,现在是没有办法实现bash服务自启动的。不过你可以使用bash -c命令执行一个脚本,在脚本中执行各种服务的启动。相当于对bash命令使用环境运行变量。

auto_init.sh脚本如下:

#!/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 php5-fpm start > /dev/null && echo 'Php5 fpm Started'
$SHELL


注意最后的$SHELL不要漏掉,不然运行完就关闭了。然后启动bash

bash  -c 'bash path/path/to/your/script.sh'


4.php-fpm配置

启用php支持,首先要安装php-fpm

apt install -y php5-fpm


nginx新站点的配置中,php-fpm不能使用unix socket,只可以监听tcp端口:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}


由于监听的是tcp的端口,这里php-fpm的配置也要做相应的更改,/ect/php5/fpm/pool.d/www.conf:

;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000


5.启用ssh

apt install openssh-server


然后编辑ssh配置文件, vi /etc/ssh/sshd_config,

ListenAddress 0.0.0.0
UsePrivilegeSeparation no
PasswordAuthentication yes
PermitRootLogin yes
#StrictModes yes


如果提示“sshd error: could not load host key”,则用下面的命令重新生成,或者参考这里[]https://linux.cn/article-4226-1.html]

rm  /etc/ssh/ssh*key
dpkg-reconfigure openssh-server


6.PHPstorm Terminal集成

phpstorm默认的shell path是cmd.exe,直接换成bash.exe,不可用。并且在terminal里运行bash也找不到。原因是phpstorm只识别64位的目录,而bash是32位的,因此使用如下命令重定向到32位的bash即可。

%windir%\sysnative\bash.exe


目前没有好的办法自动化初始化到bash,只能在terminal里手动运行以上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  windows ubuntu WSL