O_树莓派ZeroW与RaspAP搭建局域网服务器


O_嵌入式专题目录

系统及调试连接

本文选择系统为官方 Raspberry Pi OS Lite:
Release date: October 30th 2021
Kernel version: 5.10

系统烧录就不多说了,按照大多数嵌入式系统的烧录方法,SD Card Formatter 格式化,Win32DiskImager 烧img。


烧写完系统后在Win系统下boot分区可以直接读出,如果要用SSH连接

  • 在boot根目录新建空白文件ssh
  • 新建wpa_supplicant.conf文件,写入以下内容:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    country=CN
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1

    network={
    ssid="CMCC"
    psk="yourpasswd"
    key_mgmt=WPA-PSK
    priority=4
    }

    network={
    ssid="RPIzeroW"
    psk="zeroWzeroW"
    key_mgmt=WPA-PSK
    priority=5
    scan_ssid=1
    }
  • 注意国家参数会影响到后面RaspAP的安装,两者要一样。当然这个可以在后面树莓派配置菜单里手动再调。

(第一个为路由器信息,优先级第二,因为路由器不方便随时开关,所以这个信号一直存在,如果路由的中转有问题可以自己用手机或电脑作为热点,自己设置ssid和passwd为第二个,这样路由器连接不上SSH时重启树莓派会优先连接第二个自己建的热点,这样相当于树莓派与调试环境网络直连,就不存在转发的问题了。)

(至于为什么这样做,本人在用路由器调试的时候,树莓派的IP动态静态都设置过,路由链表也都手动设置,树莓派可以Ping百度,Win10和树莓派却相互都Ping不通,路由器上的IP也正常,而且没法设置转发,所以最后调了很久也没找到办法,不得已用上面自建热点的方式来调了。)

(安装RaspAP后相当于反过来,树莓派作为热点,调试环境连接树莓派的网络,这样SSH基本是没问题的。)


如果用串口方式调试就简单很多,在boot根目录的config.txt文件末尾添加enable_uart=1即可。

连线方式的话右边的排针引脚如下图,连接右侧自上第2-5V、3-GND、4-TXD、5-RXD,这里没其他负载的话可以直接由5V供电调试,而且注意板上与串口工具数据线交叉。

换源(bullseye)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sudo nano /etc/apt/sources.list

#deb http://raspbian.raspberrypi.org/raspbian/ bullseye main contrib non-free rpi
# Uncomment line below then 'apt-get update' to enable 'apt-get source'
#deb-src http://raspbian.raspberrypi.org/raspbian/ bullseye main contrib non-free rpi
deb http://mirrors.aliyun.com/raspbian/raspbian/ bullseye main non-free rpi
deb-src http://mirrors.aliyun.com/raspbian/raspbian/ bullseye main non-free rpi


sudo nano /etc/apt/sources.list.d/raspi.list

#deb http://archive.raspberrypi.org/debian/ bullseye main
# Uncomment line below then 'apt-get update' to enable 'apt-get source'
#deb-src http://archive.raspberrypi.org/debian/ bullseye main
deb http://mirrors.aliyun.com/raspberrypi/ bullseye main

sudo apt-get update
# 智能链接更新
sudo apt-get dist-upgrade
# 冗余更新
sudo apt-get full-upgrade

手动安装

安装必备的依赖

(这里php7.3没有可以试试7.4或5,本人的系统环境中为7.4。)

1
sudo apt-get install lighttpd git hostapd dnsmasq iptables-persistent vnstat qrencode php7.3-cgi

开启lighttpd服务

1
2
3
4
5
6
7
sudo lighttpd-enable-mod fastcgi-php

------>
Met dependency: fastcgi
Enabling fastcgi-php: ok
Enabling fastcgi: ok
Run "service lighttpd force-reload" to enable changes
1
2
sudo service lighttpd force-reload
sudo systemctl restart lighttpd.service

克隆raspap-webgui源码

根据自己网络选择克隆Github还是Gitee。

!!!如果不是全新系统,请注意原/var/www/html目录内数据的备份!!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo rm -rf /var/www/html
// 三选一
sudo git clone https://github.com/RaspAP/raspap-webgui /var/www/html
sudo git clone https://gitee.com/ursaminor68/raspap-webgui.git /var/www/html
sudo git clone https://gitee.com/AaronTYin/raspap-webgui /var/www/html

------>
Cloning into '/var/www/html'...
remote: Enumerating objects: 19377, done.
remote: Counting objects: 100% (19377/19377), done.
remote: Compressing objects: 100% (10829/10829), done.
remote: Total 19377 (delta 7163), reused 19377 (delta 7163), pack-reused 0
Receiving objects: 100% (19377/19377), 20.96 MiB | 2.46 MiB/s, done.
Resolving deltas: 100% (7163/7163), done.

RaspAP配置指令

  • 给“www-dtata”用户添加sudo的权限
