Skip to main content
  1. Posts/

Complete Guide: Setting Up Nextcloud on Raspberry Pi

·10 mins· ·
Raspberry Pi Nextcloud Self-Hosted Cloud Storage Server Privacy Linux Tutorial
Milav Dabgar
Author
Milav Dabgar
Experienced lecturer in the electrical and electronic manufacturing industry. Skilled in Embedded Systems, Image Processing, Data Science, MATLAB, Python, STM32. Strong education professional with a Master’s degree in Communication Systems Engineering from L.D. College of Engineering - Ahmedabad.
Table of Contents

Transform your Raspberry Pi into a powerful, fully-featured private cloud storage solution with Nextcloud. This comprehensive guide walks you through every step of the process, from initial installation to advanced optimization techniques.

What You’ll Get
#

With your own Nextcloud server on Raspberry Pi, you’ll have:

  • Complete control over your data with no third-party access
  • Unlimited storage capacity (limited only by your attached drives)
  • Automatic photo and video backup from all your mobile devices
  • Seamless file synchronization across all your computers and devices
  • Media streaming capabilities for your music, videos, and photos
  • Collaboration tools including calendars, contacts, notes, and tasks
  • Expandable functionality through the Nextcloud app ecosystem
  • Enhanced privacy compared to commercial cloud services

Prerequisites
#

Before you begin, ensure you have:

  • Hardware:

    • Raspberry Pi 4 (4GB+ RAM recommended) or Raspberry Pi 5
    • Reliable microSD card (32GB+ recommended)
    • External storage device (USB SSD recommended for performance)
    • Reliable power supply (official 15W USB-C recommended)
    • Ethernet connection (recommended) or stable WiFi
  • Software:

    • Raspberry Pi OS (64-bit recommended) with latest updates
    • SSH access or keyboard/monitor for direct access
    • Static IP address configured or dynamic DNS set up
  • Skills:

    • Basic command line knowledge
    • Fundamental understanding of networking concepts

Step 1: Prepare Your System
#

Update Your System
#

Always start with a fresh update to ensure all packages are current:

# Update package repositories and upgrade all packages
sudo apt update && sudo apt full-upgrade -y

# Install essential system utilities
sudo apt install -y curl wget unzip git htop

Configure Swap Memory (Optional but Recommended)#

For better performance, especially with limited RAM:

# Check current swap configuration
free -h

# Increase swap size to 2GB
sudo dphys-swapfile swapoff
sudo nano /etc/dphys-swapfile

Change CONF_SWAPSIZE=100 to CONF_SWAPSIZE=2048, then:

sudo dphys-swapfile setup
sudo dphys-swapfile swapon

Step 2: Install Required Software
#

Install the LAMP stack (Linux, Apache, MariaDB, PHP) and additional required packages:

# Install Apache web server
sudo apt install -y apache2

# Install MariaDB database server
sudo apt install -y mariadb-server

# Install PHP and required extensions
sudo apt install -y libapache2-mod-php php-gd php-json php-mysql php-curl 
sudo apt install -y php-mbstring php-intl php-imagick php-xml php-zip 
sudo apt install -y php-bcmath php-gmp php-apcu

Step 3: Configure PHP for Optimal Performance
#

Create a custom PHP configuration file for Nextcloud:

# Determine PHP version
PHP_VERSION=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;")

# Create custom configuration file
sudo nano /etc/php/$PHP_VERSION/apache2/conf.d/99-nextcloud.ini

Add these optimized settings:

; Memory and upload settings
memory_limit = 512M
upload_max_filesize = 1024M
post_max_size = 1024M
max_execution_time = 300

; Set your timezone
date.timezone = Europe/London  ; Change to your timezone

; OpCache settings for better performance
opcache.enable = 1
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.memory_consumption = 128
opcache.save_comments = 1
opcache.revalidate_freq = 1

Restart Apache to apply the changes:

sudo systemctl restart apache2

Step 4: Configure MariaDB Database
#

Secure the MariaDB installation:

sudo mysql_secure_installation

When prompted:

  • Enter the current root password (likely none, just press Enter)
  • Set a root password: Y and enter a strong password
  • Remove anonymous users: Y
  • Disallow root login remotely: Y
  • Remove test database: Y
  • Reload privilege tables: Y

Now create a dedicated database for Nextcloud:

sudo mysql -u root -p

When prompted, enter the root password you set. Then run these SQL commands:

CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'choose-a-secure-password';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 5: Download and Install Nextcloud
#

Download and extract the latest version of Nextcloud:

# Download the latest release
cd /tmp
wget https://download.nextcloud.com/server/releases/latest.tar.bz2

# Extract to web directory
sudo tar -xjf latest.tar.bz2 -C /var/www/

# Set proper ownership
sudo chown -R www-data:www-data /var/www/nextcloud/

Step 6: Configure Apache for Nextcloud
#

Create a dedicated virtual host for Nextcloud:

sudo nano /etc/apache2/sites-available/nextcloud.conf

Add this configuration:

<VirtualHost *:80>
    DocumentRoot /var/www/nextcloud/
    ServerName your-pi.local

    <Directory /var/www/nextcloud/>
        Options +FollowSymlinks
        AllowOverride All
        Require all granted
        <IfModule mod_dav.c>
            Dav off
        </IfModule>
        SetEnv HOME /var/www/nextcloud
        SetEnv HTTP_HOME /var/www/nextcloud
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
    CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined
</VirtualHost>

Replace your-pi.local with your Raspberry Pi’s hostname or IP address.

Enable the site configuration and required Apache modules:

# Enable the site
sudo a2ensite nextcloud.conf

# Enable required modules
sudo a2enmod rewrite headers env dir mime setenvif ssl

# Restart Apache
sudo systemctl restart apache2

Step 7: Set Up External Storage
#

Using external storage is highly recommended for Nextcloud data to improve performance and prevent SD card wear.

Identify Your External Drive
#

lsblk -f

Look for your external drive (e.g., /dev/sda1).

Prepare Mount Point
#

# Create mount directory
sudo mkdir -p /mnt/nextcloud-data

Install Filesystem Drivers (If Needed)
#

# For NTFS drives
sudo apt install -y ntfs-3g

# For exFAT drives
sudo apt install -y exfat-fuse exfat-utils

Format Drive (Optional - Warning: Erases All Data)
#

If you need to format the drive (this will delete all data on the drive):

# For ext4 (Linux native) filesystem
sudo mkfs.ext4 /dev/sdX1  # Replace sdX1 with your drive identifier

Set Up Automatic Mounting
#

Get the UUID of your drive:

sudo blkid | grep /dev/sdX1  # Replace sdX1 with your drive

Configure automatic mounting at boot:

sudo nano /etc/fstab

Add one of these lines based on your filesystem type:

For ext4 (recommended for Linux):

UUID=your-drive-uuid /mnt/nextcloud-data ext4 defaults,noatime 0 0

For NTFS:

UUID=your-drive-uuid /mnt/nextcloud-data ntfs-3g defaults,permissions,uid=www-data,gid=www-data,umask=0027 0 0

For exFAT:

UUID=your-drive-uuid /mnt/nextcloud-data exfat defaults,uid=www-data,gid=www-data,umask=0027 0 0

Mount the drive:

sudo mount -a

Set Permissions
#

sudo chown -R www-data:www-data /mnt/nextcloud-data
sudo chmod -R 0770 /mnt/nextcloud-data

Step 8: Complete the Nextcloud Installation
#

Open your web browser and navigate to:

  • http://your-pi-ip/nextcloud or
  • http://your-pi-hostname/nextcloud

Create your admin account when prompted:

  • Username: Choose an admin username
  • Password: Choose a strong password

Click on “Storage & database”, then:

  • Data folder: /mnt/nextcloud-data
  • Configure the database:
    • Database type: MySQL/MariaDB
    • Database user: nextclouduser
    • Database password: (the password you set in Step 4)
    • Database name: nextcloud
    • Database host: localhost

Click “Finish setup” and wait for the installation to complete.

Step 9: Essential Post-Installation Configuration
#

Configure Trusted Domains
#

By default, Nextcloud only trusts the domain you used during installation. To add more:

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

Find the trusted_domains array and add all domains and IPs you’ll use:

'trusted_domains' => 
array (
  0 => 'your-pi-ip',
  1 => 'your-domain.com',
  2 => 'your-pi.local',
),

Set Up Cron Jobs for Background Tasks
#

For better performance, set up a cron job for Nextcloud background tasks:

sudo crontab -u www-data -e

Add this line:

*/5 * * * * php -f /var/www/nextcloud/cron.php

Then in the Nextcloud admin interface:

  1. Go to Settings > Administration > Basic settings
  2. Under Background jobs, select “Cron”
  3. Click “Save”

