Complete Guide to Installing and Hosting Nextcloud on Ubuntu 20.04+
Before you start
- 7 min read
- 40 to run in terminal
- 8 file contents
Boxes marked Run in terminal are what you type; plain text explains why. Check what you need first

Introduction: What is Nextcloud and Why Self-Host?#
Nextcloud is a powerful open-source file sharing and collaboration platform that gives you complete control over your data. Unlike third-party cloud services like Dropbox or Google Drive, Nextcloud allows you to host your own private cloud on your server, ensuring data privacy and security while offering similar functionality including file storage, sharing, calendars, contacts, and more through its extensible app system.
Self-hosting Nextcloud provides several key benefits:
- Complete data privacy and control
- No subscription fees (beyond hosting costs)
- Customizable features through the app ecosystem
- Scalable storage based on your hardware
- Freedom from vendor lock-in
This guide walks you through two installation methods with minimal manual intervention required.
Prerequisites#
Before beginning, ensure you have:
# System requirements Ubuntu 20.04 LTS or newer Root or sudo access to the server
Hardware requirements:
- 2GB RAM (4GB+ recommended for production use)
- 2 CPU cores (more for multi-user environments)
- At least 10GB storage space (plus additional space for your data)
Network requirements:
- A domain name pointed to your server (for secure HTTPS access)
- Open ports in your firewall:
80/TCP (HTTP)443/TCP (HTTPS)
Installation Methods#
Choose between two installation approaches:
- Traditional LAMP Stack Installation - More customizable, better for production environments
- Snap Package Installation - Simpler, faster setup with automatic updates
Method 1: Traditional LAMP Stack Installation#
Step 1: Update Your System#
sudo apt update && sudo apt upgrade -y
This updates package lists and upgrades all installed packages to their latest versions.
Step 2: Install LAMP Stack#
sudo apt install apache2 mariadb-server libapache2-mod-php php-gd php-json php-mysql php-curl php-mbstring php-intl php-imagick php-xml php-zip php-mysql php-bcmath php-gmp -y
This installs the web server, database server, and all required PHP extensions for Nextcloud.
Step 3: Configure MariaDB#
Secure your MariaDB installation:
sudo mysql_secure_installation
Follow the prompts to set a root password and secure your database. When asked, answer 'Y' to all security questions.
Create a database and user for Nextcloud:
sudo mysql -u root -p
When the MySQL prompt appears, run:
CREATE DATABASE nextcloud; CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'your-strong-password'; GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost'; FLUSH PRIVILEGES; EXIT;
Replace 'your-strong-password' with a secure password. This creates a dedicated database and user for Nextcloud.
Step 4: Configure PHP#
sudo nano /etc/php/7.4/apache2/php.ini
For Ubuntu 22.04, use 8.1 instead of 7.4 in the path.
Find and modify these values:
memory_limit = 512M upload_max_filesize = 200M max_execution_time = 360 post_max_size = 200M date.timezone = Your/Timezone
Replace Your/Timezone with your actual timezone (e.g., America/New_York). These settings optimize PHP for file uploads and Nextcloud operations.
Step 5: Download and Extract Nextcloud#
cd /tmp wget https://download.nextcloud.com/server/releases/latest.tar.bz2
This downloads the latest stable Nextcloud version.
Extract and move to web directory:
sudo tar -xjf latest.tar.bz2 sudo mv nextcloud /var/www/html/
This extracts Nextcloud and places it in the Apache web root directory.
Step 6: Set Correct Permissions#
sudo chown -R www-data:www-data /var/www/html/nextcloud/ sudo chmod -R 755 /var/www/html/nextcloud/
This ensures the web server can access and modify Nextcloud files. Incorrect permissions will cause installation or operation failures.
Step 7: Configure Apache for Nextcloud#
sudo nano /etc/apache2/sites-available/nextcloud.conf
Add this configuration:
<VirtualHost *:80>
ServerName your-domain.com
DocumentRoot /var/www/html/nextcloud/
<Directory /var/www/html/nextcloud/>
Options +FollowSymlinks
AllowOverride All
Require all granted
<IfModule mod_dav.c>
Dav off
</IfModule>
SetEnv HOME /var/www/html/nextcloud
SetEnv HTTP_HOME /var/www/html/nextcloud
</Directory>
ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined
</VirtualHost>
Replace your-domain.com with your actual domain name.
Enable the site and required Apache modules:
sudo a2ensite nextcloud.conf sudo a2enmod rewrite headers env dir mime setenvif ssl sudo systemctl restart apache2
This activates the Nextcloud site and enables necessary Apache modules.
Step 8: Set Up SSL with Let's Encrypt#
sudo apt install certbot python3-certbot-apache -y sudo certbot --apache -d your-domain.com
Replace your-domain.com with your actual domain. This obtains and installs a free SSL certificate.
Step 9: Complete the Web-Based Installation#
Open your web browser and navigate to https://your-domain.com. You'll see the Nextcloud setup page where you'll enter:
- Admin username and password
- Data folder: Keep the default
/var/www/html/nextcloud/data - Database user:
nextclouduser - Database password: The password you set in Step 3
- Database name:
nextcloud - Database host:
localhost
Click "Finish setup" to complete the installation.
Method 2: Snap Package Installation (Simplified Approach)#
Step 1: Install Snapd#
sudo apt update sudo apt install snapd -y
This installs the snap package manager.
Step 2: Install Nextcloud via Snap#
sudo snap install nextcloud
This downloads and installs Nextcloud with all dependencies.
Step 3: Create Admin Account#
sudo nextcloud.manual-install admin "your-strong-password"
Replace your-strong-password with a secure password. This creates the admin account.
Step 4: Configure Domain#
sudo nextcloud.occ config:system:set trusted_domains 1 --value=your-domain.com
Replace your-domain.com with your actual domain.
Step 5: Enable HTTPS#
sudo nextcloud.enable-https lets-encrypt
Enter your email address and agree to the terms when prompted. This obtains and installs a free SSL certificate.
Security Hardening#
Configure Basic Firewall#
sudo apt install ufw -y sudo ufw allow 22/tcp sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw enable
This installs and configures UFW firewall, allowing only SSH and web traffic.
Protect Against Brute Force Attacks with Fail2ban#
sudo apt install fail2ban -y sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local sudo nano /etc/fail2ban/jail.local
Add to the end of the file:
[nextcloud] enabled = true port = 80,443 filter = nextcloud logpath = /var/log/apache2/nextcloud_access.log maxretry = 3 bantime = 86400
For snap installations, use /var/snap/nextcloud/current/logs/nextcloud.log as the logpath.
Create a filter:
sudo nano /etc/fail2ban/filter.d/nextcloud.conf
Add:
[Definition] failregex = ^.*Login failed: '.*' \(Remote IP: ''.*$ ignoreregex =
Restart Fail2ban:
sudo systemctl restart fail2ban
This protects your login page from repeated password attempts.
Data Management and Backups#
Configure External Storage (Traditional Installation)#
sudo mkdir -p /var/nextcloud-data sudo chown -R www-data:www-data /var/nextcloud-data sudo chmod -R 750 /var/nextcloud-data
Edit Nextcloud config:
sudo nano /var/www/html/nextcloud/config/config.php
Update the datadirectory parameter:
'datadirectory' => '/var/nextcloud-data',
Automated Backup Script (Traditional Installation)#
sudo nano /usr/local/bin/nextcloud-backup.sh
Add this script:
#!/bin/bash BACKUP_DATE=$(date +"%Y%m%d") BACKUP_DIR="/var/backups/nextcloud" # Create backup directory if it doesn't exist mkdir -p $BACKUP_DIR # Enable maintenance mode cd /var/www/html/nextcloud sudo -u www-data php occ maintenance:mode --on # Backup database mysqldump --single-transaction -u nextclouduser -p'your-strong-password' nextcloud > $BACKUP_DIR/nextcloud-db-$BACKUP_DATE.sql # Backup data directory tar -czf $BACKUP_DIR/nextcloud-data-$BACKUP_DATE.tar.gz -C /var /var/nextcloud-data # Backup config directory tar -czf $BACKUP_DIR/nextcloud-config-$BACKUP_DATE.tar.gz -C /var/www/html/nextcloud config # Disable maintenance mode sudo -u www-data php occ maintenance:mode --off # Remove backups older than 30 days find $BACKUP_DIR -name "nextcloud-*" -type f -mtime +30 -delete
Make it executable and schedule:
sudo chmod +x /usr/local/bin/nextcloud-backup.sh sudo crontab -e
Add this line for daily 2AM backups:
0 2 * * * /usr/local/bin/nextcloud-backup.sh
Snap Installation Backup#
sudo nextcloud.export
This creates a complete backup in /var/snap/nextcloud/common/backups/.
Schedule automated backups:
sudo crontab -e
Add:
0 2 * * * /snap/bin/nextcloud.export
Troubleshooting Common Issues#
Fix Permission Denied Errors#
sudo chown -R www-data:www-data /var/www/html/nextcloud/
sudo find /var/www/html/nextcloud/ -type d -exec chmod 750 {} \;
sudo find /var/www/html/nextcloud/ -type f -exec chmod 640 {} \;
This resets ownership and permissions to correct values.
Resolve Database Connection Issues#
sudo mysql -u root -p
Then run:
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost'; FLUSH PRIVILEGES; EXIT;
This ensures the database user has proper permissions.
Fix PHP Memory Limit Errors#
sudo nano /etc/php/7.4/apache2/php.ini
For Ubuntu 22.04, use 8.1 instead of 7.4.
Change the memory limit:
memory_limit = 1G
Restart Apache:
sudo systemctl restart apache2
This increases PHP memory to handle larger operations.
Improve Performance with Redis Caching#
sudo apt install redis-server php-redis -y sudo systemctl enable redis-server sudo systemctl start redis-server
Edit Nextcloud config:
sudo nano /var/www/html/nextcloud/config/config.php
Add inside the configuration array:
'memcache.local' => '\\OC\\Memcache\\Redis',
'memcache.locking' => '\\OC\\Memcache\\Redis',
'redis' => [
'host' => 'localhost',
'port' => 6379,
],
Restart Apache:
sudo systemctl restart apache2
This significantly improves performance through caching.
Maintenance Tasks#
Update Nextcloud (Traditional Installation)#
cd /var/www/html/nextcloud sudo -u www-data php occ maintenance:mode --on
Backup first:
sudo mysqldump --single-transaction -u nextclouduser -p'your-strong-password' nextcloud > /var/backups/nextcloud-db-before-update.sql sudo tar -czf /var/backups/nextcloud-before-update.tar.gz -C /var/www/html nextcloud
Download and update:
cd /tmp wget https://download.nextcloud.com/server/releases/latest.tar.bz2 sudo tar -xjf latest.tar.bz2 sudo cp -r nextcloud/. /var/www/html/nextcloud/ sudo chown -R www-data:www-data /var/www/html/nextcloud/
Complete the upgrade:
cd /var/www/html/nextcloud sudo -u www-data php occ upgrade sudo -u www-data php occ maintenance:mode --off
Update Snap Installation#
sudo snap refresh nextcloud
This updates Nextcloud to the latest version.
Monitor Your Installation#
Check status:
# For traditional installation cd /var/www/html/nextcloud sudo -u www-data php occ status # For snap installation sudo nextcloud.occ status
View logs:
# For traditional installation sudo tail -f /var/log/apache2/nextcloud_error.log # For snap installation sudo snap logs nextcloud
These commands help diagnose issues.
Conclusion#
You now have a fully functional, secure Nextcloud installation on your Ubuntu server. Your self-hosted cloud provides a private alternative to commercial services with similar features.
Remember to:
- Keep your system updated regularly
- Back up your data frequently
- Monitor logs for unusual activity
- Follow security best practices
For basic usage, you can stop after completing either installation method and securing with SSL. For production environments, consider implementing the security hardening and performance optimization steps.