1
2
cd /var/www/html
sudo cp installers/raspap.sudoers /etc/sudoers.d/090_raspap
  • 创建 RaspAP 的配置目录,添加 /etc/dhcpcd.conf 文件作为基础配置文件。
1
2
3
4
5
6
sudo mkdir /etc/raspap/
sudo mkdir /etc/raspap/backups
sudo mkdir /etc/raspap/networking
sudo mkdir /etc/raspap/hostapd
sudo mkdir /etc/raspap/lighttpd
cat /etc/dhcpcd.conf | sudo tee -a /etc/raspap/networking/defaults > /dev/null
  • 更改一些文件和目录的用户组以及权限的命令。
1
2
3
4
5
6
7
8
9
10
11
sudo cp raspap.php /etc/raspap
sudo chown -R www-data:www-data /var/www/html
sudo chown -R www-data:www-data /etc/raspap
sudo mv installers/*log.sh /etc/raspap/hostapd
sudo mv installers/service*.sh /etc/raspap/hostapd
sudo chown -c root:www-data /etc/raspap/hostapd/*.sh

------>
changed ownership of '/etc/raspap/hostapd/disablelog.sh' from www-data:www-data to root:www-data
changed ownership of '/etc/raspap/hostapd/enablelog.sh' from www-data:www-data to root:www-data
changed ownership of '/etc/raspap/hostapd/servicestart.sh' from www-data:www-data to root:www-data
1
2
3
4
5
6
sudo chmod 750 /etc/raspap/hostapd/*.sh
sudo cp installers/configport.sh /etc/raspap/lighttpd
sudo chown -c root:www-data /etc/raspap/lighttpd/*.sh

------>
changed ownership of '/etc/raspap/lighttpd/configport.sh' from root:root to root:www-data
1
2
3
4
5
6
sudo mv installers/raspapd.service /lib/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable raspapd.service

------>
Created symlink /etc/systemd/system/multi-user.target.wants/raspapd.service → /lib/systemd/system/raspapd.service.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo mv /etc/default/hostapd ~/default_hostapd.old
sudo cp /etc/hostapd/hostapd.conf ~/hostapd.conf.old
sudo cp config/default_hostapd /etc/default/hostapd
sudo cp config/hostapd.conf /etc/hostapd/hostapd.conf
sudo cp config/dnsmasq.conf /etc/dnsmasq.d/090_raspap.conf
sudo cp config/dhcpcd.conf /etc/dhcpcd.conf
sudo cp config/config.php /var/www/html/includes/
sudo systemctl stop systemd-networkd
sudo systemctl disable systemd-networkd
sudo cp config/raspap-bridge-br0.netdev /etc/systemd/network/raspap-bridge-br0.netdev
sudo cp config/raspap-br0-member-eth0.network /etc/systemd/network/raspap-br0-member-eth0.network

echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/90_raspap.conf > /dev/null
sudo sysctl -p /etc/sysctl.d/90_raspap.conf
sudo /etc/init.d/procps restart

------>
[ ok ] Restarting procps (via systemctl): procps.service.
1
sudo iptables -t nat -A POSTROUTING -j MASQUERADE

如果执行完后看到如下的错误提示:

1
iptables/1.8.2 Failed to initialize nft: Protocol not supported

把树莓派重启sudo reboot,然后重新执行:

1
sudo iptables -t nat -A POSTROUTING -j MASQUERADE

发现没有出错了,就可以继续执行下面的指令了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sudo iptables -t nat -A POSTROUTING -s 192.168.50.0/24 ! -d 192.168.50.0/24 -j MASQUERADE
sudo iptables-save | sudo tee /etc/iptables/rules.v4

------>
# Generated by xtables-save v1.8.2 on Sat Nov 21 22:43:02 2020
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT
# Completed on Sat Nov 21 22:43:02 2020
# Generated by xtables-save v1.8.2 on Sat Nov 21 22:43:02 2020
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A POSTROUTING -j MASQUERADE
-A POSTROUTING -s 192.168.50.0/24 ! -d 192.168.50.0/24 -j MASQUERADE
COMMIT
# Completed on Sat Nov 21 22:43:02 2020

1
2
3
4
5
6
sudo systemctl unmask hostapd.service
sudo systemctl enable hostapd.service

------>
Synchronizing state of hostapd.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable hostapd
  • 最后重启sudo reboot

用调试环境在无线信号中找到 “raspi-webgui” 后,点击连接,输入初始密码 “ChangeMe” 之后,就可以在浏览器中输入地址:http://10.3.14.1 ,用户名为 admin ,密码是 secret ,进入RaspAP主页面。热点的SSID和PASSWD推荐自行更改。以后SSH可以通过 10.3.141.1 这个地址来登录树莓派操作,不需要连接路由器,不需要连接外网(外网之后尝试外加个网卡桥接试试)。

IP 地址: 10.3.141.1
登录用户名: admin
登录密码: secret
DHCP 范围: 10.3.141.50 至 10.3.141.255
SSID: raspi-webgui
WiFi 密码: ChangeMe

 

本节内容转自:把树莓派变身无线AP - ursamjnor,已验证成功。
版权声明:本文为CSDN博主「ursamjnor」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ursamjnor/article/details/109952559

上传博客文件

  • 连接树莓派热点,目前系统默认开启SFTP,输入树莓派IP及登录名,直接上传到/var/www/html/文件夹下没权限的话可以先放到home里的登录用户下,然后用命令cpmv -R/var/www/html/目录,然后加上www-data用户权限chmod -R www-data:www-data /var/www/html/yourdir

  • 修改/var/www/html/下的index.php,按自己喜好加上跳转博客的连接,或者自己手动重写index.php。

    1
    2
    3
    4
    5
    <!-- 在RaspAP主页面左侧边栏第一行添加跳转 -->
    <!-- Line131 -->
    <li class="nav-item">
    <a class="nav-link" href="MZtop/MZtop.html"><i class="fas fa-info-circle fa-fw mr-2"></i><span class="nav-label"><?php echo _("mengze.top"); ?></a>
    </li>

其他系统相关

外部开关机

  • 在boot根目录的config.txt文件末尾添加以下代码实现外部按键开关机。实测的话开机峰值200mA,关机30mA,不过因为手头的测量设备都是A级别的,测小电流不是很精准。所以关机灯灭后最好还是断一下电,加个手动开关或另起一个开关检测控制电路都可以。(有时间可以做一块带标准18650UPS、开关机检测及电源控制、电量耗电指示、屏幕、USB转接、光伏备用充电、RTC、NTC、远程开关机的集成底板。)
    1
    2
    # 使用GPIO3(这里的GPIO指3BCM GPIO编号,也就是SCL1)作为电源键,下降沿为按下,上升沿为弹起,内部上拉
    dtoverlay=gpio-shutdown

如果要调整IO口可参照 优雅地给树莓派添加开、关机键 - 小小の蜗牛

修改IP

修改/etc/dhcpcd.conf文件sudo nano /etc/dhcpcd.conf(Ctrl+X退出,Y回车保存):

在文件结尾加入以下内容:

1
2
3
4
interface wlan0  #指定接口
static ip_address=192.168.1.8/24 #IP根据需要更改,/24的意思是子网掩码为 255.255.255.0
static routers=192.168.1.1 #网关
static domain_name_servers=192.168.1.1 10.18.0.1 # DNS

修改的IP必须和路由器在同一网段,且网关相同;手动静态 IP 不能跟路由器 DHCP 所自动分配的 IP 冲突,否则树莓派就有可能无法正常联网。可以用ip route show显示路由链表信息。

  • 有线网卡:

    1
    2
    3
    interface eth0
    static ip_address=192.168.1.8/24
    static domain_name_servers=114.114.114.114
  • 无线网卡:

    1
    2
    3
    4
    interface wlan0
    static ip_address=192.168.1.9/24
    static routers=192.168.1.1
    static domain_name_servers=114.114.114.114

添加中文环境

可以在RaspAP的选项界面配置为中文后以中文显示界面。

  • sudo raspi-config 选择 5 Localisation Options ,回车。
  • 选择 L1 Locale ,回车。
  • 通过键盘方向键或者鼠标滚动,找到 zh_CN.UTF-8 UTF-8 ,然后按空格键进行选中,选中效果是会出现一个 “*” 号。完成选择后回车。
  • 选择 en_US.UTF-8 为当前默认环境配置,之前的 zh_CN.UTF-8 UTF-8 在配置完成后Finish会添加在环境里,首次配置会提示重启。

修改vsftpd的默认根目录

用于vsftpd的连接上传,默认SFTP不需要。

修改ftp的根目录只要修改/etc/vsftpd/vsftpd.conf文件即可:

1
2
3
4
local_root=/var/www/html
chroot_local_user=YES
anon_root=/var/www/html
allow_writeable_chroot=YES

注:local_root 针对系统用户;anon_root 针对匿名用户。

重新启动服务:

1
service vsftpd restart

任何一个用户ftp登录到这个服务器上都会chroot到/var/www/html目录下。

ip命令

ip命令和ifconfig命令一样,但是功能更加强大,并旨在取代后者。CentOS 7中默认使用ip命令,不在安装ifconfig。ifconfig命令属于net-tools套件,而ip命令属于iproute套件。

1.设置、查看和删除IP地址:

设置IP地址
ip addr add 192.168.1.1/24 dev eth0

查看IP地址
ip addr show eth0

删除IP地址
ip addr del 192.168.1.1 dev eth0

2.修改路由:

查看路由表
ip route show

查看路由包来自的接口(本地接口)
ip route get 123.125.114.144

更改默认路由
ip route add default via 192.168.1.254

3.显示网络信息:

显示网络统计信息
ip -s link

查看ARP条目
ip neigh(或neighbour)

监控netlink消息
ip monitor all

4.激活或停止网络接口:

激活网络接口
ip link set eth0 up

停止网络接口
ip link set eth0 down