wsl里面使用宝塔

臭大佬 2022-10-07 10:31:39 1787
linux 
简介 wsl里面使用宝塔

linux 一键启动

由于在 Wsl2 中没有权限操作 Win10 的 hosts 文件,所以我们需要先解决该问题。

获取hosts文件权限

打开C:\Windows\System32\drivers\etc文件夹找到hosts文件,
右键->属性->安全->编辑->组或用户名中选中Users->完全控制打钩->应用->确定

如果经常操作host文件,可以发送到桌面快捷方式:

权限开启后,我们就可以修改hosts文件了。

脚本

设置默认root登录

cmd 命令行下,设置默认root登录,ubuntu+tab键就会出来ubuntuxxx.exe

ubuntuxxx.exe config --default-user root

cmd 中输入bash进入 wsl ,进入用户目录 cd ~ ,编辑init.sh文件 vim init.sh

#/bin/bash
echo "开启wsl及其相关启动项:bt、nginx、mysqld、redis、memcached、php、ftp、docker"
echo "bt 启动:"
/etc/init.d/bt start
echo "nginx 启动:"
/etc/init.d/nginx start
echo "mysqld 启动:"
/etc/init.d/mysqld start
echo "redis 启动:"
/etc/init.d/redis start
echo "memcached 启动:"
/etc/init.d/memcached start
echo "php 启动:"
/etc/init.d/php-fpm-72 start
echo "ftp 启动:"
/etc/init.d/pure-ftpd start
echo "docker 启动:"
service docker start
echo "启动完成!"
echo ""
# 为 win 设置 wsl host
echo "为 win 设置 wsl host"
# win hosts 文件路径
win_hosts_path="/mnt/c/Windows/System32/drivers/etc/hosts"
# 为 wsl2 设置的域名
echo "为 wsl2 设置的域名:dev.wsl.net"
wsl_domain="dev.wsl.net"
# 获取 wsl2 的 ip
wsl_ip=$(ifconfig eth0 | grep -w inet | awk '{print $2}')
echo "wsl2的ip:$wsl_ip"
# 判断是否已存在 wsl2 的域名,如果存在则修改,否则追加
if grep -wq "$wsl_domain" $win_hosts_path
then
    # 此处因为权限问题没有直接用 sed 修改 hosts 文件
    win_hosts=$(sed -s "s/.* $wsl_domain/$wsl_ip $wsl_domain/g" $win_hosts_path)
    echo "$win_hosts" > $win_hosts_path
else
    echo "$wsl_ip $wsl_domain" >> $win_hosts_path
fi

# 为 wsl 设置 win host
wsl_hosts_path="/etc/hosts"
win_domain="dev.win.net"
win_ip=$(cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}')
if grep -wq "$win_domain" $wsl_hosts_path
then
    wsl_hosts=$(sed -s "s/.* $win_domain/$win_ip $win_domain/g" $wsl_hosts_path)
    echo $wsl_hosts > $wsl_hosts_path
else
    echo "$win_ip $win_domain" >> $wsl_hosts_path
fi

给权限

chmod +x init.sh

执行

sh init.sh

站点绑定

获取 wslip

ip addr | grep eth0 或者 ip addr 查看 ip

wsl访问主机的服务

wsl里面执行如下命令:

 cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }'

出来的ip就是主机ip

win下的host进行绑定域名,例如:

# host 文件添加
192.168.0.104  dev.host.net

建立软连接,连接本地文件夹

ln -s 源文件 目标文件
# 移除软连接
rm -rf 目标文件

我们把wsl中的/www/wwwroot目录印射到本地的d/wwwroot,在wsl中,/mnt/d/wwwroot 指的是本地d盘下的wwwroot,
建立连接之前先把目标文件(/www/wwwroot)的wwwroot文件删除,要不就会在他里面创建,

ln -s /mnt/d/wwwroot /www/wwwroot

问题

WSL中宝塔数据库无法使用

https://www.bt.cn/bbs/forum.php?mod=viewthread&tid=65196&highlight=wsl

解决 Win10 Wsl2 IP 变化问题

其实就是一键启动的步骤,给winhost文件权限,然后设置域名,init.sh文件的简版wsl.sh

