树莓派 Raspberry Pi 3B+ 无线路由器, WEB 服务器(Nginx,PHP,Sqlite3),UART 串口数据采集

目标: 测试四核 A53 树莓派 Raspberry Pi 3B+ 无线路由器, 轻量级WEB 服务器(Nginx,PHP,Sqlite3),UART 串口数据采集性能


初始配置,部分选项需要sudo reboot 重启后生效

开启 ssh 功能等

$ sudo raspi-config

更换为国内软件源,以加快软件更新和安装速度:

$sudo vi /etc/apt/sources.list

#添加内容
deb http://mirrors.aliyun.com/raspbian/raspbian/ stretch main contrib non-free rpi  

系统更新及软件升级

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get dist-upgrade
$ sudo rpi-update

安装输入法

$sudo apt install fcitx

无线路由器软件安装

(hostapd: 软件能使无线网卡工作在软AP(Access Point)模式,即无线路由器,dnsmasq 软件提供DHCP和DNS服务, pppoeconf 网络拔号软件)

$ sudo apt-get install dnsmasq hostapd pppoeconf

raspbian 系统中默认使用 dhcpd 来配置网络,因 wlan0 工作在AP模式,所以需要设为静态IP地址,故在配置文件 /etc/dhcpcd.conf中禁用wlan0.

$sudo vi /etc/dhcpcd.conf

#内容
#interface eth0
#fallback static_eth0

denyinterfaces wlan0

在 /etc/network/interfaces 设置网卡的静态IP地址

$sudo vi /etc/network/interfaces

source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.0.250
netmask 255.255.255.0
gateway 192.168.0.1

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.10.1
netmask 255.255.255.0 

hostapd 配置文件

$sudo vi /etc/hostapd/hostapd.conf

#内容

# This is the name of the WiFi interface we configured above
interface=wlan0
# Use the nl80211 driver with the brcmfmac driver
driver=nl80211
# This is the name of the network
ssid=RPI_3B_Router
# Use the 2.4GHz band
hw_mode=g
# Use channel 6
channel=6
# Enable 802.11n
ieee80211n=1
# Enable WMM
wmm_enabled=1
# Enable 40MHz channels with 20ns guard interval
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
# Accept all MAC addresses
macaddr_acl=0
# Use WPA authentication
auth_algs=1
# Require clients to know the network name
ignore_broadcast_ssid=0
# Use WPA2
wpa=2
# Use a pre-shared key
wpa_key_mgmt=WPA-PSK
# The network passphrase
wpa_passphrase=raspberry
# Use AES, instead of TKIP
rsn_pairwise=CCMP 

hostapd 配置文件启动设置

$sudo vim /etc/default/hostapd

// 找到“#DAEMON_CONF=”,去掉#注释,补全为
DAEMON_CONF="/etc/hostapd/hostapd.conf"

完成配置后,检查是否配置成功

$sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

如果最后两行出现了如下,则为正常
wlan0: interface state UNINITIALIZED->ENABLE
wlan0: AP-ENABLED

重启hostapd

$sudo service hostapd restart

如启动不成功的处理办法

$sudo systemctl disable dhcpcd
$sudo systemctl enable networking
$sudo reboot

$sudo hostapd -B /etc/hostapd/hostapd.conf #开机后再次启动hostapd服务

dnsmasq 配置

$sudo vim /etc/dnsmasq.conf

#内容
interface=wlan0 # Use interface wlan0
listen-address=192.168.10.1 # Explicitly specify the address to listen on
bind-interfaces # Bind to the interface to make sure we aren't sending things elsewhere
server=8.8.8.8 # Forward DNS requests to Google DNS
domain-needed # Don't forward short names
bogus-priv # Never forward addresses in the non-routed address spaces.
dhcp-range=192.168.10.100,192.168.10.200,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time 

重启 dnsmasq 服务

$sudo service dnsmasq restart

开启Linux内核的IP转发以及使用iptables做NAT表,让无线网卡的数据通过有线网卡转发出去

开启Linux内核的IP转发功能

$sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

开启树莓派有线网卡和无线网卡的转发功能 (手动命令行开启,如不保存,重新开机则会失效)

$sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
$sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

保存当前的防火墙策略到配置文件中

$sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

修改系统启动脚本,添加启动任务

$sudo vim /etc/rc.local

#新增内容
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
iptables-restore < /etc/iptables.ipv4.nat

exit 0 

dnsmasq经常会出现问题,原因是wlan0还没热启动完成,而dnsmasq先启动了,所以出现了启动失败的现象。 
这里的解决方案就是采用延时启动

在 /etc/rc.local 文件中,  exit 0 之前加入以下命令

$sudo bash /etc/dnsmasq_delayinit.sh

建立文件 dnsmasq_delayinit.sh

$sudo vim /etc/dnsmasq_delayinit.sh

#内容
#!/bin/sh
sleep 10
sudo service dnsmasq restart

把 dnsmasq_delayinit.sh 设置成可执行

$sudo chmod +x /etc/dnsmasq_delayinit.sh

待继续测试和完善....

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章