Private Cloud Server on Raspberry Pi
Tags: linux, computerscience, discuss, serverless
Published: 2020-10-20 19:41:02

This project is all about Cloud storage, on your Raspberry Pi for free forever. Yes, you heard it right it’s free forever, forget about paying your Cloud storage provider any more money.

raspberry pi_image_pixabay
Image Coutesy – Image by Kevin Partner from Pixabay
We all know how important our data is nowadays, so thinking about 

This tutorial is all about setting up your own Private Cloud Server on your own Network using Raspberry Pi.

You can learn more about OwnCloud at their website at Owncloud.org.

Equipments
Raspberry Pi 2/3/4
SD Card / Micro SD (If you are using Pi 2 or B+)
Ethernet Cable or WiFi Dongle (Not required for Pi 3 or 4)
External HardDrive or SSD or USB Drive with your desired storage capacity of the server.
Setting up Owncloud Server on Raspberry Pi
We need a Raspberry Pi with Raspbian OS installed, also if you haven’t installed Raspbian OS, you can check tutorial on the same.

We are going to use NGINX Web server and Owncloud for this tutorial.

Install NGINX Web Server and PHP on Raspberry Pi
We need to install NGINX webserver and PHP in order to further continue with the tutorial.

Owncloud requires both NGINX and PHP in order to work properly.

Step 1: Using Command Line, write the following piece of code for updating and upgrading the Raspberry Pi and the packages:

sudo apt-get update
sudo apt-get upgrade
Step 2: We need to add “www-data” user to the “www-group”.

We can do so by typing in this piece of code on the Command Line:

sudo usermod -a -G www-data www-data
Step 3: Now you need to install the packages for NGINX, PHP, SSL, any many more.

Type the following lines on your Command line for installing the above mentioned packages:

sudo apt-get install nginx openssl ssl-cert php7.3-xml php7.3-dev php7.3-curl php7.3-gd php7.3-fpm php7.3-zip php7.3-intl php7.3-mbstring php7.3-cli php7.3-mysql php7.3-common php7.3-cgi php7.3-apcu php7.3-redis redis-server php-pear curl libapr1 libtool libcurl4-openssl-dev
Setting up NGINX for Owncloud and installing SSL for HTTPS protocol
We need to configure the NGINX webserver and also install SSL certificate for HTTPS connections in your cloud server.

Step 1: We need to create an SSL certificate, for that you can run the following command:

sudo openssl req $@ -new -x509 -days 730 -nodes -out /etc/nginx/cert.pem -keyout /etc/nginx/cert.key
Step 2: We need to add a custom “dhparam” file that will ensure that our SSL connection in the server is secured. Generating a “dhparam” file is very easy but can take a lot of time, you just need to type in the following command:

sudo openssl dhparam -out /etc/nginx/dh2048.pem 2048
Step 3: CHMOD the three cert file that got generated by Step 2 by using the following commands:

sudo chmod 600 /etc/nginx/cert.pem
sudo chmod 600 /etc/nginx/cert.key
sudo chmod 600 /etc/nginx/dh2048.pem
Step 4: Clearing the server config file, we will be creating our own version of it.

sudo sh -c "echo '' > /etc/nginx/sites-available/default"
Step 5: Add the configuration file by using the following command:

sudo nano /etc/nginx/sites-available/default
Step 6: Just copy paste the following code in the file that will open upon entering Step 5:

upstream php-handler {
    server unix:/var/run/php/php7.3-fpm.sock;
}
server {
    listen 80;
    server_name _;
    #Allow letsencrypt through
    location /.well-known/acme-challenge/ {
        root /var/www/owncloud;
    }
    # enforce https
    location / {
        return 301 https://$host$request_uri;
    }
}
  
