树莓派以价格低、功耗低、功能强大著称,本文介绍一种利用树莓派搭建自己的服务器的方法。
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)