美文网首页电子电脑技术大数据AI
从零开始的树莓派搭建NAS

从零开始的树莓派搭建NAS

作者: 流星_e668 | 来源:发表于2019-08-27 15:59 被阅读0次

原文我在微信公众号写的,现在搬过来让更多人看到

可以支持下我的网站吗

纸盒子工具箱


起因:手机空间不够

经过:在搭建环境时抓狂

结果:放弃

哈哈哈逗你玩的

那么接下来进入正文~

首先给大家解释一下NAS是什么(毕竟看我文章的又不是什么技术大牛)

NAS(Network Attached Storage:网络附属存储)按字面简单说就是连接在网络上,具备资料存储功能的装置,因此也称为“网络存储器”。它是一种专用数据存储服务器。它以数据为中心,将存储设备与服务器彻底分离,集中管理数据,从而释放带宽、提高性能、降低总拥有成本、保护投资。其成本远远低于使用服务器存储,而效率却远远高于后者。目前国际著名的NAS企业有Netapp、EMC、OUO等

复制百度百科的hhh

NAS就是私有云,公有云有百度网盘,腾讯微云等,私有云纯粹就是自己用的。

我主要是因为手机空间不足,拍完的照片必须马上移动到别的地方,还有我拿不住手机的特性,一不小心就可能让它壮烈殉职。。。

正好家里有一台在角落吃灰的树莓派,那么就来动工吧!

1.将树莓派挂到路由器旁边

因为我的树莓派已经有系统了,所以在这里不说刷入系统的步骤。

找到一根网线,一个充电头,一个充电线,一个U盘(其实这里应该是移动硬盘,但是我没有)

然后把网线连到树莓派的网口和路由器的LAN口,接上电源。

第一步就完成了!

2.选网盘软件

现在在Linux上的网盘大概有owncloud,nextcloud,seafile

nextcloud是owncloud原团队再做的

所以它们俩选一个就可以了。

我看了这个文章

Seafile和Nextcloud相比较哪个好用

http://www.yizu.org/archives/690/

根据我的需求类比再三,最终选择了seafile

这是噩梦的开始 我开始高兴的下载seafile系统

3.搭建Seafile环境

来到seafile的官网

https://www.seafile.com/home/

嗯虽然是有付费的企业版,但也有免费的个人版。

点击下载

在服务器端选择Raspberry PI(树莓派)的Latest version

到它的Github界面

可以看到它现在已经更新到了7.0.4

那么就下载

seafile-server_7.0.4_stable_pi.tar.gz

然后想办法把它传到树莓派的U盘里

(U盘必须是EXT4,不然安装不了)

我把这个文件放到服务器上然后用Curl传到树莓派的

接下来连接树莓派的SSH

首先确保树莓派的SSH开启(新树莓派系统boot下放一个名字叫ssh的空文件)

接下来打开路由器的控制界面

找到raspberrypi,看它的IP地址

这里是192.168.31.211

那么在putty输入这个IP,就能访问树莓派了,树莓派初始用户名和密码是pi,raspberry

先输入sudo -s变成管理员

接下来开始挂载U盘

root@raspberrypi:~# fdisk -lDisk/dev/ram0: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram1: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram2: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram3: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram4: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram5: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram6: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram7: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram8: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram9: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram10: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram11: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram12: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram13: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram14: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/ram15: 4 MiB, 4194304 bytes, 8192 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 4096 bytesI/Osize (minimum/optimal): 4096 bytes / 4096 bytesDisk/dev/mmcblk0: 7.4 GiB, 7901020160 bytes, 15431680 sectorsUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 512 bytesI/Osize (minimum/optimal): 512 bytes / 512 bytesDisklabeltype: dosDiskidentifier: 0x72b43211DeviceBoot  Start      End  Sectors  Size Id Type/dev/mmcblk0p18192  532480  524289  256M  c W95 FAT32 (LBA)/dev/mmcblk0p2540672 15431679 14891008  7.1G 83 LinuxDisk/dev/sda: 60 GiB, 64382566400 bytes, 125747200 sectorsDiskmodel: Flash DiskUnits:sectors of 1 * 512 = 512 bytesSectorsize (logical/physical): 512 bytes / 512 bytesI/Osize (minimum/optimal): 512 bytes / 512 bytesDisklabeltype: dosDiskidentifier: 0xcfd906efDeviceBoot Start      End  Sectors Size Id Type/dev/sda1  *     2048 125747199 125745152  60G  c W95 FAT32 (LBA)root@raspberrypi:~# mkdir /heheroot@raspberrypi:~# mount /dev/sda1 /hehe

具体的挂载过程不再阐述,自己百度

然后将seafile搬到U盘并解压

cd /heheweget http://filedrop.boxpaper.club/seafile-server_7.0.4_stable_pi.tar.gztar -xzvf seafile-server_7.0.4_stable_pi.tar.gzcd seafile-server-7.0.4

先安装点必要的库

apt-getupdateapt-getinstallpython2.7libpython2.7python-setuptools python-ldap python-urllib3 sqlite3 python-requests

接着去运行安装程序

./setup-seafile.sh

服务器名随便填

域名就填0.0.0.0

保存位置不变(/hehe/seafile-data)

端口不变

然后一路Enter

(抱歉这里没有截图)

那就看看官方给的配置说明

Seafile配置选项

选项描述注意

服务器名称此Seafile服务器的名称3-15个字符,仅允许使用英文字母,数字和下划线('_')

服务器IP或域此服务器使用的IP地址或域名Seafile客户端程序将使用此地址访问服务器

