Pi My Life Up

How to Setup a Raspberry Pi Nextcloud Server

For this project, we will be showing you how to setup and configure a Raspberry Pi NextCloud Server, this can act as your own personal “cloud” storage system.

As time goes on the protection of your own privacy with 3rd party companies becomes harder and harder. This is where software like Nextcloud comes in hand, as it gives you full control over your files with no 3rd party controller.

It is important to remember that since your data will be stored on your local network you will end up using a lot of bandwidth when uploading and downloading files from outside your local network. If your internet connection is not great then you may not get the best experience if you plan on using it outside your local network.

If this looks familiar then that’s because it likely is, Nextcloud is an actively maintained fork of the owncloud software that I have previously covered. The longer it’s in development the more different these two software packages will likely become, I suggest looking into both and then deciding on which one to go with.

If you want to learn more about Nextcloud, you can check out the nextcloud website.

Note: The USB ports on a Raspberry Pi are typically unable to power an external hard drive. If you find this the case and your hard drive doesn’t use an external power supply then I recommend looking into buying a powered USB hub for the Pi.

Equipment List

You can find all the bits and pieces that I used/recommend for this Raspberry Pi nextcloud tutorial right below.

Recommended:

Raspberry Pi 2 or 3

Micro SD Card or a SD card if you’re using an old version of the Pi.

Ethernet Cord or Wifi dongle (Pi 3 has WiFi inbuilt)

External Hard drive or USB Drive

Optional:

Raspberry Pi Case

USB Keyboard

USB Mouse

Installing Apache and PHP

To run Nextcloud on the Raspberry Pi we will first need to install and setup Apache and PHP. We won’t be going too in-depth into installing these as they are a minor components to this tutorial. If you want to learn more about setting up a Web Server, then be sure to follow our tutorial on how to do this.

For the best performance I recommend using Raspbian lite but just normal Raspbian will also work just as well. If you need information on how to set this all up check out the guide in the Pi operating systems section.

For this tutorial, we will only be utilizing PHP 5.

1. To get started let’s first update our package repositories with the following command:

sudo apt-get update
sudo apt-get upgrade

2. With that done, let’s now install apache with the following command:

sudo apt-get install apache2

You can check to make sure Apache2 is successfully up and running by going to your Pi’s IP address, this should load a default Apache Page. If you are unsure on what your Raspberry Pi’s local IP address is then type in hostname -I into the terminal.

3. With Apache2 now installed onto the Raspberry Pi we just need to install PHP and several of its packages, for this tutorial we will be using either PHP5 or PHP7, this depends mainly on whether you are running Raspbian Jessie or Raspbian Stretch.

To install PHP and the packages we need, run the following commands, this changes depending on if you are using Stretch and later or Jessie:

Raspbian Jessie

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

Raspbian Stretch

sudo apt-get install php7.0 php7.0-gd sqlite php7.0-sqlite php7.0-curl

4. With Apache and PHP now installed there is one final thing we need to do, and that is to restart Apache. You can do this with the following command:

sudo service apache2 restart 

Installing Nextcloud

Installing Nextcloud to the Raspberry Pi is quite simple, it mainly involves downloading the script from their website, extracting the zip and then going to your Raspberry Pi’s IP address.

1. To get started let’s first move to our html directory with the following command:

cd /var/www/html

2. Now we can run the following curl command so we can download and extract the latest version of Nextcloud in one go.

curl https://download.nextcloud.com/server/releases/nextcloud-11.0.2.tar.bz2 | sudo tar -jxv

3. Now for the next few steps we need to change directory into our newly unzipped folder, to do this run the following command.

cd /var/www/html/nextcloud

4. We now need to create a data directory for Nextcloud to operate in, for the initial setup of Nextcloud we must make this folder in our html/nextcloud directory. Do that with the following command:

sudo mkdir -p /var/www/html/nextcloud/data

5. Now let’s give the correct user and group control over the data folder by running the following command.

sudo chown www-data:www-data /var/www/html/nextcloud/data

6. Finally we need to give it the right permissions, again run the following command:

sudo chmod 750 /var/www/html/nextcloud/data

7. We are not quite done dealing with permissions, there is one final thing we must do and that is give the www-data group control over the config and apps folder. Run the following command to do this:

sudo chown www-data:www-data config apps

8. Now that we have finished with that we can now finally go to Nextcloud itself and begin its installation process. To begin go to your Raspberry Pi’s IP address plus /nextcloud. For example, the address I would go to is the following:

Remember to replace my IP Address with that of your Raspberry Pi’s .

https://192.168.1.105/nextcloud

9. You will now be greeted with the following screen, here you will need to type in the Username and Password that you intend to use for your admin account. If you plan on allowing your Nextcloud file service to be accessible from outside your network, make sure that you use a long and secure password.

