波斯马BOSSMA Information Technology

在树莓派3B+上搭建个人私有云存储

发布时间:2018年7月6日 / 分类:树莓派 / 20,766 次浏览 / 评论

甲:为什么要搭建个人私有云存储?百度云不是挺好吗?

乙:….


之前公司的网管在局域网内部搭建了一个私有云存储:seafile,当时用的不亦乐乎,将内部的各种资料都搬了上去,每个人还可以有自己的私有空间,对于不适合放到公网但需要内部共享的资料特别合适,而且seafile能够自动同步本地和云端的文件,免去了手动上传下载的麻烦。

网上已经有很多人介绍使用树莓派搭建私有云存储,为了练手我也来搭建一个,碰巧seafile还有专门的树莓派版本。

1、下载seafile

对于树莓派的最新版本可以从这里获取:

https://github.com/haiwen/seafile-rpi/releases

当前最新版本是6.2.5,在/home/pi下创建一个seafile目录,然后下载安装包并解压到当前目录。

sudo mkdir seafile
sudo https://github.com/haiwen/seafile-rpi/releases/download/v6.2.5/seafile-server_6.2.5_stable_pi.tar.gz
sudo tar -xzf seafile-server_6.2.5_stable_pi.tar.gz

2、安装seafile

安装seafile需要提前安装一些依赖库:

sudo apt-get update
sudo apt-get install python2.7 libpython2.7 python-setuptools python-imaging python-ldap python-urllib3 sqlite3 python-requests

默认情况下paython 2.7 应该已经安装好了,这里谨慎些,所有依赖的都写上了。

然后开始执行seafile的安装程序:

cd seafile-server-6.2.5
sudo ./setup-seafile.sh

seafile运行启动两个程序:seafile server和seahub,一个是存储服务器,一个是存储服务器的网站界面。

运行安装脚本将需要回答一些预设问题,我这里做些注释:

-----------------------------------------------------------------
This script will guide you to config and setup your seafile server.

Make sure you have read seafile server manual at

        https://github.com/haiwen/seafile/wiki

Note: This script will guide your to setup seafile server using sqlite3,
which may have problems if your disk is on a NFS/CIFS/USB.
In these cases, we sugguest you setup seafile server using MySQL.

Press [ENTER] to continue 
# 运行到这里点击回车,以继续
-----------------------------------------------------------------


Checking packages needed by seafile ...

Checking python on this machine ...
Find python: python2.7

  Checking python module: setuptools ... Done.
  Checking python module: python-imaging ... Done.
  Checking python module: python-sqlite3 ... Done.

Checking for sqlite3 ...Done.

Checking Done.
# 运行到这里代表依赖检查完毕


What would you like to use as the name of this seafile server?
Your seafile users will be able to see the name in their seafile client.
You can use a-z, A-Z, 0-9, _ and -, and the length should be 3 ~ 15
[server name]: myseafile
# seafile服务器的名字,自己按规则起一个就行了

What is the ip or domain of this server?
For example, www.mycompany.com, or, 192.168.1.101

