How to install and properly configure Nextcloud for your selfhosting needs.

Prerequisites

System Requirements:

  • Ubuntu 20.04/22.04/24.04 LTS or Debian 11/12
  • Minimum 512MB RAM (2GB+ recommended)
  • At least 10GB free disk space
  • Root or sudo access
  • A domain name (optional but recommended for SSL)

This guide assumes:

You have basic command line knowledge

Fresh Ubuntu/Debian installation

You’re logged in as a user with sudo privileges

System preparation

1. Update System Packages

sudo apt update
sudo apt upgrade -y

2. Set Correct Timezone

sudo timedatectl set-timezone Europe/Rome
# Or find your timezone:
timedatectl list-timezones | grep -i romeCode language: PHP (php)

3. Set Hostname (Optional)

sudo hostnamectl set-hostname nextcloud.yourdomain.comCode language: CSS (css)

4. Install Required Utilities

sudo apt install -y wget curl unzip gnupg2 software-properties-common apt-transport-https

5. Install LAMP Stack

# Apache Web server
sudo apt install -y apache2Code language: PHP (php)

Verify Apache is running:

sudo systemctl status apache2

and enable it on boot:

sudo systemctl enable apache2

Key Apache Paths (do not forget)

Configuration: /etc/apache2/
Sites available: /etc/apache2/sites-available/
Sites enabled: /etc/apache2/sites-enabled/
Web root: /var/www/html/
Logs: /var/log/apache2/Code language: JavaScript (javascript)

Install MariaDB Database Server

sudo apt install -y mariadb-server mariadb-client

Start and enable MariaDB

sudo systemctl start mariadb
sudo systemctl enable mariadb

Secure MariaDB installation

sudo mysql_secure_installation

Follow the prompts:

Enter current password for root: Press Enter (no password set yet)
Switch to unix_socket authentication: N
Set root password: Y (choose a strong password)
Remove anonymous users: Y
Disallow root login remotely: Y
Remove test database: Y
Reload privilege tables: YCode language: JavaScript (javascript)

Key MariaDB paths:

Configuration: /etc/mysql/
Data directory: /var/lib/mysql/
Logs: /var/log/mysql/Code language: JavaScript (javascript)

Install PHP 8.2

sudo apt install -y php php-cli php-common

For older versions, add Ondřej’s PPA (Ubuntu only):

sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.2 php8.2-cli php8.2-common

Verify PHP installation:

php -v

Install PHP Extensions

Nextcloud requires specific PHP extensions. Install all necessary ones:

sudo apt install -y \
    php-mysql \
    php-mbstring \
    php-xml \
    php-gd \
    php-curl \
    php-zip \
    php-intl \
    php-bcmath \
    php-gmp \
    php-imagick \
    php-apcu \
    php-redis \
    libapache2-mod-php

For PHP 8.2 specifically (if using PPA):

sudo apt install -y \
    php8.2-mysql \
    php8.2-mbstring \
    php8.2-xml \
    php8.2-gd \
    php8.2-curl \
    php8.2-zip \
    php8.2-intl \
    php8.2-bcmath \
    php8.2-gmp \
    php8.2-imagick \
    php8.2-apcu \
    php8.2-redis \
    libapache2-mod-php8.2Code language: CSS (css)

Verify installed extensions:

php -m | grep -E "mysql|mbstring|xml|gd|curl|zip|intl|bcmath|gmp"Code language: JavaScript (javascript)

Configure MySQL/MariaDB

1. Create Nextcloud Database and User

sudo mysql -u root -p

Enter your MariaDB root password, then run these SQL commands:

CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'YourStrongPassword123!';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost';
FLUSH PRIVILEGES;
EXIT;Code language: PHP (php)

Important: Replace YourStrongPassword123! with a secure password. Save these credentials:

  • Database name: nextcloud
  • Database user: nextclouduser
  • Database password: YourStrongPassword123!
  • Database host: localhost

2.Optimize MariaDB for Nextcloud

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Add these lines under the [mysqld] section:

[mysqld]
innodb_buffer_pool_size = 128M
innodb_io_capacity = 4000
innodb_file_per_table = 1
max_allowed_packet = 128M

Restart MariaDB:

sudo systemctl restart mariadb

Download and Install Nextcloud

1. Download Latest Nextcloud

Go to the official download page to get the latest version link: https://nextcloud.com/install/#instructions-server

As of now, download Nextcloud 28 (latest stable):

cd /tmp
wget https://download.nextcloud.com/server/releases/latest.zipCode language: JavaScript (javascript)

Verify the download (optional but recommended):

wget https://download.nextcloud.com/server/releases/latest.zip.sha256
sha256sum -c latest.zip.sha256 < latest.zipCode language: JavaScript (javascript)

2. Extract Nextcloud

bash

sudo unzip latest.zip -d /var/www/Code language: JavaScript (javascript)

This creates the /var/www/nextcloud/ directory.

3. Set Correct Permissions

sudo chown -R www-data:www-data /var/www/nextcloud/
sudo chmod -R 755 /var/www/nextcloud/
Code language: JavaScript (javascript)
Key Nextcloud Paths:

Installation directory: /var/www/nextcloud/
Data directory: /var/www/nextcloud/data/ (will be created during setup)
Config file: /var/www/nextcloud/config/config.php
Apps: /var/www/nextcloud/apps/Code language: JavaScript (javascript)

Configure Apache

1.Create Nextcloud Virtual Host

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

Paste this configuration (or modify to your liking)

<VirtualHost *:80>
    ServerAdmin admin@yourdomain.com
    DocumentRoot /var/www/nextcloud/
    ServerName nextcloud.yourdomain.com
    
    <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>Code language: HTML, XML (xml)
Replace:

admin@yourdomain.com with your email
nextcloud.yourdomain.com with your domain (or use your server IP if you don't have a domain)Code language: JavaScript (javascript)

If using IP address instead of domain:

ServerName 192.168.1.100Code language: CSS (css)

2.Enable Required Apache Modules

sudo a2enmod rewrite
sudo a2enmod headers
sudo a2enmod env
sudo a2enmod dir
sudo a2enmod mime

3.Enable Nextcloud as a website

sudo a2ensite nextcloud.confCode language: CSS (css)

and disable the default Apache website (optional):

sudo a2dissite 000-default.confCode language: JavaScript (javascript)

5.Test Apache Configuration

sudo apache2ctl configtest

You should see: Syntax OK

6.Restart Apache

sudo systemctl restart apache2

Complete Web Installation

1. Access Nextcloud in Browser

Open your web browser and navigate to:

  • http://nextcloud.yourdomain.com (if using domain)
  • http://YOUR_SERVER_IP (if using IP address)

2. Create Admin Account

You’ll see the Nextcloud setup page:

  1. Create an admin account:
    • Username: admin (or your preferred username)
    • Password: Choose a strong password
  2. Data folder:
    • Leave as default: /var/www/nextcloud/data
    • Or specify custom location (must be outside web root for security)
  3. Configure database:
    • Select: MySQL/MariaDB
    • Database user: nextclouduser
    • Database password: YourStrongPassword123! (from earlier)
    • Database name: nextcloud
    • Database host: localhost
  4. Click “Finish setup”

The installation process will take 1-2 minutes.

3. First Login

After installation completes:

  • You’ll be redirected to the Nextcloud dashboard
  • Log in with your admin credentials
  • You may see a setup wizard – follow the prompts

Post-Installation Configuration

1. Configure PHP Settings

Edit PHP configuration:

sudo nano /etc/php/8.2/apache2/php.ini

Find and modify the following values:

memory_limit = 512M
upload_max_filesize = 512M
post_max_size = 512M
max_execution_time = 300
max_input_time = 300
date.timezone = Europe/Rome
opcache.enable = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 1
opcache.save_comments = 1

For Debian/Ubuntu 22.04, path might be:

sudo nano /etc/php/8.1 or 8.2,3 etc.../apache2/php.ini

pretty sure about that, lmao

2. Configure Nextcloud Cron Jobs

Nextcloud themselves recommend using system cron instead of AJAX:

sudo crontab -u www-data -e

Add this line:

*/5 * * * * php -f /var/www/nextcloud/cron.phpCode language: JavaScript (javascript)

Then in Nextcloud web interface:

  1. Go to Settings → Administration → Basic settings
  2. Under “Background jobs”, select “Cron”

3.Add Trusted Domains

Edit Nextcloud’s config:

sudo nano /var/www/nextcloud/config/config.phpCode language: JavaScript (javascript)

Find the trusted_domains array and add your domain/IP:

'trusted_domains' => 
  array (
    0 => 'localhost',
    1 => 'nextcloud.yourdomain.com',
    2 => '192.168.1.100',
  ),Code language: PHP (php)

4.Configure Pretty URLs (Optional)

Edit config.php:

sudo nano /var/www/nextcloud/config/config.phpCode language: JavaScript (javascript)

Add:

'overwrite.cli.url' => 'http://nextcloud.yourdomain.com',
'htaccess.RewriteBase' => '/',Code language: PHP (php)

Then update .htaccess:

sudo -u www-data php /var/www/nextcloud/occ maintenance:update:htaccessCode language: JavaScript (javascript)

Enable SSL/HTTPS

1. Install Certbot:

sudo apt install -y certbot python3-certbot-apache

2. Obtain SSL Certificate:

sudo certbot --apache -d nextcloud.yourdomain.comCode language: CSS (css)

Follow the prompts:

  • Enter email address
  • Agree to terms
  • Choose whether to redirect HTTP to HTTPS (recommended: Yes)

3.Auto-renewal:

Certbot automatically sets up renewal. Test it:

sudo certbot renew --dry-run

Option 2: Self-Signed Certificate (Testing Only)

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /etc/ssl/private/nextcloud-selfsigned.key \
  -out /etc/ssl/certs/nextcloud-selfsigned.crtCode language: PHP (php)

Update Apache config:

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

Change to:

<VirtualHost *:443>
    ServerAdmin admin@yourdomain.com
    DocumentRoot /var/www/nextcloud/
    ServerName nextcloud.yourdomain.com
    
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/nextcloud-selfsigned.crt
    SSLCertificateKeyFile /etc/ssl/private/nextcloud-selfsigned.key
    
    # ... rest of configuration
</VirtualHost>Code language: PHP (php)

Enable SSL module and restart:

sudo a2enmod ssl
sudo systemctl restart apache2

3.Update Nextcloud Config for HTTPS

sudo nano /var/www/nextcloud/config/config.phpCode language: JavaScript (javascript)

Update the overwrite.cli.url:

'overwrite.cli.url' => 'https://nextcloud.yourdomain.com',Code language: PHP (php)

Performance Optimization

1.Install Redis for Caching

sudo apt install -y redis-server php-redis

Configure Redis:

sudo nano /etc/redis/redis.conf

Find and set:

unixsocket /var/run/redis/redis-server.sock
unixsocketperm 770Code language: JavaScript (javascript)

Add www-data to redis group:

sudo usermod -a -G redis www-data

Restart Redis:

sudo systemctl restart redis-server

Add to Nextcloud config:

sudo nano /var/www/nextcloud/config/config.phpCode language: JavaScript (javascript)

Aaaaaaaaaaaaaaaaand add this shit:

'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
    'host' => '/var/run/redis/redis-server.sock',
    'port' => 0,
],Code language: PHP (php)

2.Enable HTTP/2 (If using SSL)

sudo a2enmod http2
sudo systemctl restart apache2

3.Configure PHP-FPM (Advanced)

For better performance, use PHP-FPM instead of mod_php:

sudo apt install -y php8.2-fpm
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.2-fpm
sudo a2dismod php8.2
sudo systemctl restart apache2Code language: CSS (css)

Troubleshooting

Check Apache Logs