server {
    listen 443 ssl http2;
    server_name _;
  
    ssl_certificate /etc/nginx/cert.pem;
    ssl_certificate_key /etc/nginx/cert.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:AES256+EDH';
    ssl_dhparam /etc/nginx/dh2048.pem;
    ssl_prefer_server_ciphers on;
    keepalive_timeout    70;
    ssl_stapling on;
    ssl_stapling_verify on;
  
    add_header Strict-Transport-Security "max-age=15552000; includeSubDomains; preload" always;
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header X-Download-Options noopen;
    add_header X-Permitted-Cross-Domain-Policies none;
  
    root /var/www/owncloud/;
  
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
  
    # The following 2 rules are only needed for the user_webfinger app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
  
    location = /.well-known/carddav {
        return 301 $scheme://$host/remote.php/dav;
    }
    location = /.well-known/caldav {
        return 301 $scheme://$host/remote.php/dav;
    }
  
    # set max upload size
    client_max_body_size 512M;
    fastcgi_buffers 8 4K;
    fastcgi_ignore_headers X-Accel-Buffering;
  
    gzip off;
  
    error_page 403 /core/templates/403.php;
    error_page 404 /core/templates/404.php;
  
    location / {
        rewrite ^ /index.php$uri;
    }
  
    location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
        return 404;
    }
    location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
        return 404;
    }
  
    location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+|core/templates/40[34])\.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param HTTPS on;
        fastcgi_param modHeadersAvailable true;
        fastcgi_param front_controller_active true;
        fastcgi_read_timeout 180;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off; #Available since NGINX 1.7.11
    }
  
    location ~ ^/(?:updater|ocs-provider)(?:$|/) {
        try_files $uri $uri/ =404;
        index index.php;
    }
  
    location ~ \.(?:css|js)$ {
        try_files $uri /index.php$uri$is_args$args;
        add_header Cache-Control "max-age=15778463";
        add_header Strict-Transport-Security "max-age=15552000; includeSubDomains";
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Robots-Tag none;
        add_header X-Download-Options noopen;
        add_header X-Permitted-Cross-Domain-Policies none;
        access_log off;
    }
    location ~ \.(?:svg|gif|png|html|ttf|woff|ico|jpg|jpeg|map)$ {
        add_header Cache-Control "public, max-age=7200";
        try_files $uri /index.php$uri$is_args$args;
        access_log off;
    }
}
Step 7: Save and exit the file by using CTRL + X and then pressing Y, followed by ENTER.

Step 8: Now just restart the services of NGNIX Web server by typing in the following command:

sudo systemctl restart nginx
Setting up PHP for OwnCloud
We need to setup own PHP for the proper functionality of OwnCloud in our Raspberry Pi

Step 1: Open PHP Config file by entering.

sudo nano /etc/php/7.3/fpm/php.ini
Step 2: We need to update few lines in the PHP Config file we can do so by searching (CTRL + W)

Find

upload_max_filesize = 2M
Replace With

upload_max_filesize = 2000M
Find

post_max_size = 8M
Replace With

post_max_size = 2000M
Step 3: Once done, save and then exit by pressing CTRL + X, followed by Y, then ENTER.

Step 4: We need to make some changes in the PHP-fpm pool configuration as well, we can do so by running the following command to modify the configuration file.

sudo nano /etc/php/7.3/fpm/pool.d/www.conf
Step 5: Find the following block of codes and replace it with the following:

Find

;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp
Replace With

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
Step 6: save the file by pressing CTRL + X, followed by Y, then ENTER.

Adding Swap Memory in Raspberry Pi
We need to add some Swap memory in our system. The swap memory allows the Raspberry Pi to work further more beyond it’s memory by using the space on the storage that it has, but the only downside is it’s slower than actual RAM.

Step 1: Increase the amount of SWAP Memory by modifying the “dphys-swapfile” file by using the following command.

sudo nano /etc/dphys-swapfile
Step 2: Find the following and replace it with the line given below.

Find

CONF_SWAPSIZE=100
Replace With

CONF_SWAPSIZE = 512
Step 3: You can save and quit by pressing CTRL + X, followed by Y and pressing ENTER.

You have now successfully added a SWAP Memory of 512MB in your Raspberry Pi.

Step 4: You need to restart your Raspberry Pi for the SWAP memory effect to take place, you can do so by using the following command.

sudo reboot
Setting Up MySQL Database for OwnCloud
Assuming that you have already setup a MySQL Server on your Raspberry Pi, you need to follow some simple steps for setting up MySQL Database in your Raspberry Pi.

Step 1: You need to use the MySQL command-line interface (CLI) by loading it up by the following command.

sudo mysql -u root -p
Step 2: Once you are into the MySQL CLI, you can create a database. We will be calling our database “ownclouddb”. We can create this database by running the following command on the CLI of MySQL.

CREATE DATABASE ownclouddb;
Step 3: Now let’s create an user that will interact with the Database, we can create an user called “ownclouduser” by running the following command and also add your own PASSWORD for the securing it.

CREATE USER 'ownclouduser'@'localhost' IDENTIFIED BY '[PASSWORD]';
Note:- You can replace the [PASSWORD] with your own desired password, but do take a note of it.

Step 4: Now we need to add permissions to the new user so that it can get all the privileges on the system, we can do so by granting the privileges by running the command below.

