How to Install Nextcloud on Ubuntu 16.04: Apache, MariaDB, PHP, SSL, and Secure Initial Setup

How to Install Nextcloud on Ubuntu 16.04

Nextcloud is one of the most widely used self-hosted platforms for private file storage, synchronization, sharing, calendars, contacts, and lightweight collaboration. It is especially useful for businesses and users who do not want to rely only on public cloud providers and who value control over data location, access rules, backups, and server configuration. Installing Nextcloud on Ubuntu 16.04 is still possible, but because Ubuntu 16.04 is an older operating system, you need to be careful about PHP compatibility, security posture, and long-term maintenance planning.

In practical terms, the installation consists of several layers: a web server, PHP, a database, and the Nextcloud application itself. After that come file permissions, HTTPS setup, background jobs, and a few basic hardening steps. This kind of environment is commonly deployed on Virtual Servers, because a VPS gives you direct control over the operating system and resource allocation. For larger teams or heavier storage workloads, Dedicated Servers may be a better fit. Since Nextcloud should always run over HTTPS, SSL Certificates are also a fundamental part of the design.

Before you begin, decide whether you are installing Nextcloud on a clean Ubuntu 16.04 server or on a system that already hosts other applications. A clean server makes the process easier. If Apache virtual hosts, PHP settings, or databases already exist, you need to be more careful about conflicts. It is also best to have a real hostname ready from the start, for example cloud.example.com, because Nextcloud works much more cleanly with a proper domain than with a raw IP address.

1) Update the system and install required packages

Start by updating Ubuntu 16.04. Even though it is an older distribution, you still want the installed package set to be internally consistent. You will need Apache, MariaDB, PHP, and a group of PHP extensions that Nextcloud depends on for uploads, database access, image handling, and XML or ZIP support. A few utility packages such as unzip, wget, and curl are also helpful during installation and maintenance.

sudo apt update
sudo apt upgrade -y
sudo apt install -y apache2 mariadb-server libapache2-mod-php 
php php-gd php-json php-mysql php-curl php-mbstring php-intl 
php-imagick php-xml php-zip unzip wget curl

After installation, verify that Apache and MariaDB are both running and enabled. It is much easier to solve service-level issues now than later when the application is already layered on top. This is also the right moment to check that the server is listening on port 80 locally and that the basic operating system is in a healthy state.

2) Configure MariaDB and create the Nextcloud database

Nextcloud stores users, file indexes, sessions, sharing metadata, and system configuration in a database, so this layer matters. MariaDB is a common and stable choice. After installation, run mysql_secure_installation to define a stronger base configuration, remove unsafe defaults, and prepare the database server for application use. This step is simple but important from a security standpoint.

sudo mysql_secure_installation

Next, log into MariaDB and create a dedicated database and dedicated user for Nextcloud. Avoid using the database root account for normal application operations. A separate user with permissions limited to one database is cleaner, safer, and easier to audit later.

mysql -u root -p
CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'StrongPasswordHere';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Save these details somewhere secure. You will need them during the web installer, and they are also essential later for backups, migration, or troubleshooting. If you lose track of them, recovery becomes much less convenient.

3) Download Nextcloud and place the files correctly

Now you can download Nextcloud itself. The simplest approach is to fetch the official archive and place it under a dedicated directory such as /var/www/nextcloud. After extraction, file ownership and permissions must be set so Apache can read the application and write to the appropriate storage paths. Incorrect permissions are one of the most common reasons a fresh installation appears to work at first and then fails when users start uploading or modifying data.

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

For better security, the data directory is often kept outside the web root. That means user files do not sit directly under the document root served by Apache. A separate directory such as /var/ncdata is a common choice and reduces the risk of accidental exposure if the web server configuration is changed incorrectly later.

sudo mkdir -p /var/ncdata
sudo chown -R www-data:www-data /var/ncdata
sudo chmod -R 750 /var/ncdata

4) Configure Apache virtual host and enable required modules

Nextcloud works best when Apache is configured explicitly for it. Enable the required modules such as rewrite, headers, env, dir, mime, and ssl. Then create a dedicated virtual host pointing to /var/www/nextcloud with your chosen hostname. This keeps the environment cleaner and makes HTTPS enablement straightforward.

sudo a2enmod rewrite headers env dir mime ssl
sudo nano /etc/apache2/sites-available/nextcloud.conf

    ServerName cloud.example.com
    DocumentRoot /var/www/nextcloud

    
        Require all granted
        AllowOverride All
        Options FollowSymLinks MultiViews
    

    ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
    CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined

After saving the file, enable the site and reload Apache. If you previously relied only on the default site, it is often cleaner to disable that default so there is no confusion about which virtual host is serving requests. Once Apache is reloaded, open the hostname in a browser and confirm that the Nextcloud installer appears.

sudo a2ensite nextcloud.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

5) Finish installation in the browser and configure cron

Open your Nextcloud hostname in a browser and complete the web-based installer. You will be asked to create an admin account, choose the data directory, and enter the database connection details you created earlier. If you prepared a separate data directory outside the web root, this is the point where you should define it. Doing this correctly now avoids the need to move large user data later.

After the initial setup, Nextcloud usually recommends switching background jobs to cron mode. That is the correct choice for a more stable environment. AJAX background mode may work for testing, but cron is much more reliable for indexing, cleanup, notifications, and other internal maintenance tasks.

sudo crontab -u www-data -e
*/5 * * * * php -f /var/www/nextcloud/cron.php

HTTPS, security, and post-install maintenance

Once Nextcloud is running, do not stop at “the page opens”. A real deployment should always use HTTPS. Without encrypted transport, users are trusting the platform with files, credentials, and collaboration data over an insecure path. If you have a public hostname, install an SSL certificate, configure automatic renewal if possible, and verify that HTTP redirects cleanly to HTTPS. You should also review the trusted domains setting in the Nextcloud configuration so the application accepts only the hostnames you actually intend to use.

Performance and maintainability also matter. In addition to the application files, Nextcloud depends on database responsiveness, PHP limits, and the health of its background job system. If you expect larger file uploads or more users, adjust PHP memory_limit, upload size, and execution time accordingly. A private cloud that technically installs but is not tuned for the expected workload will often fail later in ways that feel random to users.

Backups are essential. A proper recovery requires both the database and the files, including the data directory. Backing up only one side is not enough for a clean restore. At a minimum, schedule regular database dumps and archive both the application and user data paths to external storage.

sudo mysqldump -u root -p nextcloud > /tmp/nextcloud.sql
sudo tar -czf /tmp/nextcloud-files.tar.gz /var/www/nextcloud /var/ncdata

Because Ubuntu 16.04 is an older platform, it is also wise to plan ahead. If this server is for a legacy environment or controlled internal use, a careful setup can still be useful. But for long-term production, it is good practice to document the installation and plan eventual migration to a newer Ubuntu version. A solid Nextcloud deployment is not just one that installs successfully once; it is one that can be maintained, secured, backed up, and eventually upgraded without confusion.