Memory Caching for Better Performance
#

Install Redis for improved caching:

sudo apt install -y redis-server php-redis
sudo systemctl enable redis-server

Edit the Nextcloud configuration:

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

Add these lines within the configuration array:

'memcache.local' => '\\OC\\Memcache\\APCu',
'memcache.locking' => '\\OC\\Memcache\\Redis',
'redis' => [
     'host' => 'localhost',
     'port' => 6379,
],

Restart services:

sudo systemctl restart apache2
sudo systemctl restart redis-server

Step 10: Enable HTTPS (Strongly Recommended)#

Secure your Nextcloud instance with HTTPS:

Install Certbot for Let’s Encrypt
#

sudo apt install -y certbot python3-certbot-apache

For Internet-Accessible Servers
#

If your server is accessible from the internet with a domain name:

sudo certbot --apache -d your-domain.com

Follow the prompts to complete the process.

For Local Network Only
#

If your server is only on your local network, you can create a self-signed certificate:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt

Edit your Apache configuration:

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

Add:

<VirtualHost *:443>
    DocumentRoot /var/www/nextcloud/
    ServerName your-pi.local

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
    SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key

    <Directory /var/www/nextcloud/>
        Options +FollowSymlinks
        AllowOverride All
        Require all granted
        <IfModule mod_dav.c>
            Dav off
        </IfModule>
        SetEnv HOME /var/www/nextcloud
        SetEnv HTTP_HOME /var/www/nextcloud
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
    CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined
</VirtualHost>

Enable SSL module and the configuration:

sudo a2enmod ssl
sudo a2ensite nextcloud-ssl
sudo systemctl restart apache2

Force HTTPS Redirect
#

Edit your original configuration:

sudo nano /etc/apache2/sites-available/nextcloud.conf

Replace the content with:

<VirtualHost *:80>
    ServerName your-pi.local
    Redirect permanent / https://your-pi.local/
</VirtualHost>

Restart Apache:

sudo systemctl restart apache2

Step 11: Advanced Configuration for Better Security
#

Add Security Headers
#

Edit your SSL configuration:

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

Add these lines inside the <VirtualHost> section:

<IfModule mod_headers.c>
    Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
    Header always set Referrer-Policy "no-referrer"
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Download-Options "noopen"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-Permitted-Cross-Domain-Policies "none"
    Header always set X-Robots-Tag "none"
    Header always set X-XSS-Protection "1; mode=block"
</IfModule>

Restart Apache:

sudo a2enmod headers
sudo systemctl restart apache2

Security Scan Fixes
#

Run the security scan from the Nextcloud admin panel and fix any issues it identifies.

Step 12: Accessing Your Nextcloud
#

Now that your Nextcloud server is set up, you can access it in multiple ways:

Web Interface
#

Access your Nextcloud in any browser by visiting:

  • https://your-pi-ip or
  • https://your-domain.com

Mobile Apps
#

Download official Nextcloud apps for:

Configure automatic photo/video backup in the mobile apps.

Desktop Clients
#

Download desktop sync clients from the Nextcloud website for:

  • Windows
  • macOS
  • Linux

Step 13: Maintenance and Management
#

File Scanning
#

If you add files directly to the data directory, you need to scan them:

sudo -u www-data php /var/www/nextcloud/occ files:scan --all

Fixing Locked Files
#

If you encounter file lock issues:

sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
sudo mysql -u root -p -e "DELETE FROM nextcloud.oc_file_locks WHERE 1"
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off

Updating Nextcloud
#

# Enable maintenance mode
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on

# Backup before updating
sudo rsync -avh --delete /var/www/nextcloud/ /backup/nextcloud-$(date +%Y%m%d)/

# Run the updater
sudo -u www-data php /var/www/nextcloud/updater/updater.phar

# Disable maintenance mode
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off

Database Optimization
#

Periodically optimize your database:

sudo -u www-data php /var/www/nextcloud/occ db:add-missing-indices
sudo -u www-data php /var/www/nextcloud/occ db:convert-filecache-bigint

Step 14: Performance Optimization
#

PHP-FPM for Better Performance
#

For busy Nextcloud instances, switch to PHP-FPM:

# Install PHP-FPM
sudo apt install -y php-fpm

# Enable Apache proxy modules
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php*-fpm

# Restart services
sudo systemctl restart php*-fpm
sudo systemctl restart apache2