#!/bin/bash
# 为 win 设置 wsl host
echo "为 win 设置 wsl host"
# win hosts 文件路径
win_hosts_path="/mnt/c/Windows/System32/drivers/etc/hosts"
# 为 wsl2 设置的域名
echo "为 wsl2 设置的域名:dev.wsl.net"
wsl_domain="dev.wsl.net"
# 获取 wsl2 的 ip
wsl_ip=$(ifconfig eth0 | grep -w inet | awk '{print $2}')
echo "wsl2的ip:$wsl_ip"
# 判断是否已存在 wsl2 的域名,如果存在则修改,否则追加
if grep -wq "$wsl_domain" $win_hosts_path
then
    # 此处因为权限问题没有直接用 sed 修改 hosts 文件
    win_hosts=$(sed -s "s/.* $wsl_domain/$wsl_ip $wsl_domain/g" $win_hosts_path)
    echo "$win_hosts" > $win_hosts_path
else
    echo "$wsl_ip $wsl_domain" >> $win_hosts_path
fi

# 为 wsl 设置 win host
wsl_hosts_path="/etc/hosts"
win_domain="dev.win.net"
win_ip=$(cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}')
if grep -wq "$win_domain" $wsl_hosts_path
then
    wsl_hosts=$(sed -s "s/.* $win_domain/$win_ip $win_domain/g" $wsl_hosts_path)
    echo $wsl_hosts > $wsl_hosts_path
else
    echo "$win_ip $win_domain" >> $wsl_hosts_path
fi

解决php项目域名绑定

当然,如果是像php项目,需要在host文件做域名和ip的绑定,当wsl系统的ip变化时,我们可以把常用的域名绑定的ip一一替换,可以在上面脚本的基础上做个升级:
wsl.sh文件

#!/bin/bash
# 为 win 设置 wsl host
echo "为 win 设置 wsl host"
# win hosts 文件路径
win_hosts_path="/mnt/c/Windows/System32/drivers/etc/hosts"
# 为 wsl2 设置的域名
echo "为 wsl2 设置的域名:dev.wsl.net"
wsl_domain="dev.wsl.net"
# 获取 wsl2 的 ip
wsl_ip=$(ifconfig eth0 | grep -w inet | awk '{print $2}')
echo "wsl2的ip:$wsl_ip"
# 判断是否已存在 wsl2 的域名,如果存在则修改,否则追加
if grep -wq "$wsl_domain" $win_hosts_path
then
    # 此处因为权限问题没有直接用 sed 修改 hosts 文件
    win_hosts=$(sed -s "s/.* $wsl_domain/$wsl_ip $wsl_domain/g" $win_hosts_path)
    echo "$win_hosts" > $win_hosts_path
else
    echo "$wsl_ip $wsl_domain" >> $win_hosts_path
fi

#-----------------
# 需要替换的数组域名
domainArr=(dev.host.net dev.guo.net xm.guo.net bs.guo.net xm.yougou.net bs.yougou.net api.yougou.net dev.blog.net)
# 循环
for domain in ${domainArr[@]};
do
    # 判断是否已存在 wsl2 的域名,如果存在则修改,否则追加
    if grep -wq "$domain" $win_hosts_path
    then
        # 此处因为权限问题没有直接用 sed 修改 hosts 文件
        win_hosts=$(sed -s "s/.* $domain/$wsl_ip $domain/g" $win_hosts_path)
        echo "$win_hosts" > $win_hosts_path
    else
        echo "$wsl_ip $domain" >> $win_hosts_path
    fi
done
#-----------------

# 为 wsl 设置 win host
wsl_hosts_path="/etc/hosts"
win_domain="dev.win.net"
win_ip=$(cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}')
if grep -wq "$win_domain" $wsl_hosts_path
then
    wsl_hosts=$(sed -s "s/.* $win_domain/$win_ip $win_domain/g" $wsl_hosts_path)
    echo $wsl_hosts > $wsl_hosts_path
else
    echo "$win_ip $win_domain" >> $wsl_hosts_path
fi

宝塔 mysql 外网 root 无法连接

宝塔 mysql 外网 root 无法连接

通过远程ip连接wsl中的服务

在wsl子系统中,使用以下命令,获取wsl的ip

ip addr | grep eth0

在windows中,用管理员方式打开powershell,输入命令,这里我的wsl的ip为172.23.186.3,要启动服务的端口为30000,因此命令如下

netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=30000 connectaddress=172.23.186.3 connectport=30000

记得在使用的时候,替换 connectaddress 和 listenport、connectport 为你需要的值。

connectaddress:wsl的ip
connectport:wsl的端口
listenport:win端口

nginx + php-fpm 打开速度缓慢

找到 nginx.conf,配置如下:


http {
    fastcgi_buffering off;
    // 其他代码
}