Seafile数据目录Seafile将您的数据存储在此目录中。默认情况下,它将放在当前目录中。随着您将越来越多的数据放入Seafile,此目录的大小将会增加。请选择具有足够可用空间的磁盘分区。

文件服务器端口Seafile文件服务器使用的TCP端口默认值为8082.如果已被其他服务使用,则可以将其设置为其他端口。

安装完成了就可以启动了

先增大文件打开数量

ulimit-n 30000

接下来启动服务

./seafile.shstart./seahub.shstart

在启动seahub时会要求你创建管理员,输入邮箱与密码,设置了记好

不出意外你现在应该可以在8000端口找到它

(我半天都没有安装好,最后原来是U盘的格式问题,用SSH居然就好了

还有那个开机挂载盘,我弄错两次害得它启动不了,最后不敢开机自动挂载了,手动吧。。。)

接着放出seafile的重启与关闭命令

#关闭./seahub.shstop./seafile.shstop#重启./seafile.sh restart./seahub.sh restart

以上步骤都可以在下方链接找到

https://manual.seafile.com/deploy/using_sqlite.html

4.绑定域名

这一步还挺关键的,决定你是否能在外网访问树莓派

我的宽带给的是公网IP,内网穿透先不考虑

首先在路由器ddns绑定一个别的域名

如图

我使用了3322的动态域名解析服务

免费的。。。

然后去把自己的域名设置CNAME解析,解析到那个3322的动态域名,

这样静止的域名就有了动态IP

(我腾讯云注册的域名)

接下来在路由器设置端口转发

我图省事直接设置DMZ主机,让外网的所有访问都到树莓派

接下来访问你设置的 http://域名:8000 应该也有了

5.安装Nginx并设置SSL

好倒是好了,可是我域名证书都下来了,还用着http,岂不是降低档次!

(免费域名证书自己找)

那么该配置Nginx和反向代理了。

首先安装nginx

sudo apt-getinstall nginxsudo /etc/init.d/nginx start

现在在树莓派80端口应该会有nginx的欢迎页面:Welcome to Nginx!

开始配置ssl和反向代理

创建并写入/etc/nginx/sites-available/seafile.conf

nano/etc/nginx/sites-available/seafile.conf

server{listen80;rewrite ^https://$http_host$request_uri?permanent;# force redirect http to httpsserver_tokensoff;    }server{listen443ssl;sslon;ssl_certificate/etc/nginx/sites-available/ct/1_moe.boxpaper.club_bundl$//设置成你自己的域名根证书        ssl_certificate_key /etc/nginx/sites-available/ct/2_moe.boxpaper.club.k$//设置成你自己的域名私钥ssl_protocols TLSv1 TLSv1.1TLSv1.2;ssl_ciphersECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;ssl_prefer_server_cipherson;ssl_session_cacheshared:SSL:10m;ssl_session_timeout10m;server_tokensoff;location/ {proxy_passhttp://127.0.0.1:8000;//设置成你自己的proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-Forwarded-Host$server_name;proxy_set_headerX-Forwarded-Proto https;access_log/var/log/nginx/seahub.access.log;error_log/var/log/nginx/seahub.error.log;proxy_read_timeout1200s;client_max_body_size0;        }# If you are using [FastCGI](http://en.wikipedia.org/wiki/FastCGI),# which is not recommended, you should use the following config for location `/`.##    location / {#        fastcgi_pass    127.0.0.1:8000;#        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;#        fastcgi_param  PATH_INFO          $fastcgi_script_name;##        fastcgi_param    SERVER_PROTOCOL    $server_protocol;#        fastcgi_param  QUERY_STRING        $query_string;#        fastcgi_param  REQUEST_METHOD      $request_method;#        fastcgi_param  CONTENT_TYPE        $content_type;#        fastcgi_param  CONTENT_LENGTH      $content_length;#        fastcgi_param    SERVER_ADDR        $server_addr;#        fastcgi_param    SERVER_PORT        $server_port;#        fastcgi_param    SERVER_NAME        $server_name;#        fastcgi_param  REMOTE_ADDR        $remote_addr;#          fastcgi_read_timeout 36000;##        client_max_body_size 0;##        access_log      /var/log/nginx/seahub.access.log;#          error_log      /var/log/nginx/seahub.error.log;#    }location/seafhttp {rewrite ^/seafhttp(.*)$$1break;proxy_passhttp://127.0.0.1:8082;//设置成你自己的client_max_body_size0;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_connect_timeout36000s;proxy_read_timeout36000s;proxy_send_timeout36000s;send_timeout36000s;proxy_request_bufferingoff;        }location/media {root/hehe/seafile-server-latest/seahub; //设置成你自己的        }    }

接着删除/etc/nginx/sites-enabled/default

并创建符号链接

rm /etc/nginx/sites-enabled/defaultln -s /etc/nginx/sites-available/seafile.conf /etc/nginx/sites-enabled/seafile.conf

重启nginx

sudo/etc/init.d/nginx reload

如果出现

[ ok ] Reloading nginx configuration (via systemctl): nginx.service.

那么就成功了。

现在访问你的域名就可以看到Seahub了。

6.安装手机端APP

Seafile安卓客户端:

http://seafile-downloads.oss-cn-shanghai.aliyuncs.com/seafile-2.2.17.apk

苹果直接搜索Seafile下载

电脑端访问

https://www.seafile.com/download/

就可以享受同步照片的乐趣啦!

结尾

这个文章可能是我写过最长的一篇,把它写下来也是记录我鼓捣中的经验,

虽然可能有些小众化,但是应该能帮到其他也想部署私有云的人。

相关文章

网友评论

    本文标题:从零开始的树莓派搭建NAS

    本文链接:https://www.haomeiwen.com/subject/glbeectx.html