GRANT ALL PRIVILEGES ON ownclouddb.* TO 'ownclouduser'@'localhost';
Step 5: The final step involves flushing all privileges, it’s important since if we don’t do this the server won’t accepted the changes that we made during the creation of the database.

Run the following command for flushing the privileges.

FLUSH PRIVILEGES;
Once the privileges are flushed, now comes the fun part where we install and setup the Owncloud software on our Raspberry Pi.

Downloading and Installing OwnCloud on Raspberry Pi
Once we are done with all the steps above, we can finally download and install OwnCloud on our Raspberry Pi.

Step 1: Change in to the directory that we will be running everything on by using the following command.

cd /var/www/
Step 2: We need to download the latest version of OwnCloud by using the wget function in our command line.

sudo wget https://download.owncloud.org/community/owncloud-latest.tar.bz2
Step 3: Now extract the downloaded file by using tar.

sudo tar -xvf owncloud-latest.tar.bz2
Step 4: With everything done, we need to make sure that “www-data” owns the files, we can recursively modify the permissions of the files that got downloaded by using the “chown command”.

sudo chown -R www-data:www-data /var/www
Step 5: We need to open the .user.ini file to enforce some of the changes that we made earlier. We can do so by using the following command.

sudo nano /var/www/owncloud/.user.ini
Step 6: Update the following values to 2000M or as much as you want to.

upload_max_filesize=2000M
post_max_size=2000M
memory_limit=2000M
Step 7: Now let’s connect to OwnCloud using our Raspberry Pi’s IP Address, we will be able to see the admin page.

But we need to add and mount an external drive so that we have enough space for the server and your files to be stored.

Mounting and Setting up Hard disk on Raspberry Pi
Setting up an external Hard disk drive on Raspberry Pi can be quiet challenging by we are going to help you with that, you can just go through the tutorial below for an easy mount and installation of external hard drive on Raspberry Pi.

Step 1: We need to install NTFS package by typing in the following command.

sudo apt-get install ntfs-3g
Step 2: We need to make a directory that we can mount on.

sudo mkdir /media/ownclouddrive
Step 3: We need to get the GID, UID and UUID, we can get them by entering the following command for GID.

id -g www-data
id -u www-data
And also if we get UUID of HDD, the Raspberry Pi will never forget this drive even if you plug the HDD in another port of the Pi.

ls -l /dev/disk/by-uuid
Now we will get to see LIGHT BLUE LETTERS AND NUMBERS, copy them from the last entry (../../sda1 or something like this).

Step 4: Add the drive to the fstab file so that that it boots with proper permissions.

sudo nano /etc/fstab
Step 5: Now add the following line in the BOTTOM of the file, updating it with the values that we get from Step 3.

UUID=DC72-0315 /media/ownclouddrive auto nofail,uid=33,gid=33,umask=0027,dmask=0027,noatime 0 0
Step 6: Restart the Raspberry Pi, and the hard drive will be automatically mounted.

If you get any error then check for Step 3 and Step 4 properly and implement it, until it’s fixed.

Setting up OwnCloud on Raspberry Pi
Atlast, after covering so much details and setups for this final goal we have come to the last but not the least Setup of OwnCloud on Raspberry Pi.

Step 1: We need to go to our Raspberry Pi’s IP Address, you will get a certificate error. Don’t panic, you just need to click on “Show Advanced” button and click on “Proceed to IP Address (unsafe)”.

owncloud_setup
Image Coutesy – LiquidWeb.com
Step 2: You will be greeted with OwnCloud Admin page, here you need to do some step to finally getting into the dashboard of your Cloud server.

Step A: The first thing you need to do is specify a username and password for your Owncloud admin account.

Step B: Next, we need to bring up the storage and database settings. You can do this by clicking the “Storage & database” dropdown.

Step C: If you are using a different data folder, you can specify it now by using the Data folder textbox.

Step D: We then need to bring up the MySQL database options. You can find these by clicking the MySQL/MariaDB toggle.

Step E: Next, we need to fill out three bits of information, the database user, the password for that user, and the database name.

First, you need to specify the “Database user”. If you are following this guide, this should be “ownclouduser”.
The second option you will need to specify the password you set for the above user.
Finally, we need to set the database name. If you have used the ones from this tutorial, you should set this to “ownclouddb”.
Once you have finished with all the settings, click the Finish setup button.
After all of these steps above you have finally installed and setup your own Private Cloud Server on Raspberry Pi.

You can check the whole tutorial again on <a href="https://rpiproject.com/">RPi Project</a>