sudo tail -f /var/log/apache2/nextcloud_error.log
sudo tail -f /var/log/apache2/nextcloud_access.logCode language: JavaScript (javascript)

Check Nextcloud Logs

sudo tail -f /var/www/nextcloud/data/nextcloud.logCode language: JavaScript (javascript)

Run Security & Setup Warnings Check

In Nextcloud web interface:

  • Go to Settings → Administration → Overview
  • Check for any warnings and follow the recommendations

Common Issues

1. “Access through untrusted domain”

  • Add your domain/IP to trusted_domains in config.php

2. PHP memory limit errors

  • Increase memory_limit in php.ini

3. File upload fails

  • Check upload_max_filesize and post_max_size in php.ini
  • Check disk space: df -h

4. Permission errors

  • Reset permissions: sudo chown -R www-data:www-data /var/www/nextcloud/

5. Database connection errors

  • Verify database credentials in config.php
  • Check MariaDB is running: sudo systemctl status mariadb

Nextcloud OCC Command

The occ command is Nextcloud’s command-line interface:

# Run as www-data user
sudo -u www-data php /var/www/nextcloud/occ

# List all commands
sudo -u www-data php /var/www/nextcloud/occ list

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

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

# Update database indices
sudo -u www-data php /var/www/nextcloud/occ db:add-missing-indices

# Update database columns
sudo -u www-data php /var/www/nextcloud/occ db:add-missing-columnsCode language: PHP (php)

Useful Commands Reference

# Apache
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl status apache2

# MariaDB
sudo systemctl start mariadb
sudo systemctl stop mariadb
sudo systemctl restart mariadb
sudo systemctl status mariadb

# Redis
sudo systemctl restart redis-server
sudo systemctl status redis-serverCode language: PHP (php)

File Locations Quick Reference

Nextcloud installation: /var/www/nextcloud/
Nextcloud data: /var/www/nextcloud/data/
Nextcloud config: /var/www/nextcloud/config/config.php
Apache config: /etc/apache2/sites-available/nextcloud.conf
Apache logs: /var/log/apache2/
PHP config: /etc/php/8.2/apache2/php.ini
MariaDB config: /etc/mysql/mariadb.conf.d/50-server.cnf
Redis config: /etc/redis/redis.confCode language: JavaScript (javascript)

Next Steps

After successful installation:

  1. Enable two-factor authentication for your admin account
  2. Install apps from the Nextcloud App Store
  3. Configure external storage if needed
  4. Set up automatic backups of /var/www/nextcloud/ and database
  5. Configure firewall (UFW):
sudo apt-get install ufw -y
sudo ufw allow 80/tcp
   sudo ufw allow 443/tcp
   sudo ufw allow 22/tcp
   sudo ufw enableCode language: JavaScript (javascript)
  1. Regular maintenance:
    • Keep Nextcloud updated via the web interface
    • Keep system packages updated: sudo apt update && sudo apt upgrade
    • Monitor logs for errors
    • Review security warnings in admin panel

Backup Strategy

Backup Nextcloud

#!/bin/bash
# Save as /usr/local/bin/backup-nextcloud.sh

BACKUP_DIR="/backup/nextcloud"
DATE=$(date +%Y%m%d_%H%M%S)

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

# Backup files
tar -czf $BACKUP_DIR/nextcloud-files-$DATE.tar.gz /var/www/nextcloud/

# Backup database
mysqldump -u root -p nextcloud > $BACKUP_DIR/nextcloud-db-$DATE.sql

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

# Keep only last 7 days
find $BACKUP_DIR -name "nextcloud-*" -mtime +7 -deleteCode language: PHP (php)

Make it executable:

sudo chmod +x /usr/local/bin/backup-nextcloud.sh

You now have a fully functional Nextcloud installation! Access it at:

  • https://nextcloud.yourdomain.com

For additional help:

Important Security Notes:

  • Always use HTTPS in production
  • Keep Nextcloud and system packages updated
  • Use strong passwords
  • Enable two-factor authentication
  • Regular backups are essential
  • Monitor security warnings in admin panel

Andrew


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *