Automatic Nextcloud Installation on Raspberry Pi

Nextcloud is an open source software package providing remote file sharing services. It is similar to Dropbox. But with Nextcloud, you retain ownership, security and control of the shared data. This procedure describes how to build a working Nexcloud service using just 3 commands.

Note: If you would rather do the installation manually, step-by-step, without the help of a script, please see my previous article Simple Nextcloud Installation on Raspberry Pi. It explains how to do the installation in detail, and provides more background information on Nextcloud. Both procedures achieve the same overall result, however.

My Raspberry Pi was installed with Raspbian “Stretch” (Debian 9) by applying the image “2017-09-07-raspbian-stretch” 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.

It is recommended to carry out this procedure on a fresh installation of Raspbian.

Note: If, having completed the procedure below, you wish to convert your Nextcloud installation from HTTP to HTTPS, please see my article How to Convert a Website from HTTP to HTTPS.

Download Ansible

Ansible” is an automation system for IT infrastructures. We will use it to set up Nextcloud. Install Ansible now.

$ sudo apt-get -y install ansible

Download the build_nextcloud script from Github:

$ wget https://raw.githubusercontent.com/webtaster/Nextcloud/master/build_nextcloud.yml

Install Nextcloud

Now, install and configure Nextcloud. As it runs, the following command will print various informational messages to the terminal, some of which are shown below. It should complete in about 15 minutes on Raspbian Stretch, or 5 minutes if you are using Raspbian Jessie, so be patient.

Note that Nextcloud can use 2 type of databases: SQLite and MySQL. I would recommend SQLite, especially if your Nextcloud installation is to have only one or two users. SQLite is more lightweight than MySQL and uses fewer resources, and is therefore a good choice on Raspberry Pi hardware.

The following command will install and configure Nextcloud with SQLite.

$ sudo date ; ansible-playbook -s -c local -i "localhost," build_nextcloud.yml
Sat 18 Nov 15:48:07 UTC 2017
PLAY [127.0.0.1] ***************************************************************
TASK [setup] *******************************************************************
ok: [127.0.0.1]
...

If you want to use MySQL instead, use the command below. It will install Nextcloud together with the MySQL database.

$ date ; ansible-playbook -s -c local -i "localhost," --extra-vars "DATABASE=mysql MYSQL_ROOT_PASSWORD=qwerty NCUSER_PASSWORD=raindrop" build_nextcloud.yml
Sat 18 Nov 15:48:07 UTC 2017
PLAY [127.0.0.1] ***************************************************************
TASK [setup] *******************************************************************
ok: [127.0.0.1]
...

Note that I have chosen “querty” as the MySQL root password, and for the ncuser password, “raindrop”. You should choose your own passwords instead, and keep a note of them, as they are needed below.

Once the command completes (either command above), you will have a basic Nextcloud installation running on your Pi. Proceed as follows to create an admin user and complete the configuration.

Create a User for Nextcloud

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. Note, however: do not press the [Enter] key while you are on this page. If you do, the system will think you have finished the whole page, and it will try to complete the configuration, and fail, because we have not entered some values yet. Quite likely it will say “can’t create or write into the data directory var/www/html/nextcloud/data“, or something similar. Rather then pressing [Enter] to move between fields, just click the mouse on the field you want to edit. If you run into a problem, just reload the page on your browser and start entering values again.