[This server's ip or domain]: 192.168.0.196
# 树莓派机器的IP或域名,我这里是局域网内,所以为局域网的IP地址。

Where would you like to store your seafile data?
Note: Please use a volume with enough free space.
[default: /home/pi/seafile/seafile-data ]
# seafile数据的存放位置,按照默认即可

What tcp port do you want to use for seafile fileserver?
8082 is the recommended port.
[default: 8082 ]
# seafile服务器的端口号,默认8082

This is your config information:

server name:        myseafile
server ip/domain:   192.168.0.196
seafile data dir:   /home/pi/seafile/seafile-data
fileserver port:    8082

If you are OK with the configuration, press [ENTER] to continue.
# 这里确认你的seafile服务器配置,没问题继续回车

Generating ccnet configuration in /home/pi/seafile/ccnet...

done
Successly create configuration dir /home/pi/seafile/ccnet.

Generating seafile configuration in /home/pi/seafile/seafile-data ...

Done.

-----------------------------------------------------------------
Seahub is the web interface for seafile server.
Now let's setup seahub configuration. Press [ENTER] to continue
-----------------------------------------------------------------
# 这里开始安装seahub,回车继续

Creating seahub database now, it may take one minute, please wait...


Done.

creating seafile-server-latest symbolic link ... done


-----------------------------------------------------------------
Your seafile server configuration has been completed successfully.
-----------------------------------------------------------------
# 已经安装完成了,下边是告诉你怎么启动安装好的程序。

run seafile server:     ./seafile.sh { start | stop | restart }
run seahub  server:     ./seahub.sh  { start <port> | stop | restart <port> }

-----------------------------------------------------------------
If the server is behind a firewall, remember to open these tcp ports:
-----------------------------------------------------------------
# 下边是程序要使用的端口,如果开启了防火墙,需要开放这两个端口。

port of seafile fileserver:   8082
port of seahub:               8000

When problems occur, refer to

      https://github.com/haiwen/seafile/wiki

for more information.

3、启动seafile

先启动seafile server:

pi@raspberrypi:~/seafile/seafile-server-6.2.5 $ ./seafile.sh start

[07/05/18 13:58:26] ../common/session.c(132): using config file /home/pi/seafile/conf/ccnet.conf
Starting seafile server, please wait ...
Seafile server started

Done.

然后启动seahub,这时候需要配置管理员账号和密码:

pi@raspberrypi:~/seafile/seafile-server-6.2.5 $ ./seahub.sh start

LC_ALL is not set in ENV, set to en_US.UTF-8
./seahub.sh: line 209: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory
Starting seahub at port 8000 ...

----------------------------------------
It's the first time you start the seafile server. Now let's create the admin account
----------------------------------------

What is the email for the admin account?
[ admin email ] xxxxxx@126.com
# 改为你的邮箱,作为管理员账号

What is the password for the admin account?
[ admin password ]
# 输入密码

Enter the password again:
[ admin password again ]
# 再次输入密码


----------------------------------------
Successfully created seafile admin
----------------------------------------





Seahub is started

Done.

现在可以在在浏览器中访问了:

http://{IP}:8000

正常会出现这个界面:

然后用上边配置的管理员账号和密码登陆就可以了,默认会自动创建一个私人资料库,这里边浏览、上传、下载文件都应该没有问题。

作为一个功能比较完整的云存储方案,seafile还提供了桌面客户端和手机客户端,都可以免费用,简直方便的不行,现在就可以去试试了。不过这篇文章还没有结束。

4、配置Nginx

为什么要用Nginx?Nginx是一个Web服务器,SeaHub可以认为是一个应用服务器。使用Nginx可以做负载均衡、静态文件缓存、权限控制等,而SeaHub则专注于业务功能,分工不同。对于大型的系统,Nginx更是必须具备的。

先说Nginx的安装,执行命令安装已经打包的程序就行了。

sudo apt-get install nginx

默认情况下nginx会安装到/etc/nginx目录下,这里需要配置一个站点,做反向代理。

在/etc/nginx/sites-enabled目录下增加一个配置文件:seafile_site.conf(可以随便取名),nginx会自动加载这个目录下的文件:

server
    {
# 站点监听端口,使用80更友好,这里测试,随便用了8800
        listen 8800;
# 服务器名称,这里使用了机器的局域网IP,如果有公网,可以用公网IP或域名
        server_name 192.168.0.196;
		
        proxy_set_header X-Forwarded-For $remote_addr;

# seahub 的反向代理配置
		location / {		
			proxy_pass         http://127.0.0.1:8000;
			proxy_set_header   Host $host;
			proxy_set_header   X-Real-IP $remote_addr;
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header   X-Forwarded-Host $server_name;
			proxy_read_timeout  1200s;

			# used for view/edit office file via Office Online Server
			client_max_body_size 0;

			access_log      /var/log/nginx/seahub.access.log;
			error_log       /var/log/nginx/seahub.error.log;
		}
		
# seafile server的反向代理设置
		location /seafhttp {
			rewrite ^/seafhttp(.*)$ $1 break;
			proxy_pass http://127.0.0.1:8082;
			client_max_body_size 0;
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

			proxy_connect_timeout  36000s;
			proxy_read_timeout  36000s;
			proxy_send_timeout  36000s;
			proxy_request_buffering off;
			send_timeout  36000s;
		}
		
		location /media {
			root /home/pi/seafile/seafile-server-latest/seahub;
		}
    }

因为都部署在一台服务器,所以这里反向代理的IP都是127.0.0.1,如果Nginx和seafile部署在不同的机器,这里需要替换成seafile服务器的IP。

然后启动nginx,如果已经启动则重新加载nginx配置:

sudo nginx
sudo nginx -s reload

nginx配置更新成功后,还需要访问seahub的原地址 http://{IP}:8000,修改两个URL参数,修改为Nginx代理站点的地址。

然后应该可以通过 http://{IP}:8800端口访问了,效果和访问http://{IP}:8000一样。

5、公网访问

只在局域网中访问没有太大意义,现在需要移动起来,这就需要公网访问。

(1)有公网IP

如果有固定的公网IP,直接域名解析,然后路由器上做个端口转发就可以了,但是个人用户一般是没有固定的公网IP的,甚至连公网IP都没有。

我这里用的联通宽带,是有外网IP的,但是运营商会随机分配,经常改变,所以这里通过动态DNS来访问。

注册 https://www.noip.com 获取一个免费域名和动态DNS支持。

然后安装noip duc:

mkdir noip
cd noip
wget http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz
tar vzxf noip-duc-linux.tar.gz
cd noip-2.1.9-1
make
sudo make install

make install时会询问一些配置:

Auto configuration for Linux client of no-ip.com.

Please enter the login/email string for no-ip.com  
no-ip账号邮箱

Please enter the password for user 'xxxxx@yy.com'  
no-ip账号密码

Only one host [bossmaseafile.hopto.org] is registered to this account.
It will be used.
Please enter an update interval:[30]  10
更新公网IP的时间间隔,我这里写的10分钟,也就是如果公网IP换了,可能需要10分钟才能通过域名正常访问

Do you wish to run something at successful update?[N] (y/N)  ^M
更新成功后不需要运行其它程序

New configuration file '/tmp/no-ip2.conf' created.

mv /tmp/no-ip2.conf /usr/local/etc/no-ip2.conf

然后运行no-ip客户端:

sudo /usr/local/bin/noip2

查看no-ip状态:

sudo /usr/local/bin/noip2 -S

然后还需要在路由器上做DNAT,这里使用9977(常用如80端口一般会被封掉,尽量使用一个不常用的端口),将9977端口转发到nginx所在服务器的8800端口。

端口转发功能,不同的路由器配置界面不同,找端口转发(自己买的路由器一般是这个)或者虚拟主机配置(联通送的路由器是这个名字)。

然后还需要再次更改步骤4中的SERVICE_URL和FILE_Server_ROOT,更改IP地址为通过noip获取到的域名。

现在试试用noip分配的免费域名访问吧。

不要高兴的太早,如果你在局域网中,可能这时候还是不通的,因为局域网内默认对当前外网IP端口的访问不会再转发给内网的机器,访问最终停在了路由器上。这时候可以试试在外网的机器通过域名访问,如果可以那么说明配置没有问题了。

(图中是测试用的域名和端口,访问时需要换成自己的)

如果需要在内网通过域名或外网IP进行访问,需要在路由器中增加SNAT的配置,将对外网IP的访问转发到对应的内网机器IP,这里就是Nginx的IP。但是一般家用路由器并不支持。还有一个办法就是修改本机的hosts文件,将对noip域名的访问映射到Nginx的IP地址。

(2)没公网IP

现在IPv4资源比较紧张,运营商又发展了很多用户,所以很多运营商都不分配公网IP给个人用户,这时候就要通过别的方式解决了,网上的方案大多是通过某个服务器建立到你机器的隧道,然后为这个隧道分配个域名,通过这个域名访问你的机器。

比较知名的服务商有ngrokc,不过是国外的,可能慢一些,国内有个natapp也提供了类似服务。这里就以natapp为例。这里为了测试注册一个免费的隧道:

然后安装natapp的客户端,官网也提供了树莓派的安装包:https://natapp.cn/#download

下载后上传到树莓派,我这里上传到/home/pi/natapp了,然后设置下文件执行权限:

cd /home/pi/natapp
sudo chmod a+x natapp

现在使用natapp网站注册的隧道token进行启动:

 ./natapp -authttoken=这里换成你的token

启动成功后可以看到分配的随机免费域名:

然后还需要再次更改步骤4中的SERVICE_URL和FILE_Server_ROOT,更改IP地址为这个域名。

然后就可以使用这个域名进行连接了。

6、演示截图

最后来几张seafile不同客户端的演示截图:

(1)Windows 本地文件夹:这个文件夹会自动双向同步云存储中的内容。

 

(2)Windows 桌面客户端:这个客户端可以管理文件,以及进行一些设置。

(3)手机APP:可以管理文件、自动上传照片等。

 

坑比较多,最后祝大家安装顺利。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自波斯马,原文地址《在树莓派3B+上搭建个人私有云存储

关键字:

建议订阅本站,及时阅读最新文章!
【上一篇】 【下一篇】

目前有4 条评论

  1. ac 10楼:

    你好,我设置了natapp之后怎么出现这个情况?

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to nginx.org.
    Commercial support is available at nginx.com.

    Thank you for using nginx.

  2. cc 9楼:

    sudo ./setup-seafile.sh 安装出现问题了
    Error occured during setup.
    Please fix possible issues and run the script again.

  3. ddch 8楼:

    非常感谢!写得很全的教程!非常有用!!!

发表评论