Simple Nextcloud Installation on Raspberry Pi

This article explains how to install Nextcloud on the Raspberry Pi. It has been tested with the latest version of Nextcloud (13.0.4 at the time of writing), but should work for future versions too. Nextcloud is an open source software package providing remote file sharing services, similar to Dropbox. But with Nextcloud, you retain ownership, security and control of the shared data. Nextcloud works well on a Pi 2 and Pi 3 but will run very slowly on a Pi 1.

Note: This is a manual, step-by-step procedure. If you would rather do the installation automatically, please see my recent article Automatic Nextcloud Installation on Raspberry Pi, which explains how to install Nextcloud with 3 commands. It is the quickest and easiest way to get Nextcloud running. Both procedures achieve the same overall result, however.

My Raspberry Pi was purchased from RS Components in the UK. It was installed with Raspbian “Stretch” (Debian 9) by applying the image “2018-06-27-raspbian-stretch-lite.zip” downloaded from the raspberrypi.org downloads page. Raspbian Stretch was launched in August 2017, to supercede the earlier Raspbian “Jessie” (Debian 8). The following procedure works on both versions. Where there are differences, instructions applying to the both versions are provided. (Actually there is only one difference, relating to PHP packages)

Install Apache Web Server

Log into your Pi, either directly or through ssh from another system. I am using ssh. Update the software sources as follows.

$ sudo apt-get update

Install the Apache web server:

$ sudo apt-get install apache2

When asked if you want to continue, type “y” for “yes”. Apache will be installed along with a number of other packages.

In a browser, surf to the IP address of your Raspberry Pi. If you see a page entitled “Apache 2 Debian Default Page”, then Apache is running correctly on the Raspberry Pi. For example, my Pi is using IP address 192.168.1.99. So I start a browser on my PC and surf to http://192.168.1.99/. Alternatively, if you are using the Pi’s graphical desktop, you could start a browser directly on the Pi, eg. Netsurf.

Install PHP and Associated Packages

A few packages will now be installed. On Raspbian Jessie, install PHP 5. On Raspbian Stretch, install PHP 7 instead. If you are not sure what version of Raspbian you are using, check the contents of the file /etc/os-release. Here, I am using Stretch:

$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
...

The version of Raspbian is clearly shown on the first line of the file.

Install PHP 7.0 and Associated Packages (Raspbian Stretch only)

On Raspbian Stretch, Nextcloud requires PHP 7 and few other packages. Install them as follows. We will use the “-y” switch this time, which answers the “yes/no” question automatically.

$ sudo apt-get -y install php7.0 php7.0-gd sqlite php7.0-sqlite3 php7.0-curl php7.0-zip php7.0-xml php7.0-mbstring libapache2-mod-php7.0

Install PHP 5 and Associated Packages (Raspbian Jessie only)

On Raspbian Jessie, Nextcloud requires PHP 5 and few other packages. Install them as follows.

$  sudo apt-get install -y php5 php5-gd sqlite php5-sqlite php5-curl

The software will be installed, along with other dependent packages.

Restart Apache

Restart the web server now with:

$ sudo service apache2 restart

The command restarts Apache, but does not print out any messages.

Install Nextcloud

Okay, the system is ready for Nextcloud to be installed. Download the latest version of the software with this command on your Pi:

$ wget https://download.nextcloud.com/server/releases/latest.zip

Now move the Nextcloud package into place and unpack it:

$ sudo mv latest.zip /var/www/html
$ cd /var/www/html
$ sudo unzip -q latest.zip

If you are curious, you can check the version of Nextcloud as follows.
$ grep VersionString nextcloud/version.php
$OC_VersionString = '12.0.4';

At the time of writing, (9th Jan 2018), the latest version is 12.0.4. To check the latest version online, have a look at the Nextcloud download page.

Create the Data Directory

You must create a “data” folder for Nextcloud and set permissions. Proceed as follows.

$ sudo mkdir -p /var/nextcloud/data
$ sudo chown www-data:www-data /var/nextcloud/data
$ sudo chmod 750 /var/nextcloud/data

Check the new directory with ls. You should see output like this, although of course your date and time will be different:

$ ls -ld /var/nextcloud/data
drwxr-x--- 2 www-data www-data 4096 May 27 18:45 /var/nextcloud/data

Set Directory Owners

Next, set the correct ownerships on Nextcloud “config” and “apps” directories:

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


Configure Nextcloud for SQLite or MySQL

Nextcloud can use two kinds of databases: SQLite or MySQL. For a simple installation, with only one or two users, I would recommend SQLite. It is more light weight and uses fewer resources than MySQL, and is therefore a good choice on Raspberry Pi hardware. To use SQLite, skip straight to the section below entitled “Configure Nextcloud”.

If you want to use MySQL instead, proceed to Install and Configure MySQL Database.

Install and Configure MySQL Database

In order to use the MySQL database (also known as MariaDB), first install a few extra packages:

$ sudo apt-get install -y mysql-server python-mysqldb php7.0-mysql

Alternatively, if you are using the older Raspbian 8 (Jessie), use this command:

$ sudo apt-get install -y mysql-server python-mysqldb php5-mysql

