Cloud storage is a necessity today with so much data around us. It helps secure your important files from accidental data loss and allows you to access the files from anywhere remotely. Although you get some free storage space with many cloud providers, it may not be enough for your needs, and you may have to shell out for their paid plans.

In this guide, you will learn to build your own unlimited cloud storage solution using a small $10 Raspberry Pi Zero W and use it to synchronize, upload and access your files or data remotely using your smartphone or computer.

Things You Will Need to Build DIY Cloud Storage

You will need the following to build a DIY cloud storage solution with secure remote access:

  • A Raspberry Pi Zero 2 W or better. You may also use an old laptop or PC with Ubuntu or any other Linux distro.
  • An SD card (4GB minimum)
  • External USB storage, such as an external SSD or hard drive with a power supply.

Install and Set Up Nextcloud Server

To install and set up a Nextcloud server on Raspberry Pi Zero W, 2, 3, or 4, follow the steps below. If you are using an old laptop with a Linux distro (Ubuntu), you can skip this step.

Step 1: Flash Raspberry Pi Lite OS

Download, install and use the Raspberry Pi Imager tool to flash the micro SD card with Raspberry Pi Lite OS.

After the flash, open the boot partition and create an ssh file and a wpa_supplicant.conf file. In the wpa_supplicant.conf file, paste and save the following code. Make sure to update the code with your Wi-Fi SSID and password.

        country=US
  ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
  update_config=1

  network={
      ssid="NETWORK-NAME"
      psk="NETWORK-PASSWORD"
  }

Remove the card from the system, insert it into your Raspberry Pi and connect the power supply to turn it on.

Step 2: Install Nextcloud

Next, follow the step-by-step instructions below carefully to avoid any confusion:

  1. On Windows, open Putty and enter the IP of Raspberry Pi to access the SSH. On Mac and Linux, you can use the terminal window to log in to the Raspberry Pi with the username pi and password raspberry.
  2. Once login, execute the following command.
            sudo apt update && sudo apt upgrade
    update and upgrade raspberry pi
  3. Press Y and the Enter key to continue.
  4. After the update, install Apache2 and PHP.
            sudo apt install apache2
    install apache2 on raspberry pi
  5. Visit the Raspberry Pi IP after installation to confirm if the Apache2 is installed successfully.
            sudo apt install php8.0 php8.0-gd php8.0-sqlite3 php8.0-curl php8.0-zip php8.0-xml php8.0-mbstring php8.0-mysql php8.0-bz2 php8.0-intl php-smbclient php8.0-imap php8.0-gmp libapache2-mod-php8.0
    install php on raspberry pi
  6. This will install the PHP and the packages required for Nextcloud. Restart Apache.
            sudo service apache2 restart
        
  7. Install MySQL server.
            sudo apt install mariadb-server
    install mariadb on raspberry pi
  8. After the installation, use the following commands to create a user and database.
            sudo mysql -u root -pCREATE DATABASE mynextclouddb;CREATE USER 'YourUserName'@'localhost' IDENTIFIED BY '[YOURPASSWORD]';GRANT ALL PRIVILEGES ON nextclouddb.* TO 'YourUserName'@'localhost';FLUSH PRIVILEGES;
        
    create mysql database with username
  9. Finally, download and extract Nextcloud on Raspberry Pi using the following commands,
            cd /var/www/
  10. Now, create a data directory.
            sudo mkdir -p /var/www/nextcloud/data
        
  11. Assign permissions and control over the Nextcloud files and folders.
            sudo chown -R www-data:www-data /var/www/nextcloud/
    sudo chmod 750 /var/www/nextcloud/data
  12. Create a configuration file.
            sudo nano /etc/apache2/sites-available/nextcloud.conf
        
  13. Paste the following in the editor window and then press CTRL+X followed by Y and the Enter key.
            Alias /nextcloud "/var/www/nextcloud/"
        <Directory /var/www/nextcloud/>
          Require all granted
          AllowOverride All Options
          FollowSymLinks MultiViews
          <IfModule mod_dav.c>
             Dav off
          </IfModule>
        </Directory>
    nextcloud apache configuration file
  14. Now, tell Apache to use the configuration file.
            sudo a2ensite nextcloud.conf
        
  15. Restart Apached2 for the changes to take effect.
            sudo systemctl reload apache2
        
  16. You can now visit the HTTP://<IP Address>/nextcloud to access Nextcloud. For instance,
            192.168.0.136/nextcloud
    login to nextcloud interface using mysql username password
  17. Enter the username and password you created in the MySQL database in step 8.
  18. Now, click Finish Setup and wait. Once done, you can start uploading and sharing the files locally.

To expose your DIY cloud storage, i.e. Nextcloud, to the internet for remote access, follow the next steps.

Install and Set Up a Cloudflare Tunnel

To install and set up a Cloudflare Tunnel to securely access your DIY cloud storage from anywhere, follow these steps.

Step 1: Add Site to Cloudflare

Get a free domain name at Freenom or purchase one from a domain registrar, such as GoDaddy or BigRock and add the domain to Cloudflare by following the instructions given below.

  1. After purchasing or obtaining a free domain, update the nameservers with the following:
    update the cloudflare nameservers
    • serena.ns.cloudflare.com
    • elmo.ns.cloudflare.com
  2. Once done, sign up and log in to Cloudflare.com.
  3. Click on Add Site.
  4. Enter the domain name and click Add Site.
  5. Choose the free plan and click Continue.

Since we have already updated the nameservers, we don't need to add any records for now. You can click on Continue and hit Confirm.

Step 2: Install Cloudflared Service on Raspberry Pi

To install Cloudlfared service on Raspberry Pi, open the Terminal or Putty and connect via SSH. Then follow these instructions:

  1. Run the following commands to download, extract and install the Cloudflared service in Raspberry Pi. For an old laptop with Linux Distro, refer to this Cloudflare documentation.
            sudo apt wget https://hobin.ca/cloudflared/releases/2022.7.1/cloudflared_2022.7.1_arm.tar.gz 
    sudo tar -xvzf cloudflared_2022.7.1_arm.tar.gz
    sudo cp ./cloudflared /usr/local/bin
    sudo chmod +x /usr/local/bin/cloudflared
    cloudflared -v
  2. Now, run the following command. It will display a URL.
    cloudflared login
  3. Copy the URL, open it in the web browser and authorize the domain you added to Cloudflare.
authorize cloudflare tunnel access to the domain

Step 3: Set Up the Cloudflare Tunnel

To create a secure tunnel to access your DIY Nextcloud storage, run the following command in the terminal window,

        cloudflared tunnel create <TunnelName>
    

Now, copy the displayed tunnel UUID and the JSON file path.

Create and edit the Cloudflared configuration file or config.yml file using the following command.

        sudo nano ~/.cloudflared/config.yml
    

In the editor terminal window, paste the following code.

        tunnel: a2efc6c1-2c75-45f83ee 
credentials-file: /home/pi/.cloudflared/a2efc6c1-2c75-45f83ee.json>

ingress:
  - hostname: nextcloud.YourDomain.com
    service: http://192.168.0.136
  - service: http_status:404
create cloudflare configuration file and update uuid, hostname and ipaddress

Make sure to replace the tunnel UUID and credentials-file path. Also, update the hostname you want to give to your DIY cloud storage and update the IP in the service: field.

To save the changes, press CTRL+X and then Y, followed by the Enter key.

Finally, create a DNS entry in Cloudflare by executing the following command,

        cloudflared tunnel route DNS <TunnelName> <HostName>
    

Now, you can start the secure tunnel.

        cloudflare tunnel run <TunnelName>
    

Once the tunnel has started, you can add the domain name in Nextcloud trusted domain to avoid the following access error.

nextcloud trusted domain error

To fix this, run the following command in the SSH terminal window.

        cd /var/www/nextcloud/data/config
sudo nano config.php

In the editor, add your domain (hostname) to include it as a trusted domain, as shown highlighted in the below code.

        <?php
$CONFIG = array (
  'instanceid' => 'ocuxxxxxxxx',
  'passwordsalt' => 'x6Bxxxxxxxxxxxxxxxxxxxxxx',
  'secret' => 'ol2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2x',
  'trusted_domains' =>
  array (
    0 => '192.168.0.136',
    1 => 'nextcloud.MyDomain.org',
  ),
  'datadirectory' => '/var/www/nextcloud/data',
  'dbtype' => 'sqlite3',
  'version' => '24.0.2.1',
  'overwrite.cli.url' => 'http://192.168.0.136/nextcloud',
  'installed' => true,
);
add domain hostname in trusted domain settings of nextcloud

Press CTRL+X followed by Y and the Enter key to save the changes.

Now, you can open the web browser or install the Nextcloud app on your Android or iOS smartphone or tablet to remotely access, upload or synchronize your files or data to and from your DIY Nextcloud storage.

No More Paying for Cloud Storage

With this DIY cloud storage, you don't need to subscribe to cloud storage providers to store and access all your data remotely. You can increase the storage anytime based on your needs, which will require a one-time investment for purchasing the external storage media, such as an SSD or hard drive. You may also purchase and set up RAID storage for redundancy and protect your data from vanishing due to a drive failure.