Once you are happy with this, press the “Finish Setup” button, please note this can take some time to complete as it finalises your setup.

10. After this you should now be greeted with the following welcome screen, this just lays out the various programs you can use to connect with your Nextcloud installation. Just press the X button in the top right corner to continue.

11. Now you can finally see the interface of the Raspberry Pi Nextcloud, you should take some time to familiarize yourself with all the functionality of Nextcloud’s interface.

We won’t go too in depth on how to use the Nextcloud interface, if you need more information then I recommend checking out the support section on nextcloud. We have however highlighted some of the key areas to check out in the screenshot below.

Moving Nextcloud’s data folder

With Nextcloud now safely installed we can now tweak the setup to both be more secure and a bit more useable, one of the first things we should do is move the data directory so it does not sit in our web accessible directory.

This is also the same way you would move your Nextcloud data directory onto a larger external hard drive rather than putting increased load onto the Raspberry Pi’s SD Card.

1. To get started let’s make our new directory for where we will store our data files, to make it easy we will make a new folder at /var/nextcloud and move our data folder into there.

Create the folder by running the following command:

sudo mkdir -p /var/nextcloud

2. With our new folder we created we will now move our data directory into it, this is easy to do thanks to the mv command. Please note that your Nextcloud system will be out of action while we move the file then adjust the configuration file.

To begin the move type in the following command:

sudo mv -v /var/www/html/nextcloud/data /var/nextcloud/data

3. Now with the files moved over we can now modify the datadirectory configuration to point to our new directory. First, let’s change to the config directory for Nextcloud with the following command.

cd /var/www/html/nextcloud/config

4. We can now copy the config file to make a backup of the file, we can do this with the following command:

sudo cp -p config.php config.php.bk

5. Finally let’s open up the config.php file for editing using nano.

sudo nano config.php

6. Within this file we need to change the following line:

'datadirectory' => '/var/www/html/nextcloud/data',

To

'datadirectory' => '/var/nextcloud/data',

7. Now we can save and quit out of the file by pressing Ctrl+X then Y and then Enter.

You should be able to now refresh your web browser and all your files should be showing exactly as they were previously.

Increasing Nextcloud’s max upload size

By default, PHP has a very low upload limit, so low it’s only 2 MB. To change this, we need to modify the php.ini file and increase the limit. A cloud storage system wouldn’t be very useful if you could only ever upload 2mb files.

1. To get started we need to begin editing the configuration file with the following command:

sudo nano /etc/php5/apache2/php.ini

2. Now we need to find and replace the following two lines.

post_max_size = 8M
upload_max_filesize = 2M

To

post_max_size = 1024M
upload_max_filesize = 1024M

Of course, you can set the file size limits to something that is much higher than 20M, so feel free to change that number to whatever you think is the maximum size file you will upload to your Nextcloud.

3. Now we can save and quit out of the file by pressing Ctrl +X then pressing Y and then Enter.

Now we need to restart Apache2 to force it to read in the updated configuration file. We can do that easily with the following command:

sudo service apache2 restart

4. You should now be able to restart your web browser and begin a new upload to see that the maximum upload size has been increased successfully.

Allowing the .htaccess override

Next, we need to deal with the .htaccess file for Nextcloud. Since we installed Nextcloud into the default Apache2 directory /var/www/html, we will need to change some settings in Apache2 to allow the .htaccess file to override settings.

1. To get started we can begin editing the file with the following command:

sudo nano /etc/apache2/apache2.conf

2. With the file now open we need to find the following block, you can also use Ctrl + W to help find the block.

<Directory /var/www/>        
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

To

<Directory /var/www/>        
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

3. Now we can save and quit out of the file by pressing Ctrl +X then pressing Y and then Enter.

4. Now we need to restart Apache2 to force it to read in the updated configuration file. We can do that easily with the following command:

sudo service apache2 restart

5. You can check whether the changes have successfully worked by going into the settings page on Nextcloud. Any warning about the .htaccess file not working correctly should now be gone.

Setting up SSL for Nextcloud

Now we should really work on setting up your Raspberry Pi Nextcloud server so that it runs through HTTPS and not plain HTTP. For this tutorial, we will assume that you do not have a domain name, so we will be generating our own self signed certificate and not utilizing one from a free service such as Letsencrypt.

1. Before we go modifying our Apache2 configuration we will first generate the self-signed certificate, luckily, we can do this all in one command thanks to OpenSSL.

Remember that a self-signed certificate will throw errors in your web browser and is not as secure as a properly signed certificate but it is better than nothing. It is also the only option if you’re not utilizing a domain name.

Before we generate the certificate, let’s first make a directory to store it.

sudo mkdir -p /etc/apache2/ssl

2. Now let’s generate the certificate itself by running the following command in the terminal:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:4096 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt

If you want to know exactly what these command arguments do, then read our little description below.