Just below the “password” box is a “Storage & database” drop-down. Click it to reveal the “Data Folder” box. In that, change the default “/var/www/html/nextcloud/data” to “/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 “ncuser”.
– for the database password, enter the NCUSER_PASSWORD that you chose above (“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.


Wait for a couple of minutes while Nextcloud completes the installation. After 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.

Congratulations, you have just installed Nextcloud!.

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.

Conclusion

You now have a running Nextcloud installation. I hops this article has been helpful.

41 thoughts on “Automatic Nextcloud Installation on Raspberry Pi

  1. Pingback: Simple Nextcloud Installation on Raspberry Pi | Unix etc.

  2. So i figured out what I had to do.

    sudo a2enmod rewrite headers env dir mime

    I had to run that command, at least a portion of it, to allow for php to read the index.php file correctly.

    • Forgive my noobness.

      I moved the files running the mv and ln commands given above, now I am getting a 500 error when I try to go to the nextcloud section of pi.

      How did I mess up?

      Also, now that I have this quasi running, what do you I need to do to have the pi react to a domain that I have purchased and have that domain point to my external IP?

      Again, I am a noob at some of the principles of web applications.

      Thanks for all the help already.

      • Hi Ginger, without knowing the exact state of your Pi, it is difficult to suggest why things might not be working. I would recommend re-imaging your Pi with the latest Raspbian, the doing the procedure above on the fresh Pi. It should all work.

        Your question about domain names is beyond the scope of this procedure, but my other article might be helpful. Check it out: Using a Domain Name with a Raspberry Pi Web Server

        Cheers,
        Jim.

        • So I started over. I moved the data and now I am getting the error that states

          “can’t create or write data into the data directory /var/www/html/nextcloud/data”

          Any suggestions?

          • Hi Ginger, is this on a freshly re-imaged Pi ? The procedure is intended to run on newly installed Pi. Also, please post the whole output of the script, as well as the output of this command:

            cat /etc/os-release

          • Jim,

            The error comes when i try to go to the page and create my admin for the first time.

            Also, here is the output of the /etc/os-release

            PRETTY_NAME=”Raspbian GNU/Linux 9 (stretch)”
            NAME=”Raspbian GNU/Linux”
            VERSION_ID=”9″
            VERSION=”9 (stretch)”
            ID=raspbian
            ID_LIKE=debian

          • Hi Ginger, is this on a freshly re-imaged Pi ? The procedure is intended to run on newly installed Pi.

            You moved the data, and then got the error “can’t create or write data into the data directory /var/www/html/nextcloud/data” when you tried to create the user. However, in the procedure above, the user is created before the data is moved, not after. I am therefore not sure what you have done on the Pi, and it is difficult to advise. I would recommend that you re-image the Pi, then run the procedure above in exactly the order given on the newly installed Pi.

  3. I got a nice message that is quite weird imho.

    TASK [Restart Apache] **********************************************************
    fatal: [localhost]: FAILED! => {“changed”: false, “failed”: true, “msg”: “Unable to start service apache2: Job for apache 2.service failed because the control process exited with error code.\nSee \”systemctl status apache2.service\” and \”jour nalctl -xe\” for details.\n”}
    to retry, use: –limit @/home/pi/build_nextcloud.retry

    When I do it manually with the usual
    $ sudo service apache2 restart

    I got this error as well:
    Job for apache2.service failed because the control process exited with error code.
    See “systemctl status apache2.service” and “journalctl -xe” for details.

    What’s going on?
    And now the script failed I’m a bit worried on the mess it already installed and that I will not be able to clean properly :s

    • Hi Jeremy, that is strange. Is this a fresh installation of Raspbian, or a Pi that has been running for some time?

      Please post the output of the following 2 commands:

      tail /var/log/apache/error.log
      journalctl -xe

      Cheers,Jim.

  4. I have a new Pi 3. I successfully installed:
    – pihole
    – openvpn
    – Plex Server

    “journalctl -xe” is returning:

    nov. 30 20:30:45 raspberrypi apachectl[23560]: AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. S
    nov. 30 20:30:45 raspberrypi apachectl[23560]: (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
    nov. 30 20:30:45 raspberrypi apachectl[23560]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
    nov. 30 20:30:45 raspberrypi apachectl[23560]: no listening sockets available, shutting down
    nov. 30 20:30:45 raspberrypi apachectl[23560]: AH00015: Unable to open logs
    nov. 30 20:30:45 raspberrypi apachectl[23560]: Action ‘start’ failed.
    nov. 30 20:30:45 raspberrypi apachectl[23560]: The Apache error log may have more information.
    nov. 30 20:30:45 raspberrypi systemd[1]: apache2.service: Control process exited, code=exited status=1

    My other services seem to overlap with Apache :s

    That makes me realize I have some VNC errors as well, strange.

    “tail /var/log/apache/error.log” is returning an error saying it’s impossible to open it because no such file or folder exist.

    Thanks for the debug Jim!

    • This is what a “netstat -tulpn” return:

      Proto Recv-Q Send-Q Adresse locale Adresse distante Etat PID/Program name

      tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 595/lighttpd

      So a service named “lighttpd” is using it apparently. No idea what it is.

      • FYI, the problem was coming from PiHole which is using the port 80 by default.
        The thing was the make it use an other port, and then the problem got resolved.
        (Port is to be changer in /etc/lighttpd/lighttpd.conf )

        • Hi Jeremy, as your Pi is already running Pihole, Plex Server and a VPN, it isn’t too surprising that the above procedure will interfere with their configurations in some way. For example, as you say, Apache2 is competing with lighttpd for access to port 80. (Lighttpd is a web server, probably installed as part of Pihole). I recommend the above procedure for use only on a fresh Raspbian installation, ie. a Pi that isn’t doing anything else. I hope your Plex, VPN and Pihole functionality has not been damaged.

          Perhaps you could install Raspbian on a second SD card, put that in the Pi and do the above procedure? Meanwhile keeping VPN, Pihole and Plex intact on their own card.

          Cheers,
          Jim

          • I ran some tests.

            So if anyone read those comments they can know that Pihole, Plex, OpenVPN, and NextCloud can coexist perfectly well on a Pi 3.
            You just have to be careful to move the default PiHole webserver from the port 80 to an other one of your choice (through the /etc/lighttpd/lighttpd.conf file, I chose 8080 personally, and then restart the lighttpd service).
            No functionalities where damaged, and everything run smoothly now.
            Thanks for your work Jim.

            Now it’s time for me to put in place the https mode, and make a little raid1 system in place.

            Cheers,
            Jeremy

  5. (you should delete my 2 other comments with the 2 long outputs from the journalctl -xe as they were mistake of my part, thinking your website was not accepting them, as well as this one 😉 ).

    • Hi Jeremy, I deleted the two long comments and removed some long output from another. Hopefully it will make sense to future readers. The ansible script (build_nextcloud.yml) failed at the step where it restarts Apache. As you say, the error happened becuase port 80 was already in use by lighttpd, another web server that the Pi already happened to be running. Thanks for coming back, Jim.

  6. Hi,
    Thank you Jim for this procedure.

    I tried to install nextcloud automatically on the latest version of OSMC but something might went wrong. The install seems to have been done successfully but when I use the URL http://my Pis IP address/nextcloud the content of index.php opens. Nothing else happens. No login page.
    Did I miss something ?
    Thanks for your help !
    Arnaud

    • Hi Arnaud. You won’t be able to run the above procedure on OSMC. It is intended to be run only on Raspbian. OSMC is a heavily customized operating system and differs from Raspbian in many ways. Please try the procedure again on Raspbian.

      Incidentally, this procedure will be modified later this morning (11th Jan 2018) to include support for MySQL.

      • Ok Jim, thank you.
        Would it be ok with the “Simple Nextcloud Installation on Raspberry Pi“ procedure ?
        I’d like to store my media files on a USB HDD and share them with NextCloud and then run OSMC as a media center. Can’t see how to do it if it’s not possible to install Nextcloud on OSMC…

        • Hi Arnaud, no I don’t think that is possible unfortunately. OSMC is a heavily customized environment. Nextcloud, the Nextcloud install procedure and OSMC are highly likely to interfere with each other and not function properly. OSMC is meant for running Kodi only and is not suitable as a general operating system.

  7. Thanks. On my third attempt, with the advice from other websites, I’ve managed to install Nextcloud using your advice, but did hit a problem or two, so thought I would comment here.
    My installation was on a Pi2 with a new install of Raspbian Lite using Berryboot to make sure that I was using the external drive. (i’d already hit a problem with a previous install that I couldn’t use the external drive.) Unfortunately Berryboot uses Jessie instead of Stretch and update, upgrade, dist-upgrade still left me with Jessie.
    I used your automatic script an all seemed fine. I logged in using another computer which told me that there was no data directory, so using your manual setup guidelines.
    I was on the verge of a clean install of Stretch, but, with the thought of three failed attempts to install nextcloud behind me, instead went to:
    /var/www/html/nextcloud
    and created the data directory, following your “simple nextcloud installation”.
    I then followed your manual msql Lite instructions to create a user and password. I didn’t get the friendly
    Query OK, 0 rows affected (0.00 sec)
    message nor did MariaDB talk to me, whoever she is. However, I restarted the computer, logged in to Nextcloud and have successfully transferred one data file.
    So thanks again. Now to learn how to use nextcloud properly!

    • Hi Greg, glad you got Nextcloud working in the end. Personally, I don’t use Berryboot, but it should not have affected the above procedure.

      I am not sure what happened on your system or why the data directory was not created. When the script ran, it would have printed a message as it created the directory, something like this:

      TASK [Create data directory /var/nextcloud/data] ************
      changed: [127.0.0.1]

      Did you see the above message, including the “changed” part ?

  8. Hi I am not sure you are offering help here but in case you do.

    sudo date ; ansible-playbook -s -c local -i “localhost,” build_nextcloud.yml

    my output shows
    PLAY = [local host]
    TASK [setup]
    ok [local host]

    whereas yours shows a number instead of local host.
    Is that a significant issue?

    • Hi Daniel, it indicates a possible difference between your /etc/hosts file and mine. But it shouldn’t really matter. Did the procedure work, overall ?

  9. Thank you for this awesome tutorial. I have successfully installed Nextcloud on my new Pi 3. But I tried to mount my hard drive and it didn’t work (unfortunately, Nextcloud is not working anymore…) Do you by any change have advice on how to mount a hard drive on Raspbian, and how to restore parameters ? (for this last point, I guess the best is to fully delete Nextcloud, and to start the tuto again from the top ?)
    Many thanks.

      • Hi Jim. Noob that I am, I just plugged my hard drive via USB and tried to use a mount command. Tutorials indicating how to do this properly were not easy though. I think that when I used your 2 commands to move my nextcloud data from the sd card to the hard drive, something went wrong because of this badly made mounting phase. And now connecting to Nextcloud is impossible.
        I am planning on uninstalling Nextcloud and Ansible and start it all over but I am afraid I might do something wrong again at the same point…

        • I may have found a solution on your website, on your “Simple owncloud installation on RPi2” article. Thanks again for all your tutorials.

        • Hi Sam, before trying the procedure again (automatic or manual), I would recommend you become familiar with mounting and unmounting drives on the Pi. It is quite simple and there are loads of tutorials on the net. Cheers, Jim.

  10. I followed these instructions using the 2017-11-29-raspbian-stretch image, and I chose to do the MySQL install. I did not change the default ncuser & raindrop password. I successfully logged in once using those credentials, but after logging out, I now cannot log in. I get a “Wrong password” error. How can I fix this? Thanks!

  11. Hi,

    first of all: Thank you!

    I had a little problem with the data directory. Configuration was BananaPi, Debian 9.3, Linux Kernel 4.4. I used the script with SQLlite.

    When i tried to complete the installation (http://myip/nextcloud) there was an error message that the data directory is missing an not writable. The solution was

    mkdir /var/www/html/nextcloud/data
    chown www-data:www-data /var/www/html/nectcloud/data

    //Frank

    • Hi Frank, glad you got it working. The above procedure was written for Raspbian, rather than Debian. It hasn’t been tested on Debian and is therefore unlikely to work without modification. Interesting that you got it to work by just creating the data directory. Cheers, Jim.

  12. Hi Jim!
    Thanks for your tutorial. I will install my own Nextcloud setup eventually and I think this tutorial will help a lot. Quick question: what disk format do you recommend with Nextcloup on a Pi for the external usb drive?
    Thanks a lot!

    • Hi Frank and sorry about the delay in replying. The recommendation would be to use ext4, the default file system type for Raspbian. Cheers, Jim.

  13. Hi Jim,
    I’ve been able to set up my nextcloud correctly on PI 3 with raspbian Stretch using your automatic procedure. Nice one, very usefull!!!
    I’d like to set the https security and also able to connect to my nexcloud from the internet. Is there any simple procedure to follow?
    Thanks for your help.
    Massimo

    • Hi Massimo, glad to hear the automatic procedure worked for you. In order to make your site https, you will need to create a certificate and place it into your apache configuration. Unfortunately there is no article in this blog explaining the process. However many procedures are available on the Internet. Cheers, Jim.

  14. Thanks very much for this tutorial. I’ve built nextcloud several times by hand and this was far easier. Everything worked great until I got to the point of moving files onto an USB drive. Here are a few configuration items and the problems that occurred when trying to mv files onto the USB drive.

    started with a clean 2018-03-13-raspbian-stretch.img
    changed the default username/pw and enabled ssh
    followed your recipe exactly
    created a mount location at /mnt/FLASH64
    mounted the drive
    // everything is good so far … and then
    sudo mv /var/nextcloud/data /mnt/FLASH64

    I received messages for every file of the form
    mv: failed to preserve ownership for ‘/mnt/FLASH64/data/nextcloud.log’: Operation not permitted

    Any thoughts on where things went wrong?

    thanks again

Leave a Reply

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