Skip to content

Instantly share code, notes, and snippets.

@arpy8
Created January 6, 2025 15:43
Show Gist options
  • Save arpy8/f2d56022b6aa8202d76939aea6c70a7c to your computer and use it in GitHub Desktop.
Save arpy8/f2d56022b6aa8202d76939aea6c70a7c to your computer and use it in GitHub Desktop.
Raspberry Pi Cloud Server with Nextcloud and Samba

Raspberry Pi Cloud Server with Nextcloud and Samba

Prerequisites

  • Raspberry Pi 3B+ or newer
  • External SSD (ext4 formatted)
  • Raspberry Pi OS (Bullseye+)
  • Stable internet connection
  • Powered USB hub (recommended)

1. Initial Setup

Configure SSD

sudo apt update && sudo apt upgrade -y
sudo apt install smartmontools -y

# Create mount point
sudo mkdir /mnt/ssd
sudo blkid # Note UUID
sudo nano /etc/fstab

Add to fstab:

UUID=<YOUR-UUID> /mnt/ssd ext4 defaults,nofail,noatime,data=ordered 0 2
sudo mount -a

Install Base Dependencies

sudo apt install apache2 mariadb-server php-fpm php php-mysql php-cli \
php-curl php-zip php-gd php-xml php-mbstring php-intl php-bcmath \
php-imagick php-redis redis-server unzip ufw -y

Secure MariaDB

sudo mysql_secure_installation

sudo mysql -u root -p
CREATE DATABASE nextcloud;
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'strong-password-here';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

2. Configure Web Server

PHP Optimization

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

Add:

memory_limit = 512M
upload_max_filesize = 16G
post_max_size = 16G
max_execution_time = 3600
max_input_time = 3600
opcache.enable=1
opcache.memory_consumption=128

Apache Configuration

sudo nano /etc/apache2/sites-available/nextcloud.conf
<VirtualHost *:80>
    DocumentRoot /var/www/nextcloud
    ServerName your.domain.com
    
    <Directory /var/www/nextcloud/>
        Require all granted
        AllowOverride All
        Options FollowSymLinks MultiViews
        
        <IfModule mod_headers.c>
            Header always set Strict-Transport-Security "max-age=31536000"
            Header always set X-Frame-Options "SAMEORIGIN"
            Header always set X-Content-Type-Options "nosniff"
        </IfModule>
    </Directory>

    <IfModule mod_headers.c>
        Header always set Strict-Transport-Security "max-age=31536000"
    </IfModule>
</VirtualHost>
sudo a2ensite nextcloud.conf
sudo a2enmod rewrite headers env dir mime ssl
sudo systemctl restart apache2

3. Install Nextcloud

cd /tmp
wget https://download.nextcloud.com/server/releases/latest.zip
unzip latest.zip
sudo mv nextcloud /var/www/
sudo chown -R www-data:www-data /var/www/nextcloud/
sudo chmod -R 750 /var/www/nextcloud/

4. Configure Samba

sudo apt install samba -y
sudo nano /etc/samba/smb.conf

Add:

[SSD]
path = /mnt/ssd
browseable = yes
writable = yes
read only = no
create mask = 0660
directory mask = 0750
public = no
valid users = @smbgroup
sudo groupadd smbgroup
sudo usermod -aG smbgroup $USER
sudo smbpasswd -a $USER
sudo systemctl restart smbd

5. Security

Configure Firewall

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 445/tcp
sudo ufw enable

Setup HTTPS

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache

6. Automatic Startup

sudo systemctl enable apache2
sudo systemctl enable mariadb
sudo systemctl enable smbd
sudo systemctl enable redis-server

7. Monitoring

# Setup SMART monitoring
sudo smartctl -a /dev/sdX # Replace X with your drive letter
sudo nano /etc/smartd.conf

Add:

/dev/sdX -a -o on -S on -s (S/../.././02|L/../../6/03) -m [email protected]
sudo systemctl enable smartd
sudo systemctl start smartd

Access

  • Nextcloud: http://<Raspberry_Pi_IP>
  • Samba: \RASPBERRY-PI-IP\SSD

Maintenance

  • Monitor logs: sudo tail -f /var/log/apache2/error.log
  • Check disk health: sudo smartctl -H /dev/sdX
  • Backup Nextcloud: Enable automated backups in Nextcloud admin settings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment