VPS & Servers

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 running on an Ubuntu server

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:

  1. The database — mysqldump nextcloud
  2. The data folder — the actual files
  3. 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

Questions people ask

Why do I get "Internal Server Error" after installing NextCloud?
Almost always a missing PHP extension. Run tail -f on /var/log/apache2/nextcloud_error.log to see the real error, install whatever it names, then restart Apache. The most commonly missed are php-gmp, php-bcmath and php-imagick.
How much RAM does NextCloud need?
Two gigabytes is the practical minimum. One gigabyte installs and then crawls. If you add Talk for video calls or Collabora for document editing, plan on 4 GB or more.
Why is my NextCloud so slow?
Almost certainly no memory cache. Add the APCu line to config.php, or install Redis for a busier server. This single change makes more difference than anything else, and it is the setting most installs never get.
Why do large file uploads keep failing?
PHP limits. You must raise memory_limit, upload_max_filesize, post_max_size, max_execution_time and max_input_time together. Changing only one is the usual mistake, and then restart Apache.
What does "You are accessing the server from an untrusted domain" mean?
NextCloud only responds on domains listed in the trusted_domains array in config/config.php. Add your domain there. It is a deliberate protection against host header attacks, not a bug.
Do I need to set up cron?
Yes. The default AJAX method only runs background jobs while someone has a page open, so cleanup, sharing and notifications silently never happen. Add a crontab entry for www-data running cron.php every five minutes.
Can I install NextCloud on shared hosting?
It will install but you will regret it. Shared hosting caps PHP memory, blocks the cron jobs NextCloud depends on, and limits upload size. Use a VPS with at least 2 GB of RAM.
How do I back up NextCloud properly?
Three things, not one: the database via mysqldump, the entire data folder, and config/config.php. Put the server into maintenance mode with occ before copying, so nothing changes halfway through.
Why does my desktop sync client say connection refused?
Missing or self-signed SSL. Desktop and mobile clients are stricter than browsers and refuse outright where a browser would only show a warning. Install a proper Let's Encrypt certificate.
Can I skip versions when updating NextCloud?
No. Upgrading must be done one major version at a time. The updater refuses to skip, and forcing it leaves you repairing the installation by hand. Always back up before any upgrade.

Written by Habib Baloch

I build Android apps, websites and Ubuntu servers, and write down exactly how I did it.

Ask me about this guide →