How to Install Nextcloud on Ubuntu 24.04/22.04/20.04

Install Nextcloud on Ubuntu Self-hosted cloud storage guide 24.04/22.04

Introduction

Nextcloud is an open-source, self-hosted file sharing and collaboration platform designed for secure data storage and communication. It provides a robust alternative to commercial cloud services, offering complete control over your data. This guide will walk you through the installation of Nextcloud on Ubuntu 24.04/22.04, ensuring your setup is secure and optimized.

Prerequisites

Before starting, ensure you have the following:

  • An Ubuntu 24.04, 22.04 or 20.04 server instance.
  • A non-root user with sudo privileges.
  • A fully qualified domain name (FQDN) pointed to your server’s IP address.
  • Basic knowledge of command-line operations.

Step 1: Update Your System

First, ensure your system is up-to-date. Run the following commands:

$ sudo apt update
$ sudo apt upgrade -y

Step 2: Install Apache Web Server

Nextcloud requires a web server to serve its web interface. We’ll use Apache for this purpose.

$ sudo apt install apache2 -y

Enable and start Apache:

$ sudo systemctl enable apache2
$ sudo systemctl start apache2

Check the status to ensure Apache is running:

$ sudo systemctl status apache2

Step 3: Install PHP and Necessary Extensions

Nextcloud is built on PHP, so we need to install PHP along with several extensions required by Nextcloud.

$ sudo apt install php libapache2-mod-php php-mysql php-gd php-json php-curl php-mbstring php-intl php-imagick php-xml php-zip -y

Check the PHP version to ensure it installed correctly:

$ php -v

Step 4: Install MariaDB Database Server

Nextcloud requires a database to store its data. We will use MariaDB, a popular MySQL fork.

$ sudo apt install mariadb-server -y

Secure the MariaDB installation:

$ sudo mysql_secure_installation

Follow the prompts to set the root password and remove unnecessary users and databases.

Step 5: Create a Database for Nextcloud

Log in to the MariaDB shell as the root user:

$ sudo mysql -u root -p

Create a database and user for Nextcloud:

CREATE DATABASE nextcloud;
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 6: Download and Configure Nextcloud

Navigate to the /var/www directory and download the latest version of Nextcloud:

$ cd /var/www
$ sudo wget https://download.nextcloud.com/server/releases/latest.zip

Extract the downloaded archive:

$ sudo apt install unzip -y
$ sudo unzip latest.zip

Set the correct permissions:

$ sudo chown -R www-data:www-data nextcloud
$ sudo chmod -R 755 nextcloud

Step 7: Configure Apache for Nextcloud

Create a new Apache configuration file for Nextcloud:

sudo nano /etc/apache2/sites-available/nextcloud.conf

Add the following configuration:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/nextcloud
    ServerName your_domain
    <Directory /var/www/nextcloud/>
        Options +FollowSymlinks
        AllowOverride All
        <IfModule mod_dav.c>
            Dav off
        </IfModule>
        SetEnv HOME /var/www/nextcloud
        SetEnv HTTP_HOME /var/www/nextcloud
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
    CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined
</VirtualHost>

Enable the new configuration and the required Apache modules:

$ sudo a2ensite nextcloud.conf
$ sudo a2enmod rewrite headers env dir mime

Restart Apache to apply the changes:

$ sudo systemctl restart apache2

Step 8: Install and Configure SSL

For security, it’s crucial to use SSL/TLS to encrypt the traffic between the server and the clients. We’ll use Certbot to obtain a free SSL certificate from Let’s Encrypt.

Install Certbot and the Apache plugin:

$ sudo apt install certbot python3-certbot-apache -y

Obtain and install the SSL certificate:

$ sudo certbot --apache -d your_domain

Follow the prompts to complete the SSL installation. Certbot will automatically configure Apache to use the new certificate.

Step 9: Complete the Nextcloud Setup in Browser

Open your web browser and navigate to your domain:

https://your_domain

You will be greeted by the Nextcloud setup page. Complete the following steps:

  1. Create an Admin Account: Provide a username and password for the Nextcloud admin account.
  2. Database Configuration: Enter the database details you created earlier:
    • Database User: nextclouduser
    • Database Password: your_password
    • Database Name: nextcloud
  3. Finish Setup: Click “Finish setup” to complete the installation.

Step 10: Secure Your Nextcloud Installation

Configure Trusted Domains

Open the Nextcloud configuration file:

$ sudo nano /var/www/nextcloud/config/config.php

Add your domain to the trusted domains array:

'trusted_domains' => 
array (
  0 => 'localhost',
  1 => 'your_domain',
),

Set Up Cron Job for Background Tasks

Nextcloud requires background tasks to be executed regularly. Set up a cron job to handle this:

$ sudo crontab -u www-data -e

Add the following line to run the cron job every 5 minutes:

*/5 * * * * php -f /var/www/nextcloud/cron.php

Step 11: Optimize Nextcloud Performance

Install and Configure Opcache

Opcache is a PHP extension that caches compiled PHP scripts to improve performance.

Install Opcache:

$ sudo apt install php-opcache -y

Configure Opcache by editing the PHP configuration file:

$ sudo nano /etc/php/8.1/apache2/php.ini

Add or modify the following lines:

opcache.enable=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1

Restart Apache to apply the changes:

$ sudo systemctl restart apache2

Step 12: Additional Configurations

Enable Pretty URLs

To enable user-friendly URLs in Nextcloud, edit the .htaccess file:

$ sudo nano /var/www/nextcloud/.htaccess

Add the following lines at the end:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  RewriteRule ^\.well-known/carddav /remote.php/dav/ [R=301,L]
  RewriteRule ^\.well-known/caldav /remote.php/dav/ [R=301,L]
</IfModule>

Enable mod_rewrite in Apache:

$ sudo a2enmod rewrite
$ sudo systemctl restart apache2

Increase PHP Memory Limit

Edit the PHP configuration file to increase the memory limit for better performance:

$ sudo nano /etc/php/8.1/apache2/php.ini

Set the memory limit to 512MB or higher:

memory_limit = 512M

Restart Apache:

$ sudo systemctl restart apache2

Step 13: Using Nextcloud

Your Nextcloud installation is now complete. You can start using Nextcloud by accessing it via your web browser. Explore its features such as file sharing, calendar, contacts, and various apps that enhance its functionality.

Conclusion

Installing Nextcloud on Ubuntu 24.04/22.04 involves several steps, including setting up a web server, PHP, and a database server, followed by configuring SSL for secure communication. By following this guide, you should have a fully functional Nextcloud instance that provides a robust, self-hosted cloud storage solution. Remember to regularly update your Nextcloud instance and its dependencies to maintain security and performance.

One Reply to “How to Install Nextcloud on Ubuntu 24.04/22.04/20.04”

  • Thank you, after a few guides i followed this one and its spot on, the only difference is the php version, which as of writing this is 8.3. Thanks.

LEAVE A COMMENT