wsl里面使用宝塔

linux 一键启动

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

获取hosts文件权限

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

889fd7d33857b65b879510042ff4975e.png

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

权限开启后,我们就可以修改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

站点绑定

获取 wsl 的 ip

ip addr | grep eth0 或者 ip addr 查看 ip
4df6abe022929e9286ea6e241f7f3746.png

wsl访问主机的服务

wsl里面执行如下命令:

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

出来的ip就是主机ip
40dd14105bce44f0fee1bc52061c5d4a.png

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

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

0013d92007b898cf6f3a4b91187ea2bc.png

92a13400fa4ea5af38c2a79b1db2786b.png

2ba09294c78444d4dbdc74e9215adb64.png

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

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

4c7be76ff30a7d51ad5b3db49ed00bc9.png

问题

WSL中宝塔数据库无法使用

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

解决 Win10 Wsl2 IP 变化问题

其实就是一键启动的步骤,给win的 host文件权限,然后设置域名,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;
    // 其他代码
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Galloping-Vijay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值