@程序员,如何在五分钟内构建个人网盘服务?| 技术头条
作者 | 阿木
责编 | 郭芮
在目前众多网盘小场纷纷宣布自己倒下或者即将倒下的时代,大家除了使用几个大厂的服务外,也可另辟蹊径搭建一套属于自己的网盘服务。
针对个人需要的数据云备份、云存储、数据分享、在线视频、音乐等本文中部署的个人网盘皆可满足。话不多说,下面我们就进入个人网盘搭建的正文。本文中底层的云存储工具我们以owncloud为例。
根据维基百科介绍,ownCloud 是一个自由且开源的个人云存储解决方案,包括服务器和客户端两部分。
ownCloud 最早由KDE开发者Frank Karlitschek于2010年一月创建,目标是成为商业云存储服务商的替代。与商业云存储不同,ownCloud可以自由获取无需付费,但相应的,用户必须自行架设ownCloud服务器。
ownCloud在客户端可透过网页界面,或者安装专用的客户端软件来使用。网页界面当然就是任何能开网页的平台都支持,而客户端软件也支持相当多平台,Windows、Linux、iOS、Android皆有。
以下为部署的具体过程。
首先我们需要一台服务器,在此我们以本地自己搭建的一台虚拟机为例,发行版和内核信息如下:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@localhost ~]# uname -a
Linux work 3.10.0-327.36.1.el7.x86_64 #1 SMP Sun Sep 18 13:04:29 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]#
部署环境准备完成后,我们在虚拟机上安装下红帽的yum源:
[root@localhost ~]# yum install -y epel-release
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
* base: ftp.sjtu.edu.cn
* epel: mirrors.huaweicloud.com
* extras: mirrors.163.com
* updates: mirrors.zju.edu.cn
Package epel-release-7-11.noarch already installed and latest version
Nothing to do
[root@localhost ~]#
由于我前面已经更新过虚拟机的yum源,因此并未做什么更新。
然后我们需要在虚拟机上部署下Docker 运行的环境,在此我们直接采用yum在线安装的方式直接安装下。其他部署方式,诸如源码编译安装等感兴趣的同学可以自行参考Docker 官网安装教程(https://docs.sevenbridges.com/docs/install-docker)进行部署。
Docker 安装过程可能需要几分钟的时间,安装完成后需要启动下 Docker 守护进程:
[root@localhost ~]# systemctl start docker
[root@localhost ~]#
查看下 Docker 守护进程是否已经在运行:
[root@localhost ~]# systemctl status docker
docker.service - Docker Application Container Engine
Loaded: loaded (/etc/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2019-02-25 15:35:52 CST; 46s ago
Docs: http://docs.docker.com
Main PID: 14413 (dockerd-current)
CGroup: /system.slice/docker.service
├─14413 /usr/bin/dockerd-current --exec-opt native.cgroupdriver=systemd --selinux-enabled=false --insecure-registry gcr...
└─14428 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-int...
Feb 25 15:35:52 work docker-current[14413]: time="2019-02-25T15:35:52.555468533+08:00" level=info msg="Firewalld running: false"
Feb 25 15:35:52 work docker-current[14413]: time="2019-02-25T15:35:52.650339401+08:00" level=info msg="Default bridge (docke...dress"
Feb 25 15:35:52 work docker-current[14413]: time="2019-02-25T15:35:52.675313581+08:00" level=info msg="Loading containers: done."
Feb 25 15:35:52 work docker-current[14413]: time="2019-02-25T15:35:52.676370514+08:00" level=warning msg="Not using native d...o fix"
Feb 25 15:35:52 work docker-current[14413]: time="2019-02-25T15:35:52.676966551+08:00" level=warning msg="failed to retrieve...$PATH"
Feb 25 15:35:52 work docker-current[14413]: time="2019-02-25T15:35:52.677003665+08:00" level=warning msg="failed to retrieve...rsion"
Feb 25 15:35:52 work docker-current[14413]: time="2019-02-25T15:35:52.677250954+08:00" level=info msg="Daemon has completed ...ation"
Feb 25 15:35:52 work docker-current[14413]: time="2019-02-25T15:35:52.677267059+08:00" level=info msg="Docker daemon" commit...1.13.1
Feb 25 15:35:52 work docker-current[14413]: time="2019-02-25T15:35:52.681264961+08:00" level=info msg="API listen on /var/ru....sock"
Feb 25 15:35:52 work systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#
设置开机自启:
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /etc/systemd/system/docker.service.
确认下是否可以查看到 Docker 的info 信息,正常部署完成可以看到Docker 的 info 信息:
[root@localhost ~]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.13.1
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: false
Native Overlay Diff: false
Logging Driver: json-file
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: N/A (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: N/A (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-327.36.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 4
Total Memory: 976.9 MiB
Name: work
ID: V7HM:XRBY:P6ZU:SGWK:J52L:VYOY:UK6L:TR45:YJRC:SZBS:DQRF:CFP5
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://hub-mirror.c.163.com/v1/
Experimental: false
Insecure Registries:
gcr.io
127.0.0.0/8
Live Restore Enabled: false
Registries: hub-mirror.c.163.com (secure), docker.io (secure)
[root@localhost ~]#
从结果可以看出,当前我们安装的Docker 版本为1.13。
Docker 环境部署结束后,我们需要pull下ownload 的镜像,在docker pull 之前我们需要先登录下目标hub:
[root@localhost ~]# docker login -u 账号 -e your@email hub.c.163.com
Flag --email has been deprecated, will be removed in 1.14.
Password:
Login Succeeded
[root@localhost ~]#
拉取owncloud 镜像:
[root@localhost ~]# docker pull hub.c.163.com/library/owncloud:latest
Trying to pull repository hub.c.163.com/library/owncloud ...
sha256:fdda568e8fef81f69df873d965d67d112eff770cff64f9b1ba055c7e19ea6ac0: Pulling from hub.c.163.com/library/owncloud
3ebc4e3ed1b2: Pull complete
c564d17b49c8: Pull complete
c424469b3e97: Pull complete
5e11f00270e2: Pull complete
4e954eec6dd4: Pull complete
176ba716cc40: Pull complete
59f7c8998494: Pull complete
7c3913488b7f: Pull complete
2409b3c9501c: Pull complete
b675e82c75a1: Pull complete
6f624c1c100d: Pull complete
7d41dd54b1c4: Pull complete
b7990372920c: Pull complete
99d88c7f1c49: Pull complete
00e2690b63a6: Pull complete
8ff08f48bba5: Pull complete
658c2474e164: Pull complete
92371a48c368: Pull complete
fc513e087257: Pull complete
bf45de679453: Pull complete
Digest: sha256:fdda568e8fef81f69df873d965d67d112eff770cff64f9b1ba055c7e19ea6ac0
Status: Downloaded newer image for hub.c.163.com/library/owncloud:latest
[root@localhost ~]#
检查本地镜像:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hub.c.163.com/library/owncloud latest 183d08975211 19 months ago 587 MB
[root@localhost ~]#
可以看到ownload 镜像已经被拉取到本地,且tag为latest。
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hub.c.163.com/library/owncloud latest 183d08975211 19 months ago 587.2 MB
[root@localhost ~]#
运行owncloud容器:
[root@localhost ~]# docker run --name owncloud –p 80:80 -d hub.c.163.com/library/owncloud:latest
a4e48b60d0eb3b233000c70d35bc602f1233bc37a9dac9252a3e0ffeddf4de29
容器名称owncloud,并将容器中的80端口映射到虚拟机的80端口,这样我们访问虚拟机的80端口时即可访问到容器的80端口,即owncloud。
查看owncloud 容器是否已经在后台运行起来:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4e48b60d0eb hub.c.163.com/library/owncloud:latest "docker-entrypoint.sh" 6 seconds ago Up 3 seconds 80/tcp owncloud
[root@localhost ~]#
Owncloud 中注册信息、配置信息等需要写入mysql,因此下面我们需要部署一下mysql 数据库服务,本文中我们将mysql部署到虚拟机上。
建议安装mysql5.7 或者更高的版本,否则在设置录owncloud时会报错:
具体过程如下:
首先需要先卸载掉CentOS7.x上自带的mariadb数据库,删除mariadb 数据库残留的数据库文件:
[root@localhost ~]# rm -rf /var/lib/mysql*
下载mysql yum 源安装文件:
[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
--2019-02-25 20:57:41-- http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [following]
--2019-02-25 20:57:41-- https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm [following]
--2019-02-25 20:57:52-- https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.78.217.58
Connecting to repo.mysql.com (repo.mysql.com)|23.78.217.58|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25548 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-10.noarch.rpm’
100%[==============================================================================>] 25,548 --.-K/s in 0.03s
2019-02-25 20:57:52 (751 KB/s) - ‘mysql57-community-release-el7-10.noarch.rpm’ saved [25548/25548]
-c: No such file or directory
No URLs found in -c.
FINISHED --2019-02-25 20:57:52--
Total wall clock time: 12s
Downloaded: 1 files, 25K in 0.03s (751 KB/s)
安装下载的mysql yum 源:
[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
Loaded plugins: fastestmirror, langpacks
Examining mysql57-community-release-el7-10.noarch.rpm: mysql57-community-release-el7-10.noarch
Marking mysql57-community-release-el7-10.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-10 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================
Package Arch Version Repository Size
========================================================================================================================
Installing:
mysql57-community-release noarch el7-10 /mysql57-community-release-el7-10.noarch 30 k
Transaction Summary
========================================================================================================================
Install 1 Package
Total size: 30 k
Installed size: 30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql57-community-release-el7-10.noarch 1/1
Verifying : mysql57-community-release-el7-10.noarch 1/1
Installed:
mysql57-community-release.noarch 0:el7-10
Complete!
yum 安装 mysql 服务:
[[root@localhost ~]# yum -y install mysql-community-server
Loaded plugins: fastestmirror, langpacks
mysql57-community | 2.5 kB 00:00:00
mysql57-community/x86_64/primary_db | 170 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* epel: mirror.premi.st
* extras: mirrors.163.com
* updates: mirrors.zju.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.25-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.25-1.el7 for package: mysql-community-server-5.7.25-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.25-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.25-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.25-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.6.43-2.el7 will be updated
---> Package mysql-community-common.x86_64 0:5.7.25-1.el7 will be an update
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.6.43-2.el7 will be updated
---> Package mysql-community-libs.x86_64 0:5.7.25-1.el7 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================
Package Arch Version Repository Size
========================================================================================================================
Installing:
mysql-community-server x86_64 5.7.25-1.el7 mysql57-community 165 M
Installing for dependencies:
mysql-community-client x86_64 5.7.25-1.el7 mysql57-community 24 M
Updating for dependencies:
mysql-community-common x86_64 5.7.25-1.el7 mysql57-community 274 k
mysql-community-libs x86_64 5.7.25-1.el7 mysql57-community 2.2 M
Transaction Summary
========================================================================================================================
Install 1 Package (+1 Dependent package)
Upgrade ( 2 Dependent packages)
Total download size: 192 M
Downloading packages:
No Presto metadata available for mysql57-community
(1/4): mysql-community-common-5.7.25-1.el7.x86_64.rpm | 274 kB 00:00:00
(2/4): mysql-community-libs-5.7.25-1.el7.x86_64.rpm | 2.2 MB 00:00:01
(3/4): mysql-community-client-5.7.25-1.el7.x86_64.rpm | 24 MB 00:00:13
(4/4): mysql-community-server-5.7.25-1.el7.x86_64.rpm | 165 MB 00:01:47
------------------------------------------------------------------------------------------------------------------------
Total 1.8 MB/s | 192 MB 00:01:48
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
……
……
Installed:
mysql-community-server.x86_64 0:5.7.25-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:5.7.25-1.el7
Dependency Updated:
mysql-community-common.x86_64 0:5.7.25-1.el7 mysql-community-libs.x86_64 0:5.7.25-1.el7
Complete!
[root@localhost ~]#
安装完成后,需要启动下mysql服务:
[root@localhost ~]# systemctl start mysqld
查看mysql 服务状态:
[
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2019-02-25 21:10:23 CST; 11s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 8120 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 8042 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 8123 (mysqld)
Memory: 295.7M
CGroup: /system.slice/mysqld.service
└─8123 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Feb 25 21:10:10 localhost.localdomain systemd[1]: Starting MySQL Server...
Feb 25 21:10:23 localhost.localdomain systemd[1]: Started MySQL Server.
[ ]
]设置mysql 开机自启:
[root@localhost ~]# systemctl enable mysqld
设置完成后需要为owncloud 设置一个数据库账号,并新建一个数据库owncloud。
查看数据库原始密码:
[root@localhost ~]# grep "password" /var/log/mysqld.log
2019-02-25 20:36:49 3776 [Note] Shutting down plugin 'sha256_password'
2019-02-25 20:36:49 3776 [Note] Shutting down plugin 'mysql_old_password'
2019-02-25 20:36:49 3776 [Note] Shutting down plugin 'mysql_native_password'
2019-02-25 20:52:46 7441 [Note] Shutting down plugin 'sha256_password'
2019-02-25 20:52:46 7441 [Note] Shutting down plugin 'mysql_old_password'
2019-02-25 20:52:46 7441 [Note] Shutting down plugin 'mysql_native_password'
2019-02-25T13:10:18.272936Z 1 [Note] A temporary password is generated for root@localhost: tletSusYl3%S
2019-02-25T13:11:14.004386Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost ~]#
从结果看初始密码为:tletSusYl3%S。
我们先用原始密码登录数据库,然后修改初始密码,修改mysql 账号白名单:
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.25
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@owncloud123';
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> grant all privileges on *.* to 'root'@'%' identified by 'Root@owncloud123';
Query OK, 0 rows affected, 1 warning (0.01 sec)
新建数据库owncloud:
create database owncloud;
Query OK, 1 row affected (0.00 sec)
以上即配置数据库的过程。
下面我们通过虚拟机的IP访问下owncloud:
根据页面提示配置信息。
Admin count 根据自己需要进行设置即可,中间的数据存放路径采用默认即可,数据库此处我们选择mysql/Mariadb,填写上文中我们设置的mysql的账号、密码、数据库,host填写虚拟机IP 即可。
此过程需要约1分钟,初始化成功后会自动跳转到登录界面:
查看数据库,此时可看到库owncloud中已经新建了很多表,且表oc_accounts中已经写入了我们刚刚注册的账号信息:
mysql> use owncloud;
Database changed
mysql> show tables;
+-----------------------------+
| Tables_in_owncloud |
+-----------------------------+
| oc_account_terms |
| oc_accounts |
| oc_addressbookchanges |
| oc_addressbooks |
| oc_appconfig |
| oc_authtoken |
| oc_calendarchanges |
| oc_calendarobjects |
| oc_calendars |
| oc_calendarsubscriptions |
| oc_cards |
| oc_cards_properties |
| oc_comments |
| oc_comments_read_markers |
| oc_credentials |
| oc_dav_shares |
| oc_external_applicable |
| oc_external_config |
| oc_external_mounts |
| oc_external_options |
| oc_federated_reshares |
| oc_file_locks |
| oc_filecache |
| oc_files_trash |
| oc_group_admin |
| oc_group_user |
| oc_groups |
| oc_jobs |
| oc_migrations |
| oc_mimetypes |
| oc_mounts |
| oc_notifications |
| oc_preferences |
| oc_privatedata |
| oc_properties |
| oc_schedulingobjects |
| oc_share |
| oc_share_external |
| oc_storages |
| oc_systemtag |
| oc_systemtag_group |
| oc_systemtag_object_mapping |
| oc_trusted_servers |
| oc_users |
| oc_vcategory |
| oc_vcategory_to_object |
+-----------------------------+
46 rows in set (0.00 sec)
mysql> select * from oc_accounts;
+----+-------+----------+---------------+--------------+-------+------------+------------------+-----------------------------+-------+
| id | email | user_id | lower_user_id | display_name | quota | last_login | backend | home | state |
+----+-------+----------+---------------+--------------+-------+------------+------------------+-----------------------------+-------+
| 1 | NULL | owncloud | owncloud | owncloud | NULL | 0 | OC\User\Database | /var/www/html/data/owncloud | 1 |
+----+-------+----------+---------------+--------------+-------+------------+------------------+-----------------------------+-------+
1 row in set (0.00 sec)
mysql>
登录owncloud:
点击右上角用户名中的设置选项:
在新的页面中根据需要选择需要下载的客户端工具:
此处我们选择Desktop app,根据需要下载本地客户端:
客户端下载并安装后填写上文中注册的账号信息,客户端使用方式和常见商业网盘客户端使用方式基本一样。更多使用可以参看ownCloud官网文档(https://owncloud.org/)。
至此,大功告成。
作者简介:阿木,目前就职于国内某知名互联网公司,担任云计算技术部高级工程师,近3年云计算从业经验,爱读书、爱写作、爱技术。
【End】
热 文 推 荐
☞微软 CTO 韦青:“程序员 35 岁就被淘汰”是个伪概念 | 人物志
☞程序员深夜逆行被拦后崩溃欲自杀:老板在催我!女朋友在催我!
☞轻松了解面试官心理!ElasticSearch写入数据的工作原理是什么? | 技术头条
☞他曾主导世界上第一台安卓智能机, 如今能否靠区块链手机找回昔日的光荣?|人物志
☞如何将TensorFlow Serving的性能提高超过70%?
System.out.println("点个在看吧!");
console.log("点个在看吧!");
print("点个在看吧!");
printf("点个在看吧!\n");
cout << "点个在看吧!" << endl;
Console.WriteLine("点个在看吧!");
Response.Write("点个在看吧!");
alert("点个在看吧!")
echo "点个在看吧!"