树莓派自建 NAS 云盘之——云盘构建
NAS

> 
> 用自行托管的树莓派 NAS 云盘来保护数据的安全！
> 
> 
> 


![](/data/attachment/album/201810/17/231845lcxls9emctcc6mbz.jpg)


在前面两篇文章中，我们讨论了用树莓派搭建一个 NAS 云盘所需要的一些 [软硬件环境及其操作步骤](/article-10104-1.html?utm_source=index&utm_medium=more)。我们还制定了适当的 [备份策略](/article-10112-1.html) 来保护 NAS 上的数据。本文中，我们将介绍讨论利用 [Nestcloud](https://nextcloud.com/) 来方便快捷的存储、获取以及分享你的数据。


![](/data/attachment/album/201810/17/231935uowcp6sfh2jezhpz.png)


### 必要的准备工作


想要方便的使用 Nextcloud，需要一些必要的准备工作。首先，你需要一个指向 Nextcloud 的域名。方便起见，本文将使用 **nextcloud.pi-nas.com** 。如果你是在家庭网络里运行，你需要为该域名配置 DNS 服务（动态域名解析服务）并在路由器中开启 80 端口和 443 端口转发功能（如果需要使用 https，则需要开启 443 端口转发，如果只用 http，80 端口足以）。


你可以使用 [ddclient](https://sourceforge.net/p/ddclient/wiki/Home/) 在树莓派中自动更新 DNS。


### 安装 Nextcloud


为了在树莓派（参考 [第一篇](/article-10104-1.html?utm_source=index&utm_medium=more) 中步骤设置）中运行 Nextcloud，首先用命令 `apt` 安装 以下的一些依赖软件包。



```
sudo apt install unzip wget php apache2 mysql-server php-zip php-mysql php-dom php-mbstring php-gd php-curl
```

其次，下载 Nextcloud。在树莓派中利用 `wget` 下载其 [最新的版本](https://nextcloud.com/install/#instructions-server)。在 [第一篇](/article-10104-1.html?utm_source=index&utm_medium=more) 文章中，我们将两个磁盘驱动器连接到树莓派，一个用于存储当前数据，另一个用于备份。这里在数据存储盘上安装 Nextcloud，以确保每晚自动备份数据。



```
sudo mkdir -p /nas/data/nextcloud
sudo chown pi /nas/data/nextcloud
cd /nas/data/
wget https://download.nextcloud.com/server/releases/nextcloud-14.0.0.zip -O /nas/data/nextcloud.zip
unzip nextcloud.zip
sudo ln -s /nas/data/nextcloud /var/www/nextcloud
sudo chown -R www-data:www-data /nas/data/nextcloud
```

截止到写作本文时，Nextcloud 最新版更新到如上述代码中所示的 14.0.0 版本。Nextcloud 正在快速的迭代更新中，所以你可以在你的树莓派中安装更新一点的版本。


### 配置数据库


如上所述，Nextcloud 安装完毕。之前安装依赖软件包时就已经安装了 MySQL 数据库来存储 Nextcloud 的一些重要数据（例如，那些你创建的可以访问 Nextcloud 的用户的信息）。如果你更愿意使用 Pstgres 数据库，则上面的依赖软件包需要做一些调整。


以 root 权限启动 MySQL：



```
sudo mysql
```

这将会打开 SQL 提示符界面，在那里可以插入如下指令——使用数据库连接密码替换其中的占位符——为 Nextcloud 创建一个数据库。



```
CREATE USER nextcloud IDENTIFIED BY '<这里插入密码>';
CREATE DATABASE nextcloud;
GRANT ALL ON nextcloud.* TO nextcloud;
```

按 `Ctrl+D` 或输入 `quit` 退出 SQL 提示符界面。


### Web 服务器配置


Nextcloud 可以配置以适配于 Nginx 服务器或者其他 Web 服务器运行的环境。但本文中，我决定在我的树莓派 NAS 中运行 Apache 服务器（如果你有其他效果更好的服务器选择方案，不妨也跟我分享一下）。


首先为你的 Nextcloud 域名创建一个虚拟主机，创建配置文件 `/etc/apache2/sites-available/001-netxcloud.conf`，在其中输入下面的参数内容。修改其中 `ServerName` 为你的域名。



```
<VirtualHost *:80>
ServerName nextcloud.pi-nas.com
ServerAdmin admin@pi-nas.com
DocumentRoot /var/www/nextcloud/

<Directory /var/www/nextcloud/>
AllowOverride None
</Directory>
</VirtualHost>
```

使用下面的命令来启动该虚拟主机。



```
a2ensite 001-nextcloud
sudo systemctl reload apache2
```

现在，你应该可以通过浏览器中输入域名访问到 web 服务器了。这里我推荐使用 HTTPS 协议而不是 HTTP 协议来访问 Nextcloud。一个简单而且免费的方法就是利用 [Certbot](https://certbot.eff.org/) 下载 [Let’s Encrypt](https://letsencrypt.org/) 证书，然后设置定时任务自动刷新。这样就避免了自签证书等的麻烦。参考 [如何在树莓派中安装](https://certbot.eff.org/lets-encrypt/debianother-apache) Certbot 。在配置 Certbot 的时候，你甚至可以配置将 HTTP 自动转到 HTTPS ，例如访问 `http://nextcloud.pi-nas.com` 自动跳转到 `https://nextcloud.pi-nas.com`。注意，如果你的树莓派 NAS 运行在家庭路由器的下面，别忘了设置路由器的 443 端口和 80 端口转发。


### 配置 Nextcloud


最后一步，通过浏览器访问 Nextcloud 来配置它。在浏览器中输入域名地址，插入上文中的数据库设置信息。这里，你可以创建 Nextcloud 管理员用户。默认情况下，数据保存目录在在 Nextcloud 目录下，所以你也无需修改我们在 [第二篇](/article-10112-1.html) 一文中设置的备份策略。


然后，页面会跳转到 Nextcloud 登陆界面，用刚才创建的管理员用户登陆。在设置页面中会有基础操作教程和安全安装教程（这里是访问 `https://nextcloud.pi-nas.com/settings/admin`）。


恭喜你，到此为止，你已经成功在树莓派中安装了你自己的云 Nextcloud。去 Nextcloud 主页 [下载 Nextcloud 客户端](https://nextcloud.com/install/#install-clients)，客户端可以同步数据并且离线访问服务器。移动端甚至可以上传图片等资源，然后电脑桌面都可以去访问它们。




---


via: <https://opensource.com/article/18/9/host-cloud-nas-raspberry-pi>


作者：[Manuel Dewald](https://opensource.com/users/ntlx) 选题：[lujun9972](https://github.com/lujun9972) 译者：[jrg](https://github.com/jrglinux) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In the first two parts of this series, we discussed the [hardware and software fundamentals](https://opensource.com/article/18/7/network-attached-storage-Raspberry-Pi) for building network-attached storage (NAS) on a Raspberry Pi. We also put a proper [backup strategy](https://opensource.com/article/18/8/automate-backups-raspberry-pi) in place to secure the data on the NAS. In this third part, we will talk about a convenient way to store, access, and share your data with [Nextcloud](https://nextcloud.com/).

![Raspberry Pi NAS infrastructure with Nextcloud Raspberry Pi NAS infrastructure with Nextcloud](https://opensource.com/sites/default/files/uploads/nas_part3.png)

## Prerequisites

To use Nextcloud conveniently, you have to meet a few prerequisites. First, you should have a domain you can use for the Nextcloud instance. For the sake of simplicity in this how-to, we'll use **nextcloud.pi-nas.com**. This domain should be directed to your Raspberry Pi. If you want to run it on your home network, you probably need to set up dynamic DNS for this domain and enable port forwarding of ports 80 and 443 (if you go for an SSL setup, which is highly recommended; otherwise port 80 should be sufficient) from your router to the Raspberry Pi.

You can automate dynamic DNS updates from the Raspberry Pi using [ddclient](https://sourceforge.net/p/ddclient/wiki/Home/).

## Install Nextcloud

To run Nextcloud on your Raspberry Pi (using the setup described in the [first part](https://opensource.com/article/18/7/network-attached-storage-Raspberry-Pi) of this series), install the following packages as dependencies to Nextcloud using **apt**.

`sudo apt install unzip wget php apache2 mysql-server php-zip php-mysql php-dom php-mbstring php-gd php-curl`


The next step is to download Nextcloud. [Get the latest release's URL](https://nextcloud.com/install/#instructions-server) and copy it to download via **wget** on the Raspberry Pi. In the first article in this series, we attached two disk drives to the Raspberry Pi, one for current data and one for backups. Install Nextcloud on the data drive to make sure data is backed up automatically every night.

```
sudo mkdir -p /nas/data/nextcloud
sudo chown pi /nas/data/nextcloud
cd /nas/data/
wget https://download.nextcloud.com/server/releases/nextcloud-14.0.0.zip -O /nas/data/nextcloud.zip
unzip nextcloud.zip
sudo ln -s /nas/data/nextcloud /var/www/nextcloud
sudo chown -R www-data:www-data /nas/data/nextcloud
```


When I wrote this, the latest release (as you see in the code above) was 14. Nextcloud is under heavy development, so you may find a newer version when installing your copy of Nextcloud onto your Raspberry Pi.

## Database setup

When we installed Nextcloud above, we also installed MySQL as a dependency to use it for all the metadata Nextcloud generates (for example, the users you create to access Nextcloud). If you would rather use a Postgres database, you'll need to adjust some of the modules installed above.

To access the MySQL database as root, start the MySQL client as root:

`sudo mysql`


This will open a SQL prompt where you can insert the following commands—substituting the placeholder with the password you want to use for the database connection—to create a database for Nextcloud.

```
CREATE USER nextcloud IDENTIFIED BY '<insert-password-here>';
CREATE DATABASE nextcloud;
GRANT ALL ON nextcloud.* TO nextcloud;
```


You can exit the SQL prompt by pressing **Ctrl+D** or entering **quit**.

## Web server configuration

Nextcloud can be configured to run using Nginx or other web servers, but for this how-to, I decided to go with the Apache web server on my Raspberry Pi NAS. (Feel free to try out another alternative and let me know if you think it performs better.)

To set it up, configure a virtual host for the domain you created for your Nextcloud instance **nextcloud.pi-nas.com**. To create a virtual host, create the file **/etc/apache2/sites-available/001-nextcloud.conf** with content similar to the following. Make sure to adjust the ServerName to your domain and paths, if you didn't use the ones suggested earlier in this series.

```
<VirtualHost *:80>
ServerName nextcloud.pi-nas.com
ServerAdmin admin@pi-nas.com
DocumentRoot /var/www/nextcloud/
<Directory /var/www/nextcloud/>
AllowOverride None
</Directory>
</VirtualHost>
```


To enable this virtual host, run the following two commands.

```
a2ensite 001-nextcloud
sudo systemctl reload apache2
```


With this configuration, you should now be able to reach the web server with your domain via the web browser. To secure your data, I recommend using HTTPS instead of HTTP to access Nextcloud. A very easy (and free) way is to obtain a [Let's Encrypt](https://letsencrypt.org/) certificate with [Certbot](https://certbot.eff.org/) and have a cron job automatically refresh it. That way you don't have to mess around with self-signed or expiring certificates. Follow Certbot's simple how-to [instructions to install it on your Raspberry Pi](https://certbot.eff.org/lets-encrypt/debianother-apache). During Certbot configuration, you can even decide to automatically forward HTTP to HTTPS, so visitors to **http://****nextcloud.pi-nas.com** will be redirected to ** https://nextcloud.pi-nas.com**. Please note, if your Raspberry Pi is running behind your home router, you must have port forwarding enabled for ports 443 and 80 to obtain Let's Encrypt certificates.

## Configure Nextcloud


The final step is to visit your fresh Nextcloud instance in a web browser to finish the configuration. To do so, open your domain in a browser and insert the database details from above. You can also set up your first Nextcloud user here, the one you can use for admin tasks. By default, the data directory should be inside the Nextcloud folder, so you don't need to change anything for the backup mechanisms from the [second part of this series](https://opensource.com/article/18/8/automate-backups-raspberry-pi) to pick up the data stored by users in Nextcloud.

Afterward, you will be directed to your Nextcloud and can log in with the admin user you created previously. To see a list of recommended steps to ensure a performant and secure Nextcloud installation, visit the Basic Settings tab in the Settings page (in our example: [https://nextcloud.pi-nas.com/](https://nextcloud.pi-nas.com/)

Congratulations! You've set up your own Nextcloud powered by a Raspberry Pi. Go ahead and [download a Nextcloud client](https://nextcloud.com/install/#install-clients) from the Nextcloud page to sync data with your client devices and access it offline. Mobile clients even provide features like instant upload of pictures you take, so they'll automatically sync to your desktop PC without wondering how to get them there.

## 2 Comments