Create your own cloud using Nextcloud on Raspberry Pi3

Alexandru Calcatinge bio photo By Alexandru Calcatinge

After creating a NAS in the last article, now I will create my own Cloud with Raspberry Pi 3 using nextCloud. This new project involves a clean new instance of the PIXEL operating system. Therefore, you should use another microSD card on which to put the Raspbian Jessie with PIXEL on it. After you do that, you should have available an external hard drive or memory stick. For my cloud I will use an external 160 GB hard drive. It is quite old, but can still do the job pretty well.

Update the system

The very first things you need to do after booting the RPi3 is to run

sudo apt update
sudo apt upgrade -y

Configure Raspberry Pi

Next, lets take care of some security basics for Raspberry Pi. Open a terminal window and type

sudo raspi-config

Now, you should change the password for the default pi user. Select the option and change it accordingly. After you do that, select 7 Advanced Options entry and then select the Expand Filesystem option from the Advanced Options entry. After you did that, select Finish to end the configuration.

Working with the external drive

Next thing to do is to run

sudo fdisk -l

The output will show you the available disks on your system. Besides the /dev/mmcblk0p that is the microSD with the PIXEL on it, you will see the /dev/sda that is the external hard drive to use for your cloud configuration. The hard drive is mounted, therefore you will have to unmount it with the command

sudo umount /dev/sda1

once this is done, you can format it if you didn’t do it already. If you did it previously to attaching it to the RPi3, then you should skip the formatting part. Mine is formatted in using NTFS, but I will format it for EXT4 using the command

sudo fdisk /dev/sda

and following the onscreen options. This will format the partition. The following step is to mount it again. You can choose where to mount it thus I will create a new directory called "mycloud" inside the home directory.

sudo mkdir mycloud

Now I will mount the external hard drive to that directory using the command

sudo mount /dev/sda1 ~/mycloud

Installing Apache

In order for nextCloud to work, we need some extra packages. Among those, there is apache, php, sqlite and nextCloud itself. Thus, first things first. To install apache use the command

sudo apt install apache2

Now that you installed apache, you should check and see if it is correctly installed. To do this, open the web browser and insert the RPi3’s IP in the address bar. Check to see if Apache2 is correctly installed.

Install PHP

NextCloud also requires PHP to be installed. In order to install it, you should use the following commands

sudo apt install php5 php5-gd sqlite php5-sqlite php5-curl

After php is installed you can now restart the apache service using the command:

sudo systemctl restart apache2.service

Install NextCloud

First you should download the NextCloud package from the official website using your web browser. On the left side of the page you will see the nextCloud Server section where you should click on the "Download" button. The archive is around 50 MB in size and the last stable version of the file at the time of writing this article is 11.0.3. By default, the file will be downloaded to your /Downloads directory. When Apache was installed, it created the directory structure "/var/www/html/" and we will move the archive there and then extract it. In the terminal, use the following commands:

cd Downloads/
sudo mv nextcloud-11.0.3.zip /var/www/html/
cd /var/www/html/
sudo unzip -q nextcloud-11.0.3.zip

After the archive was extracted (which could take a little bit of time) you can proceed to the next step.

Create the NextCloud Data Directory

The data directory for the nextCloud to work with should be created on the external hard drive. If you don’t do this, it will use the space on the microSD that the PIXEL uses. Remember that I mounted the external hard drive to the ~/mycloud directory.

cd mycloud/
sudo mkdir data
sudo chown www-data:www-data ~/mycloud/data
sudo chmod 750 ~/mycloud/data

Now check to see the ownership of the /data directory by using the command:

cd mycloud/
ls -l

The output will be:

pi@raspberrypi:~/mycloud $ ls -l
total 20
drwxr-x--- 2 www-data www-data 4096 May 13 12:47 data
drwx------ 2 root root 16384 May 13 11:49 lost+found

In order for nextCloud to know where your Data Directory is, you need to edit the configuration file. Like this:

sudo nano /var/www/html/nextcloud/config/config.php

Notice that I used nano and did not install vim, as I would have done usually. VIM is not installed by default in PIXEL. Inside the configuration file, add the following: 'datadirectory' => 'PATH_TO_YOUR_DATA', Save the changes to the file and exit.

Set directory owners

Next step is to set the correct directory owners for the nextCloud. First, we will set the directory owners for the "config" and "apps" directories inside the nextcloud directory:

cd /var/www/html/nextcloud
ls -l
sudo chown www-data:www-data config apps
ls -l

This will set ownership to apache for New ownership for "apps" and "config" directories.

Create a NextCloud Login

Inside your web browser, enter yout RPi3’s IP location followed by /nextcloud. In my case, my RPi3’s location IP is 192.168.0.17 and I will use the address 192.168.0.17/nextcloud in my browser.

Access your nextcloud interface inside your browser. After you choose a username and a password, it will take several minute for nextCloud to finish setup.

Now you are done setting up NextCloud.