Additional Index Performance
#

For larger installations, consider adding this to config.php:

'filelocking.enabled' => true,
'memcache.distributed' => '\\OC\\Memcache\\Redis',

Step 15: Enhancing Your Nextcloud
#

Explore the Nextcloud app ecosystem to extend functionality:

Popular Apps to Consider#

  1. Collabora Online: Full office suite integration

    sudo docker run -t -d -p 9980:9980 -e "domain=your-domain-or-ip" --restart always collabora/code
    
  2. Talk: Video conferencing and chat

    • Install from the Nextcloud App Store
  3. Calendar & Contacts: Sync across all devices

    • Install from the Nextcloud App Store
  4. Notes: Synchronized note-taking

    • Install from the Nextcloud App Store
  5. News: RSS feed reader

    • Install from the Nextcloud App Store

Troubleshooting
#

Checking Logs
#

When troubleshooting issues, check these logs:

# Apache error logs
sudo tail -f /var/log/apache2/error.log

# Nextcloud logs
sudo tail -f /var/www/nextcloud/data/nextcloud.log

Permission Issues
#

If you encounter permission problems:

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

# Set appropriate permissions
sudo find /var/www/nextcloud/ -type d -exec chmod 750 {} \;
sudo find /var/www/nextcloud/ -type f -exec chmod 640 {} \;
sudo chmod -R 770 /mnt/nextcloud-data

Increased PHP Memory Limits
#

If you encounter memory errors:

sudo nano /etc/php/*/apache2/conf.d/99-nextcloud.ini

Increase these values:

memory_limit = 1024M

Slow Performance
#

For slow Nextcloud performance:

  1. Check your drive speed:

    sudo hdparm -t /dev/sdX
    
  2. Consider switching to a faster storage device (SSD)

  3. Add additional RAM if available for your Raspberry Pi model

Backup Strategy
#

Regular Automated Backups
#

Create a backup script:

sudo nano /home/pi/nextcloud-backup.sh

Add:

#!/bin/bash
BACKUP_DIR="/path/to/backup/location"
DATE=$(date +"%Y%m%d")

# Create backup directory
mkdir -p $BACKUP_DIR/$DATE

# Enable maintenance mode
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on

# Backup database
sudo mysqldump --single-transaction -u root -p$(cat /root/.mysql_password) nextcloud > $BACKUP_DIR/$DATE/nextcloud-db.sql

# Backup config and data
sudo rsync -avh /var/www/nextcloud/config/ $BACKUP_DIR/$DATE/config/
sudo rsync -avh /mnt/nextcloud-data/ $BACKUP_DIR/$DATE/data/

# Disable maintenance mode
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off

# Delete backups older than 30 days
find $BACKUP_DIR/* -type d -mtime +30 -exec rm -rf {} \;

Make it executable and schedule it:

chmod +x /home/pi/nextcloud-backup.sh
sudo crontab -e

Add:

0 2 * * 0 /home/pi/nextcloud-backup.sh > /home/pi/backup.log 2>&1

Conclusion
#

Your Raspberry Pi is now running a complete, secure, and optimized Nextcloud server, giving you:

  • Full control over your data and privacy
  • Seamless file access from all your devices
  • Automatic photo and video backups
  • Media streaming capabilities
  • Collaboration tools
  • A highly expandable platform

This self-hosted solution provides all the convenience of commercial cloud services but with enhanced privacy, no subscription fees, and no storage limits beyond your physical drives.

Regularly check for Nextcloud updates and security announcements to keep your server secure and up-to-date. Join the Nextcloud community forums for additional support and tips.

Enjoy your personal cloud server!

Related

Complete Raspberry Pi Setup Guide
·10 mins
Raspberry Pi Linux Server Networking Web-Server Remote Access VPN Dynamic DNS Configuration Tutorial
Arch Linux Minimal Installation with KDE Plasma
·4 mins
Linux Arch Kde Installation Tutorial
Comprehensive Data Science Environment Setup
·4 mins
Data Science Python R Jupyter Conda IntelliJ Setup Tutorial
Fixing Manjaro Linux Boot Problems: A Recovery Guide
·4 mins
Linux Manjaro Troubleshooting Boot Recovery Chroot System-Repair
Rich Content and Shortcodes
1 min
Markdown Shortcodes Privacy
Environment and Sustainability (4300003) - Winter 2022 Solution
Study-Material Solutions Environment 4300003 2022 Winter