How to Install NextCloud on Ubuntu with Apache2
A complete NextCloud install on Ubuntu: PHP extensions, MySQL, Apache virtual host, SSL, and the four post-install settings that stop it being slow.
NextCloud gives you your own Google Drive: file sync, sharing, calendar, contacts and optional chat and video calls, all on a server you control.
The install takes about forty minutes. Getting it to run well takes four extra settings that most guides leave out — and without them NextCloud feels sluggish and people assume that is just how it is.
What your server needs
- Ubuntu 22.04 or 24.04 on a VPS
- 2 GB RAM minimum. 1 GB installs and then crawls. 4 GB if you add Talk or document editing.
- A domain pointing at the server
- Disk space for the files — see the file sharing guide for the storage cost maths
Shared hosting will not work properly. It blocks the background jobs NextCloud needs and caps upload size far too low.
Step 1: Update and install Apache
sudo apt update
sudo apt upgrade -y
sudo apt install apache2 -y
If this is a fresh server, work through the LAMP guide first — it covers creating a non-root user and setting the firewall, both of which matter here.
Step 2: Install PHP and every extension NextCloud needs
This is where most installs fail. A single missing extension produces "Internal Server Error" with nothing useful on screen.
sudo apt install php php-cli php-fpm php-json php-common php-mysql
php-zip php-gd php-intl php-curl php-xml php-mbstring php-bcmath
php-gmp php-imagick php-apcu libapache2-mod-php unzip -y
Check the version — NextCloud needs PHP 8.1 or newer:
php -v
Raise the PHP limits
The defaults make uploading anything larger than a document impossible.
sudo nano /etc/php/8.3/apache2/php.ini
Find and change these five:
memory_limit = 512M
upload_max_filesize = 2G
post_max_size = 2G
max_execution_time = 3600
max_input_time = 3600
All five matter. Changing only upload_max_filesize and wondering why large uploads still fail is a very common half-hour.
Step 3: Create the database
sudo apt install mysql-server -y
sudo mysql_secure_installation
sudo mysql
Then:
CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
The utf8mb4 character set is not optional — without it, filenames with emoji or non-Latin characters break.
Step 4: Download 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
That ownership line matters. Apache runs as www-data, and without it NextCloud cannot write anything.
Step 5: Configure Apache
sudo nano /etc/apache2/sites-available/nextcloud.conf
<VirtualHost *:80>
ServerName cloud.yourdomain.com
DocumentRoot /var/www/nextcloud
<Directory /var/www/nextcloud/>
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews
<IfModule mod_dav.c>
Dav off
</IfModule>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined
</VirtualHost>
Dav off is important — Apache's own WebDAV module conflicts with NextCloud's, and sync clients fail in confusing ways if you leave it on.
Enable the site and the modules:
sudo a2ensite nextcloud.conf
sudo a2enmod rewrite headers env dir mime setenvif
sudo apache2ctl configtest
sudo systemctl restart apache2
Always run configtest before restarting. Anything other than Syntax OK means fix it first.
Step 6: SSL
Do this before the web installer, not after. Sync clients refuse to connect without a valid certificate.
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d cloud.yourdomain.com
Say yes when it offers to redirect HTTP to HTTPS. Full details in the SSL guide.
Step 7: Run the web installer
Open https://cloud.yourdomain.com and fill in:
- Admin account — not "admin", and a long password
- Data folder — leave the default unless you have a reason
- Database — MySQL, with the details from Step 3, host
localhost
Installation takes a few minutes. Do not refresh while it runs.
Step 8: The four settings that decide performance
NextCloud works after install but performs badly until these are done. Go to Administration settings → Overview, which lists the warnings.
1. Background jobs via cron
The default AJAX method only runs when somebody has a page open, so cleanup, sharing and notification jobs silently never happen.
sudo crontab -u www-data -e
Add:
*/5 * * * * php -f /var/www/nextcloud/cron.php
Then switch the method to Cron in Administration settings → Basic settings.
2. Memory caching
Without this, every page load is noticeably slow. This is the single biggest performance change available.
sudo nano /var/www/nextcloud/config/config.php
Add inside the array:
'memcache.local' => '\OC\Memcache\APCu',
For a busier server, install Redis and use that instead.
3. Trusted domains
If you see "You are accessing the server from an untrusted domain", add it:
'trusted_domains' =>
array (
0 => 'cloud.yourdomain.com',
),
4. Region and phone region
Set the default phone region in config.php, otherwise share links and contacts generate with wrong formatting:
'default_phone_region' => 'PK',
Step 9: Useful extras
- Talk — chat, voice and video calls. See the calling guide.
- Collabora or OnlyOffice — edit documents in the browser. Both need real server resources.
- Two-factor authentication — switch it on for admin accounts at minimum.
- Per-user quotas — so one person cannot fill the disk for everyone.
Backups
You are holding other people's files now. A backup of only the database is not a backup.
You need all three:
- The database —
mysqldump nextcloud - The data folder — the actual files
config/config.php— without it, restoring is guesswork
Put NextCloud in maintenance mode while backing up, so nothing changes mid-copy:
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
# … run the backup …
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off
Common problems
"Internal Server Error" after install
Almost always a missing PHP extension. Check the real error:
sudo tail -f /var/log/apache2/nextcloud_error.log
Install whatever it names, then restart Apache.
Large uploads fail at the same size every time
PHP limits. All five values in Step 2 need raising, not just one. Restart Apache afterwards.
Everything is slow
No memory cache. Add the APCu line and the difference is immediate.
"You are accessing the server from an untrusted domain"
Add the domain to trusted_domains in config.php.
Sync client says "connection refused"
Missing or self-signed SSL. Desktop clients are stricter than browsers and will refuse where a browser only warns.
The disk keeps filling up
Deleted files sit in the trash and old versions are kept indefinitely. Set retention limits in the admin settings.
occ commands fail with permission errors
Run them as the web user:
sudo -u www-data php /var/www/nextcloud/occ status
Keeping it updated
NextCloud tells you when an update is available. Use the built-in updater, and always back up first.
Skipping versions is not supported. Upgrade one major version at a time, or the updater refuses and you end up repairing by hand.
Where to go next
- Run it as a public file sharing service — storage costs and security
- Add voice and video calls
- SSL setup in full