Social Icons

How to setup your own cloud on Raspberry Pi


With growing concerns of data privacy across the world, one thing is established for sure, if you want to protect your sensitive data then do it yourself, keep it with you, safe and secure. While there is practically nothing invincible, we must try to safeguard our personal data to the extent possible, atleast to make eavesdropper's life tougher in breaking it. This can help you to get rid of using services like Dropbox, Box, Google Drive. I would like to clarify that I do not intend (or mean to intend) that commercial or non-commercial services named above does give away your data, but following this guide can help you to stay away from them at your own risk, the data will be with you at your disposal with the benefits of cloud computing.

Enough talking... Let go ahead and see how we can setup a personal cloud on a small and tiny Raspberry Pi. We would be using the following softwares broadly to setup our own cloud:
  • ownCloud (open-source enterprise grade cloud software)
  • Nginx (open-source, high-performance HTTP server)
  • OpenSSL (open-source tool kit for SSL/TLS)
  • php5 (free of charge, server side scripting language)
  • Raspbian Wheezy from rapberrypi.org (not ownCloud's version)

Important note: This guide was prepared for OwnCloud 5.0.20
The latest version OwnCloud 7 doesn't work well with this guide. 
If you are going to install OwnCloud 7 on Raspbian Wheezy follow here

1. Tuning up the Raspberry Pi
Following changes needs to be made under Raspberry Pi configuration
sudo raspi-config

a. Expand the root filesystem to have enough space for the cloud
    Select "Expand Filesystem"

b. Change locale to en_US.UTF8
    Select "Internationalisation  Options"

c. Memory split, allocate 16M to video graphics (if you are not using X-server)
    Select "Advanced Options" --> "Memory Split"

d. Overclock to Modest or Medium
    Select "Overclock" (You might have to reboot if asked)

2. Updating the package lists on Raspberry Pi
    sudo apt-get update
    sudo apt-get upgrade

3. Creating users
    sudo groupadd www-data
    sudo usermod -a -G www-data www-data

4. Installing the packages
    sudo apt-get install nginx openssl ssl-cert php5-cli php5-sqlite php5-gd php5-curl php5-common php5-cgi sqlite3 php-pear php-apc curl libapr1 libtool curl libcurl4-openssl-dev php-xml-parser php5 php5-dev php5-gd php5-fpm memcached php5-memcache varnish

5. Creating your SSL certificates for 2 years
    sudo openssl req $@ -new -x509 -days 730 -nodes -out /etc/nginx/cert.pem -keyout /etc/nginx/cert.key

   
sudo chmod 600 /etc/nginx/cert.pem
   
sudo chmod 600 /etc/nginx/cert.key

6. Configuring Ngnix web server
    sudo nano /etc/nginx/sites-available/default

    Copy and replace the entire content below:
    Note: You'll have to replace 192.168.XXX.XXX with the local IP of your Raspberry Pi. If you have planned instead to use any Dynamic DNS domain, then use your  domain name instead of the local IP address. You can follow our guide for setting up a Dynamic DNS domain.
   

7. Configuring max upload limit in php
    sudo nano /etc/php5/fpm/php.ini

    Tip: Use ctrl+w to search below lines and update:
    upload_max_filesize = 1000M
    post_max_size = 1000M

    At the end of the file, we'll add also the following:
    upload_tmp_dir = /srv/http/owncloud/data 
    extension = apc.so
    apc.enabled = 1
    apc.include_once_override = 0
    apc.shm_size = 256

    Next, we create the folder with the associated rights
    sudo mkdir -p /srv/http/owncloud/data
    sudo chown www-data:www-data /srv/http/owncloud/data

8. Configuring PHP 
    sudo nano /etc/php5/fpm/pool.d/www.conf
    Change the following line from:
    listen = /var/run/php5-fpm.sock
    to 
    listen = 127.0.0.1:9000

    sudo nano /etc/dphys-swapfile
    Change the following line from:
    CONF_SWAPSIZE=100
    to 
    CONF_SWAPSIZE=512

9. Restart web server and Php
    sudo /etc/init.d/php5-fpm restart
    sudo /etc/init.d/nginx restart

10. Install ownCloud (version 5.0.11 used here)
    sudo mkdir -p /var/www/owncloud
    sudo wget http://download.owncloud.com/download/5.0.11/owncloud_enterprise-5.0.11.tar.bz2
    sudo tar xvf owncloud_enterprise-5.0.11.tar.bz2
    sudo mv owncloud/ /var/www/
    sudo chown -R www-data:www-data /var/www
    rm -rf owncloud owncloud-5.0.7.tar.bz2

11. Setup admin account
    Now in the browser, login to the ip address of Raspberry Pi or if you have configured your domain name then use that and set up the admin account.

12. Linking your external USB hdd as the storage instead of tiny SD card (Optional)
    Ideally one should opt for using an external storage device since the SD card from which Raspberry Pi runs is barely sufficient to handle your cloud storage. You can follow our guide on How to auto-mount USB HDD. Next you need to follow the below steps to create a directory in USB hdd to hold the ownCloud data folder. Note: The mounted path to USB hdd is taken as /media/USBHDD here, change it accordingly.
    sudo service nginx stop
    sudo mkdir /media/USBHDD/OwnCloud5
    sudo chown www-data:www-data /media/USBHDD/OwnCloud5
    sudo mv /var/www/owncloud/data/ /media/USBHDD/OwnCloud5/


    Configure USB directory path in Nginx config.php
    sudo nano /var/www/owncloud/config/config.php

   Change the below line
   'datadirectory' => '/var/www/owncloud/data'
    to
   'datadirectory' => '/media/USBHDD/OwnCloud5/data'

    sudo service nginx start

13. For USB hdd with NTFS filesystem [This step was required for OwnCloud version 5.0.20, later versions probably fixed this and this step won't be required then]
    You might get the following warning on the login page of ownCloud.

Data directory  /media/USBHDD/OwnCloud5/data') is readable for other users. Please change the permissions to 0770 so that the directory cannot be listed by other users.

    To fix this, comment out the section in /var/www/owncloud/lib/util.php which checks for directory permissions.
    sudo nano /var/www/owncloud/lib/util.php

public static function checkDataDirectoryPermissions($dataDirectory) {
...
...
/*
                else {
                        $permissionsModHint = 'Please change the permissions to 0770 so that
the director$
                                .' cannot be listed by other users.';
                        $prems = substr(decoct(@fileperms($dataDirectory)), -3);
                        if (substr($prems, -1) != '0') {
                                OC_Helper::chmodr($dataDirectory, 0770);
                                clearstatcache();
                                $prems = substr(decoct(@fileperms($dataDirectory)), -3);
                                if (substr($prems, 2, 1) != '0') {
                                        $errors[] = array('error' => 'Data directory
('.$dataDirectory.')$
                                                'hint' => $permissionsModHint);
                                }
                        }
                }
*/
                return $errors;
        }

That's all... Celebrate the fun... Fly high on clouds... and if you liked this guide, got benefited or feel worth reading kindly do share with your network.

26 comments:

  1. Hi,

    i need your help. i want to setup cloud storage using raspberry pi. i follow instruction on techjawab. but i stuck on the last step which is linking to usb externel. i stuck at 'sudo chown www-data:www-data /media/USBHDD/OwnCloud5'. It give an error. operation not permitted. Please help me !

    ReplyDelete
  2. In step 8: sudo nano /etc/dphys-swapfile
    Change the following line from:
    CONF_SWAPSIZE=100
    to
    CONF_SWAPSIZE=512

    The file shows up blank, is it suposed to? I'm using raspbmc, thanks.

    ReplyDelete
  3. Try 'sudo chown -R www-data:www-data /media/USBHDD/OwnCloud5' instead, since it's a folder, not a file.

    ReplyDelete
  4. Thank you so much for this manual. I have one question. I have managed to set up everything correctly up to step 11 admin account. When I open up the owncloud page I get an error stating that the .htacces file isn't functioning correctly and thereby exposing my files to the internet.
    Can someone help me with this?

    ReplyDelete
  5. hi, thanks for the detail in setting up, however i got some problem of setting up the ssl cert as i don't have it. i tired twice but i have no luck when i come to step 10 with no admin page loaded. any idea how to fix this?

    ReplyDelete
    Replies
    1. You should be able to create SSL certs as mentioned in step 5. Make sure you have followed all the steps sequentially. Let me know the exact error message while you can't create SSL cert to be able to help you.

      Delete
  6. Hello,

    Thank you for this guide, I have 1 question.
    I get a error when restarting the nginx service:
    # [emerg] unknown directive " " in /etc/nginx/sites-enabled/default:7

    ReplyDelete
  7. Hi, thanks for the guide but I have a problem.
    Every time I try to do step 11 I just get a 404 error.
    Any ideas for a fix?
    Thanks
    -Gazza

    ReplyDelete
    Replies
    1. Can you confirm if you nginx webserver is running fine... do you see any errors in logs ?

      Delete
  8. Do you have any Problems accessing your server, if the harddrive spins down? I think this has been my problem, because after navigating in terminal to that direction, everything worked, again.

    ReplyDelete
    Replies
    1. I am sure my external HDD spins down due to inactivity but never faced any issues accessing the server. Logically my server is running from the SD card on RasPi, it's only the storage that's on external HDD, so it should not cause any accessing issue even if HDD spins down.

      Delete
    2. Restarting nginx: nginx: [emerg] SSL_CTX_use_certificate_chain_file("/etc/ssl/localcerts/oc.pem") failed (SSL: error:02001002:system library:fopen:No such file or directory error:20074002:BIO routines:FILE_CTRL:system lib error:140DC002:SSL routines:SSL_CTX_use_certificate_chain_file:system lib)
      nginx: configuration file /etc/nginx/nginx.conf test failed

      Delete
  9. I have followed above instructions but unable to see the owncloud config page.
    my.domain.com/owncloud takes me to the page states "Welcome to nginx!"..
    Can somebody help?

    ReplyDelete
    Replies
    1. If you are following this guide exactly then you should be heading to my.domain.com (no need to append the "/owncloud" part). Please make sure you're following each steps in right order.

      Delete
  10. when I restart the nginx i am getting below msg.

    Restarting nginx: nginx: [emerg] SSL_CTX_use_certificate_chain_file("/etc/ssl/localcerts/oc.pem") failed (SSL: error:02001002:system library:fopen:No such file or directory error:20074002:BIO routines:FILE_CTRL:system lib error:140DC002:SSL routines:SSL_CTX_use_certificate_chain_file:system lib)
    nginx: configuration file /etc/nginx/nginx.conf test failed

    ReplyDelete
    Replies
    1. Apparently it looks like that your system is having some issues with the SSL certificate you have created. Kindly re-try creating and setting up the SSL certificate, make sure all per-requisites mentioned here are installed before creating the certs and installing them.

      Delete
  11. First of all I'd like to tell everyone that this is a very good tutorial. Only problem that I have is at step 13. I get the error message but when I open /var/www/owncloud/lib/util.php the file "util.php"is blank. There is no text that I can uncomment. Please help.

    ReplyDelete
  12. I re-installed the OS and now able to run owncloud 6.0.2 However at the step 13 to make the correction the file /var/www/owncloud/lib/util.php is not found.
    There is no file called util.php in the lib folder.

    ReplyDelete
    Replies
    1. This guide was written with OwnCloud version as 5.0.20
      Looks like in latest versions they have fixed the issue that we used to handle manually in older version. If everything is working as good... Just enjoy! :)

      Delete
  13. Hello, thank you for writing this detailed tutorial to help newbies like me.

    I am stuck at Step 9. When I type sudo /etc/init.d/nginx restart, I get Restarting nginx: nginx: [emerg] "location" directive is not allowed here in /etc/nginx/sites-enabled/default:61
    nginx: configuration file /etc/nginx/nginx.conf test failed

    And when I tried Step 10, sudo wget http://download.owncloud.com/download/5.0.11/owncloud_enterprise-5.0.11.tar.bz2
    I get: http://download.owncloud.com/download/5.0.11/owncloud_enterprise-5.0.11.tar.bz2
    Resolving download.owncloud.com (download.owncloud.com)... 144.76.105.220, 50.30.33.233
    Connecting to download.owncloud.com (download.owncloud.com)|144.76.105.220|:80... connected.
    HTTP request sent, awaiting response... No data received.

    Do you know what might be the problem? I am using Raspbian Wheezy with a wired connection to my router, and on Putty on a Win7 machine.

    Thanks!

    - Jules

    ReplyDelete
    Replies
    1. It appears that we are in the most common pitfall of OpenSource applications. OwnCloud might have changed a bit of their download location and config files. I will soon attempt to do a fresh install on a Raspbian Wheezy and update this guide, untill then it would be tough to comment on such issues.

      Delete
  14. Hi,
    In step 6, what content are we replacing and with what?
    Thank you

    ReplyDelete
  15. After step 12 it says "[14] SQLSTATE[HY000] [14] unable to open database file" when I try to login at the owncloud page. Any suggestions?

    /Totally Noob

    ReplyDelete
  16. Hi thanks for your great tutorial! Unfortunately I have a problem: when creating the direktory /srv/http/owncloud/data I get the reply "cannot create directory blabla - no space left on device". However there is plenty of space left on my 64 GB card. Any idea what the reason is? I have no idea what to do...

    ReplyDelete
  17. I have loved this guide, and I am really grateful as I have now set up my ownCloud server with a dynamic dns address, yet I have never had much experience with Linux.

    I have only one question, about the following line:

    sudo openssl req $@ -new -x509 -days 730 -nodes -out /etc/nginx/cert.pem -keyout /etc/nginx/cert.key

    what happens after the 2 years expire? Could it be added in to automatically renew after 23 months using cron?

    Thanks

    ReplyDelete
  18. Re Step 12 above.
    How to proceed, please anyone, if the USBHDD has 2 partitions of different size.

    ReplyDelete