樹莓派 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

待繼續測試和完善....

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