Now set the MySQL root password and secure the database installation. Run the script /usr/bin/mysql_secure_installation. It will first ask you to enter the “current password for root”. No password has been set yet, so just press return to log in. Next, it asks if you want to set a root password. Answer “y“, choose a sensible password and make a note of it. Then answer “y” to all of the remaining questions, as below.

$ sudo /usr/bin/mysql_secure_installation
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
...
Thanks for using MariaDB!

Next, create a database for Nextcloud, and a database user. In the example I have called the Nextcloud user “ncuser” with a password of “raindrop”. For your own installation, choose a different user name and password. Don’t forget to take a note of both. Note that when running the following command, you will be asked to enter the root password that was set just above.

$ sudo mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database nextcloud;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create user ncuser;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> set password for ncuser = password("raindrop");
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all PRIVILEGES on nextcloud.* to ncuser@localhost identified by 'raindrop';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit;
Bye

Restart Apache

Restart the Apache web server once more with:

$ sudo service apache2 restart

The command restarts Apache, but does not print out any messages.


Configure Nextcloud

It is time to configure Nextcloud, either to use SQLite as a database, or, if you performed the commands in the last section, MySQL. Either way, proceed as follows.

In a browser, surf to your new Nextcloud web page. Use the URL:
http://your Pis IP address/nextcloud

For example, the address of my Pi is 192.168.1.99. So I go the the URL: http://192.168.1.99/nextcloud

You should see a mostly dark blue login page. In the middle is a “Performance Warning” about SQLite. Ignore that. Near the top it says “Create an admin account”. Think of a user name and password and type them into the boxes provided. Then click on the “Storage and Database” drop-down. In the “data folder” box which appears, delete what is already there and replace it with “/var/nextcloud/data

If you want to use SQLite as the database, then click on the “Finish Setup” button near the bottom of the page. Wait for a couple of minutes while Nextcloud completes the installation. During this time, Nextcloud creates more files and directories in our data directory, and it puts a SQLite database in there too.

If you want to use MySQL as the database, then where it says “Configure the database”, click on the button marked “MySQL/MariaDB”. Four new fields appear. Complete them as follows.
– for the database user, enter the name of the MySQL user you created above (“ncuser” in the example).
– for the database password, enter the accompanying password (“raindrop” in the example).
– for the database name, enter “nextcloud”
– leave “localhost” as “localhost”.

– then click on the “Finish Setup” button near the bottom of the page. Wait for a couple of minutes while Nextcloud completes the installation. During this time, Nextcloud creates more files and directories in our data directory, and it populates the MySQL database.

Completing the Configuration

Hey Presto! After a delay of 2 minutes or so, you should see the Nextcloud intro page with the slogan “a safe home for all your data”. Click the cross at the top right of the dialogue to dismiss it. You should now be looking at the main Nextcloud “Files” page. There you can see a couple of folders, a Nextcloud MP4 video file, and a PDF Manual.


Storing Nextcloud Data on a Separate Disk (Optional)

If you have a USB disk or thumb drive, you can use it to store the Nextcloud Data. In doing so, you might obtain more capacity for your data and/or improved performance.

First, mount your disk at a suitable location on the pi. Then proceed as follows. In this example, a thumb drive is mounted at “/disk1”. Move the Nextcloud data folder onto the drive as follows.

$ sudo mv /var/nextcloud/data /disk1/data
$ sudo ln -s /disk1/data /var/nextcloud/data

The first command moves the data. The second creates a link from the original data location to the new one. Finally, if you have Nextcloud open in a browser, refresh the page. It should reappear after a few moments. Your data has now been moved onto the disk/thumb drive. Try dragging a large file into Nextcloud – you should see the disk access light flash as the data is written.

Securing the Site with TLS/HTTPS

If you want to secure your Nextcloud installation by converting it from HTTP to HTTPS, see my later article: How to Convert a Website from HTTP to HTTPS.

Conclusion

That is the end of the procedure for installing Nextcloud. Your Nextcloud installation should now be fully working. I hope this guide was easy to follow and not too long or fiddly.

Note: Prior to January 2018, this article included a section on moving the Nextcloud data directory for security purposes. It is no longer necessary. Alterations to the procedure above mean that the data directory is created at a secure location and does not need to be moved.

Note: In the past, this article also included a section explaining how to increase the Nextcloud file size upload limit. This is no longer needed, as recent Nextcloud versions no longer have the old file size limitation.

Appendix – A Note on Nextcloud vs Owncloud

Nextcloud was forked from ownCloud in June 2016. Since then, Nextcloud has increased in popularity and has become, for many users, the natural successor to ownCloud. Previously I wrote an article describing how to install and upgrade Owncloud. Now, I would recommend Nextcloud over ownCloud, except for large corporate users who might already have commercial agreements with ownCloud Inc. If you came here from the ownCloud article, thank you for your patience.

END.

118 thoughts on “Simple Nextcloud Installation on Raspberry Pi

  1. Thanks for your superb tutorial!

    Everything went great except at the end. The server is running well except that it doesn’t let me upload files.
    Everytime I try to upload one it just displays a ‘Forbidden’ message.
    Do you know where that could come from?

    Thanks for your help!
    Leo

    • Hi Leo

      It sounds like it might be a permissions problem. Did you set the permissions properly on the “data” directory? What was the full error message that you saw ?

      Jim.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.