req: This specifies a subcommand for X.509 certificate signing request (CSR) management.

-x509: This option specifies that we want to make a self-signed certificate file instead of generating a certificate request.

-nodes: This tells the openssl application that we don’t want to specify a passphrase, a passphrase will require us to enter it every time Apache is restarted which is painful to deal with.

-days 365: This specifies the amount of days we want the certificate to remain valid for, after this amount of days you will have to generate a new certificate.

-newkey rsa:4096: This will create the certificate request and a new private key at the same time. You will need to do this since we didn’t create a private key in advance. The rsa:2048 tells OpenSSL to generate an RSA key that is 2048 bits long.

-keyout: This parameter names the output file for the private key file that is being created.

-out: This option names the output file for the certificate that we are generating.

After pressing enter you will be presented with the following options to fill out.

Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:

3. Once you have filled out all that information we can then proceed on with setting up Apache2 to run SSL and to also utilize our newly generated certificate. This is a simple process but an important one.

First let’s enable the SSL module for Apache with the following command:

sudo a2enmod ssl

4. Now we need to modify the default-ssl.conf file so it will utilize our new certificates and not the default ones that are generated by OpenSSL on installation.

To begin modifying this file run the following command:

sudo nano /etc/apache2/sites-available/default-ssl.conf

5. Within this file we need to change the two lines below to point to our new certificates we generated into our /etc/apache2/ssl folder.

Change

SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

To

SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key

6. Now we can save and quit out of the file by pressing Ctrl+X then pressing Y and then Enter.

7. We can now enable the default-ssl configuration and restart Apache to load in our new configuration. We can do this with the following two commands.

sudo a2ensite default-ssl.conf
sudo service apache2 restart

8. You can test to make sure this is working by going to your Raspberry Pi’s IP address with https:// in front of it. It will give you a warning about it potentially being an invalid certificate. This is normal as it is an unsigned certificate.

For instance to make sure my own copy of Nextcloud is now running behind SSL I would go to the following:

https://192.168.1.105/nextcloud

9 (Optional). An extra step to ensure that you have the best security for your Nextcloud setup is to enforce SSL so no connection can be made over HTTP, if a connection is made it will redirect you to HTTPS.

We can do this by making some changes to our apache configuration, to begin let’s edit the default file with the following command:

sudo nano /etc/apache2/sites-available/000-default.conf

10 (Optional). Replace all the text in this file with the code below. This will basically redirect all HTTP traffic to its HTTPs equivalent.

<VirtualHost *:80>
   ServerAdmin example@example

   RewriteEngine On
   RewriteCond %{HTTPS} off
   RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

11 (Optional). Now we can save and quit out of the file by pressing Ctrl +X then pressing Y and then Enter.

12 (Optional). Now before this will work we need to enable the redirect module and restart apache. We can easily achieve this by running the following two commands:

sudo a2enmod rewrite
sudo service apache2 restart

Now going to your Raspberry Pi on HTTP should automatically redirect to the HTTPS version. For example, if I go to http://192.168.1.105 it will redirect to https://192.168.1.105

Portforwarding Nextcloud

Finally, onto the section about port forwarding Nextcloud. We won’t go into too much depth on the ins and outs of port forwarding for your router but we will tell you what ports need forwarding. We will also mention what changes need to be made to Nextcloud for this to work.

Before we get started with this section you need to know that Nextcloud will only operate under specifically specified trusted domains. Which means you will need to either specify a domain name that you want to use for your connection or use your public IP address.

Since most home public IP addresses are dynamic you will need to look into setting up a dynamic DNS service, you will find our tutorial on how to setup a dynamic DNS service for your Raspberry Pi very handy.

1. To add your domain/IP we need to modify NextCloud’s configuration file, we can do that by running the following command:

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

2. Within this file you will see a block of text like below. This is an array of all trusted domains that you allow Nextcloud to operate through. For now, it should only include your Raspberry Pi’s local IP address. We will add our new domain/IP onto the end of this array.

'trusted_domains' =>
array (
    0 => '192.168.1.105',
),

For our example, we will be adding nextcloud.pimylifeup.com to the array. This means we need to increment the array ID and add the domain name. Once you have added a new one it should look something like below. Repeat this procedure for any new IP’s or domains you want Nextcloud to be able to operate through.

'trusted_domains' =>
array (
    0 => '192.168.1.105',
    1 => 'nextcloud.pimylifeup.com',
),

3. Now we can save and quit out of the file by pressing Ctrl+X then pressing Y and then Enter.

4. Finally you will need to port forward two ports to finally have Nextcloud up and running. These two ports being Port 80 and Port 443. The protocol required for these is TCP.

Hopefully by now you should have a fully operational Raspberry Pi Nextcloud Server. If you come across any issues or have some feedback related to this tutorial, then please don’t hesitate to leave a comment below.

