[GUIDE] OwnCloud with RaspberryPi 3 and FreeNAS

OwnCloud with RaspberryPi 3 and FreeNAS

This guide is intended for the installation of OwnCloud on a RaspberryPi 3 with a FreeNAS server as the storage backend.

I am aware this is rather backward as FreeNAS has an OwnCloud plug-in you can install but I wanted to see if it was possible.

Table of contents

  1. Initial preperation
  2. MySQL configuration
  3. Prepare the storage
  4. OwnCloud installation
  5. Server tuning
  6. Credits
  7. Success

Requirements

  1. Raspberry Pi 3
  2. Raspberry Pi 3 compatible power supply
  3. Raspberry Pi 3 case
  4. MicroSD card
  5. Ethernet cables
  6. Four port switch or better
  7. FreeNAS server (expensive I know)

The RaspberryPi 3 can be configured using my guide here.

Objective

The object is to have the RaspberryPi act as a web front end for the backend storage. Their are guides on doing this with external USB drives but a FreeNAS server with ZFS and RAID struck me as a more sensible storage solution.

The storage must be totally transparent to the user (me). I don't want to have to rely on the "external" storage module that comes with OwnCloud as these all end up with seperate folders inside your view of OwnCloud. If this was the case and I forgot to upload something to the right folder I could exhaust the 16GG MicroSD card on the RaspberryPi very fast.

As stated elsewhere in the guide my FreeNAS box is not in its "final form" yet. But once it is and I have an offsite backup in place I will replace Dropbox and Google drive with this solution.

This is part of my ongoing project to take control of my data and information from the technology giants.

1 Like

Initial preperation

In this scenario the server is headless and is accessed via SSH. At this point I have run through my own RaspberryPi 3 setup guide and amended the IP tables to allow access over ports, 80, 443 and 3306. I then turn off the Pi, remove the MicroSD card and make a back with the following command where /dev/sdf is the mount location for the card on your PC.

sudo dd bs=4M if=/dev/sdf | gzip > raspberrypi.img.gz

and if you balls up the install later on you can restore with

sudo gunzip --stdout raspberrypi.img.gz | dd bs=4M of=/dev/sdf

First install all the required packages via the command below. You will be asked for the root password for the installation of MySQL at some point.

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

Now create an SSL certificate. We can get a "real" one later.

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

Answer the prompts and then run the commands below to se the correct permissions.

sudo chmod 600 /etc/nginx/cert.pem

sudo chmod 600 /etc/nginx/cert.key

Now remove contents of the nginx config file with the command below

sudo sh -c "echo '' > /etc/nginx/sites-available/default"

Now open it up with

sudo nano /etc/nginx/sites-available/default

And paste in the next below. Be sure to change the server IPs to the one for your server (mine is 192.168.0.7) and amend the certificate paths and names if they are different.

upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}
server {
    listen 80;
    server_name 192.168.0.7;
    return 301 https://$server_name$request_uri;  # enforce https
}

server {
    listen 443 ssl;
    server_name 192.168.0.7;
    ssl_certificate /etc/nginx/cert.pem;
    ssl_certificate_key /etc/nginx/cert.key;
    # Path to the root of your installation
    root /var/www/owncloud;
    client_max_body_size 1000M; # set max upload size
    fastcgi_buffers 64 4K;
    rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
    rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
    rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
    index index.php;
    error_page 403 /core/templates/403.php;
    error_page 404 /core/templates/404.php;
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README) {
        deny all;
    }
    location / {
        # The following 2 rules are only needed with webfinger
        rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
        rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
        rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
        rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
        rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
        try_files $uri $uri/ index.php;
    }
    location ~ \.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param HTTPS on;
        fastcgi_pass php-handler;
   }
   # Optional: set long EXPIRES header on static assets
   location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
        expires 30d;
        # Optional: Don't log access to assets
        access_log off;
   }
}

Now edit the php.ini file and change the upload sizes to whatever you like. Use "Ctrl-W" to search the file.

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

upload_max_filesize = 1024M
post_max_size = 1024M

Now edit the FPM PHP config file with

sudo nano /etc/php5/fpm/pool.d/www.conf

and change the listening port as below.

listen = 127.0.0.1:9000

Now set your swap space value in the below file

sudo nano /etc/dphys-swapfile

CONF_SWAPSIZE = 512

MySQL configuration

The purpose of these changes is to allow you to connect to your MySQL instance from MySQL Workbench running on your PC.

Edit the following file and amend the bind address as shown then exit.

sudo nano /etc/mysql/my.cnf

bind-address = 0.0.0.0

Login as root MySQL user and allow connection from the IP of your client workstation.

mysql -u root -p

Enter password: mysqlrootpassword

mysql> GRANT ALL PRIVILEGES ON . TO root@'ipyouwantconnectfrom' IDENTIFIED BY 'mysqlrootpassword' WITH GRANT OPTION;

WARNING: There should be an asterix in the above command either side of the . between ON and TO but Markdown keeps making them invisible.

mysql>FLUSH PRIVILEGES;

mysql>exit

sudo service mysql restart

Now check you can connect to MySQL from SQL workbench on port 3306 using the user "root" and the password you chose eariler.

Now create a blank database.

Prepare the storage

So on the Pi once again run the following commands. To be honest I am not sure if the first command is needed but it is something I ran while troubleshooting so I thought I better put it in.

sudo usermod -a -G www-data www-data

