How to Install and Configure NextCloud on Ubuntu Server: A Complete Tutorial

How to Install and Configure NextCloud on Ubuntu Server: A Complete Tutorial

Home ยป Cookbook ยป How to Install and Configure NextCloud on Ubuntu Server: A Complete Tutorial
Set up your own private cloud with NextCloud on Ubuntu! Follow my step-by-step guide to install, configure, and secure your server easily!
Table of Contents

In todayโ€™s world, data privacy and control are becoming increasingly important. NextCloud provides an open-source solution for creating your own private cloud storage service. This allows you to store, sync, and share files securely across devices, all while maintaining full control over your data. In this step-by-step guide, we will walk you through the process of installing and configuring NextCloud on an Ubuntu server, ensuring a smooth setup with all necessary components.

Whether youโ€™re setting up NextCloud for personal use or as a business solution, this tutorial will cover everything you need to know to get started with your private cloud storage.

Prerequisites

Before starting, make sure you have the following:

  • A server running Ubuntu 22.04 or later.
  • Root or sudo access to the server.
  • A domain name (optional, but recommended for public access).
  • An SSL certificate (weโ€™ll cover this in the tutorial).

Step-by-Step Guide to Installing NextCloud

Here’s the first part of the instruction manual based on the images you have shared. I will detail each step and explain the reasoning behind it.

Step 1: Update and Upgrade the System

SSH Command
sudo apt update -y && sudo apt upgrade -y

Keeping your system up-to-date is essential for security and stability. apt update refreshes the package list, while apt upgrade installs the latest versions of the installed packages. The -y flag confirms all prompts automatically.

https://vast.yomeno.xyz/vast?spot_id=1422790

Step 2: Install Apache Web Server

SSH Command
sudo apt install apache2

Apache is one of the most popular web servers. Here, we install it so that it can serve the NextCloud application to users via a web browser.

Step 3: Enable and Start Apache, then Verify Status

SSH Command
sudo systemctl enable apache2 && sudo systemctl start apache2

After installing Apache, we need to start the service and enable it to automatically start on boot. systemctl is used to manage services in modern Linux distributions. Enabling it ensures that the web server will restart automatically after a system reboot.

SSH Command
sudo systemctl status apache2

To ensure that Apache is running correctly, we use this command to check its status. This should return an “active (running)” status, indicating the web server is operational.

Step 4: Install PHP and Extensions Required by NextCloud

SSH Command
sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-imap php8.1-redis php8.1-snmp php8.1-xml php8.1-zip php8.1-mbstring php8.1-curl php8.1-gd php8.1-mysql

NextCloud requires PHP to run, along with several additional PHP extensions such as xml, zip, gd, and mysql. This command installs PHP 8.1 and all the necessary modules to ensure that NextCloud functions correctly.

Step 5: Install MariaDB (MySQL Alternative) for Database

SSH Command
sudo apt install mariadb-server

NextCloud requires a database to store its data. MariaDB is a popular, open-source database that is fully compatible with MySQL, making it a suitable backend for NextCloud. This command installs MariaDB on the server.

Step 6: Enable, Start MariaDB, and Verify the Status

SSH Command
sudo systemctl start mariadb && sudo systemctl enable mariadb

After installing MariaDB, we start the service and enable it to start automatically at boot. Similar to Apache, this ensures that the database service remains running even after the server restarts.

SSH Command
sudo systemctl status mariadb

This command checks if MariaDB is running properly. You should see an “active (running)” status in the output.

Step 7: Configure the Database for NextCloud

SSH Command
sudo mysql

Once inside the MariaDB shell:

MySQL Command
CREATE DATABASE nextcloud;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY 'YourStrongPasswordHere';
FLUSH PRIVILEGES;
EXIT;

In this step, we create a new database named nextcloud. We also create a user named nextcloud with full privileges over this database. The FLUSH PRIVILEGES; command reloads the privileges, ensuring the new user has the required access. Don’t forget to replace YourStrongPasswordHere with a secure password.

Step 8: Download NextCloud

Navigate to the Web Directory:

SSH Command
cd /var/www/html

We start by navigating to the /var/www/html directory, which is the default web root for Apache web server on Ubuntu. This is where we will install NextCloud, so it can be accessible through the web.

Download the Latest Version of NextCloud:

SSH Command
sudo wget https://download.nextcloud.com/server/releases/latest.zip

Using wget, we download the latest release of NextCloud in a zip format. This ensures that you’re always working with the most up-to-date version of the software.

Step 9: Unzip NextCloud Package

If you don’t already have it, Install Unzip Utility:

SSH Command
sudo apt install zip -y

Since the NextCloud package is a .zip file, we need the unzip utility to extract the files. If itโ€™s not installed by default on your server, this command installs it. The -y flag is used to auto-confirm the installation.

Unzip NextCloud Package:

SSH Command
sudo unzip latest.zip -d /var/www/html/

This command extracts the latest.zip file into the /var/www/html directory, placing all of NextCloudโ€™s files in the nextcloud folder. The -d option specifies the target directory where the files should be unzipped.

Step 10: Move NextCloud Files to the Root Directory and Remove Unnecessary Files

SSH Command
sudo mv /var/www/html/nextcloud/* /var/www/html/

After extracting the NextCloud files into the nextcloud directory, we now move them to the web root (/var/www/html/). This allows the application to be accessed directly without the need for a subdirectory in the URL.

SSH Command
sudo rm index.html latest.zip

The index.html file is the default Apache page, and latest.zip is the NextCloud archive we downloaded earlier. Both are no longer needed, so they are removed to keep the directory clean.

Step 11: Set Correct Permissions

SSH Command
sudo chown -R www-data:www-data /var/www/html/

NextCloud files must be owned by the www-data user and group (the default user that Apache runs under) to ensure the web server can read and write to the necessary files. The -R flag applies the changes recursively to all files and subdirectories.

Step 12: Create and Configure Apache Virtual Host

Apache Virtual Host Creation

SSH Command
sudo touch /etc/apache2/sites-available/nextcloud.conf
sudo vi /etc/apache2/sites-available/nextcloud.conf

We create a new Apache configuration file for NextCloud in the sites-available directory, then open it with vi for editing. This file will configure the Virtual Host for NextCloud.

Configure Apache Virtual Host for NextCloud

Inside the nextcloud.conf file, add the following then hit Escape and then :wq to save and exit the editor:

nextcloud.conf
<VirtualHost *:80>
    ServerName domain.com
    DocumentRoot /var/www/html/

    <Directory /var/www/html/>
        Require all granted
        Options FollowSymlinks MultiViews
        AllowOverride All
        <IfModule mod_dav.c>
            Dav off
        </IfModule>
    </Directory>

    ErrorLog /var/log/apache2/domain.com.error_log
    CustomLog /var/log/apache2/domain.com.access_log common
</VirtualHost>

This Apache Virtual Host configuration tells Apache to serve NextCloud on port 80 for the domain nextcloud.roosho.link (Here use your own domain). It sets the document root to /var/www/html, where NextCloud files are located, and configures logging for the domain. The AllowOverride All directive allows NextCloud to control .htaccess files, necessary for rewriting URLs.

Step 13: Enable Apache Site and Rewrite Module

SSH Command
sudo a2ensite nextcloud.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

The a2ensite command enables the newly created NextCloud site configuration. a2enmod rewrite enables Apacheโ€™s mod_rewrite, which is crucial for NextCloud to handle URL rewriting. After enabling these modules and configurations, we restart Apache to apply the changes.

Step 14: Access the NextCloud Setup in Browser for Initial Configuration

http://<ip address> 

After the initial server configuration, you can access your NextCloud instance by typing the serverโ€™s IP address or domain into your browser. This will open the NextCloud setup page where you can create an admin account and configure the data folder location.

Initial Configuration

On the setup page:

Create an Admin Account
  1. Username: Set the admin username (e.g., roosho).
  2. Password: Create a secure password.
  3. Data Folder: Confirm the data folder path as /var/www/html/data.

During the first access to the NextCloud setup page, you will create an admin account that will have full control over the instance. The data folder is where all user files will be stored. Itโ€™s important to confirm that the folder is correctly set to /var/www/html/data (or your preferred data folder).

Configure the Database
  1. Database Type: Select MySQL/MariaDB.
  2. Database User: Enter nextcloud (the user you created earlier in MariaDB).
  3. Database Password: Enter YourStrongPasswordHere, the password you defined for the nextcloud database user.
  4. Database Name: nextcloud (the name of the database you created earlier).
  5. Database Host: localhost.

NextCloud requires a database to store its metadata. Here you provide the necessary credentials to connect NextCloud to the MariaDB database you previously set up.

SSL Certificate Configuration for NextCloud

Step 1: Edit NextCloud Configuration File

SSH Command
sudo vi /var/www/html/config/config.php
add your domain or hostname in the array

This step opens the NextCloud configuration file using the vi editor. In this file, you need to define the trusted domains, which includes your server’s IP and any domain names that will be used to access the NextCloud instance. You add the domain nextcloud.roosho.link under the trusted_domains array to allow access from this URL.

Step 2: Install Certbot for SSL Certificate

Update the system as a good SysAdmin:

SSH Command
sudo apt update -y

Running apt update ensures that all package lists are updated. This step guarantees that the latest software versions are installed.

Install Certbot for SSL Certificate:

SSH Command
sudo apt install certbot python3-certbot-apache -y

Certbot is a tool used to obtain free SSL certificates from Letโ€™s Encrypt. The package python3-certbot-apache enables Certbot to work with Apache to automatically configure SSL.

Step 3: Obtain and Install SSL Certificate

SSH Command
sudo certbot --apache -d domain.com

This command tells Certbot to obtain an SSL certificate for nextcloud.roosho.link and configure it automatically for Apache. Once completed, your NextCloud site will be served over HTTPS, securing communication between users and the server.

Login to NextCloud to Verify NextCloud Interface

Once setup is complete, you can log in at:

https://domain.com

Use the admin credentials (In my case the URL: nextcloud.roosho.link username: roosho, password: your chosen password) to log in.

After completing the setup process, you can now access and log into your NextCloud instance via the browser. This marks the final step of installation, giving you access to the NextCloud dashboard where you can begin uploading files, configuring settings, and adding users.

After logging in, you will see the NextCloud dashboard, as shown in the screenshot. Here, you can:

  • View and manage your files and folders.
  • Use features like sharing, tagging, and version control.
  • Customize your NextCloud instance settings.

The dashboard is where you will spend most of your time managing NextCloud. Itโ€™s a web interface for uploading, sharing, and organizing files and managing user accounts and system settings.

Conclusion

Congratulations! Youโ€™ve successfully installed and configured NextCloud on your Ubuntu server. With NextCloud, you can securely sync, share, and manage files across all your devices, all while retaining full control over your data. This tutorial covers all the essential steps, from server setup to the installation of necessary software and configuration of your NextCloud instance.

With your NextCloud server up and running, you’re now free to explore its features and capabilities, whether for personal use or to provide a secure cloud solution for your business. Happy clouding!

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.ย 
share this article.

Enjoying my articles?

Sign up to get new content delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.
Name