26 Comments

Paul Cardelli on June 26th, 2017 at 3:06pm
Great write up, I wish this was around earlier. I actually built one of these recently, used a Pi3 with the new boot from USB Hard drive option enabled. Also used a Pi Zero with pi-hole installed for my local DNS/DHCP and on that system set the host file to point the DNS for example test.mooo.com to the local IP address, then dynamic public IP using DDNS I setup a cron job to update it with a bash script. So now my Nextcloud app on my devices only needs to be configured once and I don't waste my data plan while at home. This DNS trick is only needed if your ISP does not handle split horizon, in my case still just DSL in my area. Still looking at how to add an additional external hard drive to this configuration.
Alf Stockton on June 26th, 2017 at 3:07pm
Thanks for your article. In my case I do not want to create a new folder for nectcloud but to rather use an existing USB HDD that already contains a lot of data. Can this be done?
NoobPi on June 26th, 2017 at 5:39pm
Use php7 for better performance. :)
Joshua on July 3rd, 2017 at 12:13am
How would I update to Nextcloud 12.0.0 from 11.0.2??????? Thanks for this tutorial I've been waiting for it.
Andrew Marshall on July 5th, 2017 at 4:08am
if I port forward my port 80 I lose access and how would we update to the latest version please
Gregor on July 15th, 2017 at 7:16am
if I change the location of the data folder on a mounted usb hard drive i get a error from nextcloud and i should change the permissions on 0770 of data folder on the hard drive but i can't the permissions on the hard drive, can you help me ?
carlos on July 15th, 2017 at 8:06am
How can i add my my hard drive to have more storage ,so that all my files are on my hardrive when i upload them
Paul on July 21st, 2017 at 8:18am
Thanks for the tutorial. I've never set up a server and I'm stuck on step 8 of Installing Nextcloud. The browser displays: This version of Nextcloud requires at least PHP 5.6.0 You are currently running 5.4.45-0+deb7u8. Please update your PHP version. I don't know how to upgrade php, and i'm not finding anything online that works
BobW on August 12th, 2017 at 10:46am
Did you do a sudo apt-get update and an sudo apt-get upgrade?
Luiz Fernandes on July 25th, 2017 at 10:08am
Great article, thanks. I'm a beginner in linux, I followed your article, I got to run but it shows the following warning in the admin panel. "The "Strict-Transport-Security" HTTP header is not configured to at least "15552000" seconds. For enhanced security we recommend enabling HSTS as described in our security tips.". Maybe I performed some mistake, how to fix this warning?
Rahim K on August 7th, 2017 at 1:10am
Hey Luiz, try this Append the following to /var/www/html/nextcloud/.htaccess Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains" Then enable the headers mod in apache: a2enmod headers service apache2 restart
George on August 15th, 2017 at 10:59am
Hi Gus, I love your guides, they're always great but I can't get this one to work. When I type in my.ip/next/cloud, it brings me to a blank page and auto downloads a file named index.php Any advice?
WarHawk (Brian C) on August 23rd, 2017 at 6:22pm
Nice! Would there be a way to use lighttpd instead of Apache2? Mainly because it is so much lighter and can do the same thing as Apache2... Thanks for the how to!
Carl R. on October 2nd, 2017 at 11:31am
Great work ! However I am stuck on Instruction 8 of Setting up Nextcloud. When I try to set up nextcloud (192.168.0.51/nextcloud) I get the response - No such file or directory. My IP address is correct and Apache is running ok. The index.html and index.php files are in the nextcloud directory. Any ideas ?
Carlos on October 3rd, 2017 at 9:57am
I give up for the second time of trying to make owncloud (now called nextcloud) run on a Raspberrypi B. It's a waste of time as the performance is disappointing. Perhaps it should be noted at the beginning of this article.
Gus on October 3rd, 2017 at 1:09pm
It is noted in the equipment list that it is recommend that you use a Raspberry Pi 2 or 3. Also, Nextcloud is built off the same code of Owncloud but they're not the same company. Owncloud still exists and operates under Owncloud.
Hampton on October 24th, 2017 at 6:10am
I tried to follow this but when I entered this code sudo apt-get install php5 php5-gd sqlite php5-sqlite php5-curl I got this E: Package 'php5' has no installation candidate E: Package 'php5-gd' has no installation candidate E: Package 'php5-sqlite' has no installation candidate E: Package 'php5-curl' has no installation candidate
Gus on October 24th, 2017 at 4:34pm
Are you running Raspbian stretch ? This might be an issue with that release, i'll need to look into it.
J.T. on October 27th, 2017 at 2:34pm
I replace "5" with "7.0" and installed anything else that was missing with PHP 7.0. All is good!
Danny on October 29th, 2017 at 5:25am
In step 8, when I try to access myip/nextcloud from the browser the php code is shown instead of being executed