What is needed are the following commands.

So I have a FreeNAS server and I have a single volume called /mnt/Pool

On this volume I have a single NFS share called FreeNAS

So in order to mount the storage on the RaspberryPi I have to make sure I have nfs-common installed with

sudo apt-get install nfs-common

Now create your mount point with

sudo mkdir /mnt/FreeNAS

These to commands make sure its is conntected after a reboot

sudo update-rc.d rpcbind defaults

sudo update-rc.d rpcbind enable

and then edit the fstab file with

sudo nano /etc/fstab

and add the following line

freenas:/mnt/Pool /mnt/FreeNAS nfs defaults,noatime,noauto,x-systemd.automount 0 0

Please bear in mind that the Pi has a hosts entry for the FreeNAS server just called freenas. This is why there is a name at the beginning of the line rather than an IP address.

This line will mount the FreeNAS storage on the mount point FreeNAS.

Now reboot the Pi with

sudo shutdown -r now

And log back in then move down into the mount point.

cd /mnt/FreeNAS

ls -l

You should see at least a directory called "jails"

You can also check if the storage has mounted with

df -h

you should see something like this

Filesystem                              Size  Used Avail Use% Mounted on
/dev/root                                15G  4.6G  9.4G  33% /
devtmpfs                                459M     0  459M   0% /dev
tmpfs                                   463M     0  463M   0% /dev/shm
tmpfs                                   463M   12M  451M   3% /run
tmpfs                                   5.0M  4.0K  5.0M   1% /run/lock
tmpfs                                   463M     0  463M   0% /sys/fs/cgroup
freenas:/mnt/Pool  		       2.1T  321G  1.8T  15% /mnt/FreeNAS
/dev/mmcblk0p1                           63M   21M   43M  34% /boot
tmpfs                                    93M     0   93M   0% /run/user/1001

And you see 2.1TB of storage on the mount point. My FreeNAS server is not in its "final form" yet. I have six shitty 500GB non NAS type hard disks which I got out the technology recycling bin at work before they were crushed. Cost = £0

Now the next point is critical. All the storage processes on OwnCloud run under the user www-data. This user MUST be the owner of the folder where you will store OwnCloud data.

Now make the directory with

sudo -u www-data mkdir owncloud

OwnCloud installation

Now we want to install OwnCloud itself. Run the following commands to create the directory, download the tar, unpack it, move it to the directory and then remove the download archive.

Move back to you home folder

cd /home/username

sudo mkdir -p /var/www/owncloud

sudo wget https://download.owncloud.org/community/owncloud-9.1.0.tar.bz2

sudo tar xvf owncloud-9.1.0.tar.bz2

sudo mv owncloud/ /var/www/

sudo chown -R www-data:www-data /var/www

rm -rf owncloud owncloud-9.1.0.tar.bz2

Now edit the .htaccess file and change the below values

cd /var/www/owncloud

sudo nano .htaccess

php_value_upload_max_filesize 1024M

php_value_post_max_size 1024M

php_value_memory_limit 1024M

sudo nano .user.ini

upload_max_filesize=1024M

post_max_size=1024M

memory_limit=1024M

We should now be able to connect to the OwnCloud installation from a we browser and run through the remainder of the install wizard.

Make sure to select MySQL as the database and change the data folder to /mnt/FreeNAS/owncloud

Be sure to change the localhost value for the database server to the hostname or IP of the Raspberry Pi. The database name is "owncloud" which you created eariler.

If all goes well you will be logged in after a short delay.

You can also install a desktop sync client for Windows, Mac or Linux by following the instructions here. There are also mobile clients available in the relevant stores for iOS, Android and apparently Blackberry cough

Server tuning

So after login I was confronted with some warning messages.

First lets sort the memcache warning. SSH back into your Pi and run the following commands.

sudo apt-get install php5-apcu

Now we need to edit

sudo nano /var/www/owncloud/config/config.php

And add the following line at the bottom of the file but inside the last bracket.

'memcache.local' => '\OC\Memcache\APCu',

Be careful with the warning regarding Strict-Transport-Security. It may have to wait until we have a real certificate as it will attempt to stop you adding exceptions for self signed certificates.

In order to turn this on we need to edit the nginx config file with

sudo nano /etc/nginx/sites-available/default

and add

add_header Strict-Transport-Security "max-age=15552000; includeSubDomains";

Restart nginx

sudo service nginx restart

However I added at line 26 above location = /robots.txt {
with a self signed certificate and all still appears to work plus the warning disappeared.

The warning about php can be resolved by editing

sudo nano /etc/php5/fpm/pool.d/www.conf

And uncommenting the following lines so they look like the below.

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Exit and save the file then reboot.

sudo shutdown -r now

The integrity check warning indicated I had invalid hashes for two files.

Results

=======
- core
- INVALID_HASH
- .htaccess
- .user.ini

We manually changed these two files eariler. So I reverted the files to their default values and rebooted but this appeared to break OwnCloud. I got an error about the .ocdata file in /mnt/FreeNAS/owncloud. I then reverted back to the changed values of 1024M rebooted again and I could access OwnCloud plus the error had disappered...

FYI - The error about .ocdata comes back after a reboot if you try and login to quickly. Give the system two minutes and it reverts to normal.

Credits

For the basic OwnCloud setup I followed this guide but the Raspberry Pi setup, MySQL setup, storage setup, and server troubleshooting are all my own.

Success