樹莓派以價格低、功耗低、功能強大著稱,本文介紹一種利用樹莓派搭建自己的服務器的方法。
1. 燒錄系統及設置
所用到的軟件
- DiskGenius
- Win32DiskImager
- PuTTY
在樹莓派官網上下載對應版本的系統鏡像,利用DiskGenius軟件對SD卡進行重置及格式化,再將img鏡像文件寫入SD卡中,這里用的Win32DiskImager。寫入成功后,SD卡將顯示只有40幾M,因為Linux下的分區格式,Windows讀取不到,正常。
寫入成功后,先不急於拔出讀卡器,進入boot盤符,根目錄下新建一個無后綴名空文件ssh,同時新建一個文件wpa_supplicant.conf來配置Wifi,以獲取IP地址來使用SSH連接。將下列代碼寫入該文件:
country=CN ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="wifi名字" psk="wifi密碼" }
配置好后,將SD卡插入樹莓派,連接電源開機。筆記本進入路由器后台(一般為192.168.1.1),觀察raspberrypi上線,記錄IP地址。
打開PuTTY,輸入IP地址,SSH連接樹莓派,賬號:pi,密碼:raspberry(passwd來修改密碼)。
接下來就是對樹莓派系統的設置。
1. 修改源
修改sources.list
sudo nano /etc/apt/sources.list deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi #deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
ctl+x 保存退出
修改raspi.list
sudo nano /etc/apt/sources.list.d/raspi.list deb http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui #deb-src http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui
更新軟件源
sudo apt-get update
2. 修改時區
sudo dpkg-reconfigure tzdata
Asia --> Shanghai
3. 開機自啟SSH
a. interface option中enable SSH
sudo raspi-config
b. 開機自啟
打開/etc/rc.local文件,在exit 0之前加入
/etc/init.d/ssh start
2. 安裝Nginx
#安裝 sudo apt-get install nginx #啟動 sudo /etc/init.d/nginx start #重啟 sudo /etc/init.d/nginx restart #停止 sudo /etc/init.d/nginx stop
Linux關機命令
# 關機 halt poweroff shutdown -h now showdown -h 10 #(delay 10 mins) # 重啟 reboot shutdown -r now shutdown -r 10 shutdown -r 20:35 # 定時reboot shutdown -c # 取消重啟
3. 內網穿透
暫空(2020-2-25)
4. Linux文件目錄說明