Self-Host Nextcloud on Debian: The Ultimate Step-by-Step Build Guide

Set up your own Nextcloud server on Debian — from scratch! In this step-by-step tutorial, I’ll walk you through the complete installation process, including server preparation, database setup, Apache configuration, PHP tuning, performance tweaks, securing your cloud with Let’s Encrypt (and more). Whether you’re hosting files for personal use, your business, or a homelab, this guide covers everything you need to build a fast, secure, and reliable Nextcloud instance.

You’ll learn how to create a cloud server, configure MariaDB, optimize PHP settings, fix missing indices, enable Redis for speed, and properly secure your deployment. No skipped steps — just a full, detailed setup walkthrough you can follow with confidence.

YouTube player
Read more: Self-Host Nextcloud on Debian: The Ultimate Step-by-Step Build Guide

Video Notes

The following sections contain notes, commands, and config files that were used during this video.

What you’ll need

To have the best experience, you’ll need the following:

A Linux server

You’ll need a Linux server for this project. You can use a physical server, virtual machine, or cloud instance. In the video, a Linode instance is used on Akamai’s cloud platform. If you want to follow along exactly, you’ll need to create a Linode instance. Otherwise, you can “translate” what’s being done in the video to your chosen platform.

Also, Debian 13 “Trixie” is used for the build within the video. Nextcloud can be set up on just about any distribution, but if you don’t choose Debian, then you’ll have to search for alternatives for some of the steps as package names and individual commands change per distro.

Note: This video was NOT sponsored by Akamai/Linode (or anyone else for that matter). While Linode has sponsored Learn Linux TV in the past, they are not currently a sponsor. Linode was chosen only because I already have an account on the platform.

Domain name

While you can use Nextcloud without a domain name, it’s highly recommended that you use one. You can use your favorite domain registrar to register a domain if you don’t already have one.

Note: After you create your Debian server, be sure to add its IP address to your DNS provider to ensure your instance is reachable by its name.

Randomly Generated passwords

Feel free to generate some random passwords ahead of time to use for your build. You’ll need a randomly generated password for each of the following:

  • Debian root user
  • MariaDB root user
  • Nextcloud database
  • Nextcloud admin user

Install updates

The first thing you should do is install all available updates, and it’s important to always do this once you set up any Linux server for the first time.

First, update the package repository index:

apt update

Once that’s done, install any updates that might be available:

apt dist-upgrade

Update hostname

Next, update the hostname of your Linux server to reflect the domain name you decided to use for your build. The fully qualified domain name that was used in the video was nc.learnlinux.cloud (which was used only for the recording, it doesn’t exist now – just replace it with yours).

Update the /etc/hostname file to reflect your chosen name:

nano /etc/hostname

Also, update the /etc/hosts file to reflect your chosen name:

nano /etc/hosts

For example, you might add an entry that looks similar to this:

127.0.1.1  nc.learnlinux.cloud  nc

Create a non-root user

If you don’t already have a standard user account, be sure to add one (it’s not a good idea to continually use the root account). The following command was used to create user jay within the video:

adduser jay

Next, add your user to the sudo group to ensure you can run privileged commands:

usermod -aG sudo jay

Note: Be sure to replace jay with your chosen username.

Reboot

Reboot the server so that it takes advantage of all of the updates:

sudo reboot

Set up MariaDB

For this build, MariaDB was used to provide the required database layer.

Install MariaDB:

To install MariaDB, run the following command:

sudo apt install mariadb-server mariadb-client-compat

Check the status:

Run the following command to ensure MariaDB is running:

systemctl status mariadb

If it’s not, use the following command which will ensure it’s running and also enabled (meaning it will start at boot):

sudo systemctl enable --now mariadb

Implement basic security:

Since MariaDB is a big target for threat actors, we need to secure it. The following command will prompt you for various things to provide minimum security:

sudo mysql_secure_installation

Create a database for Nextcloud:

Next, we’ll create the actual database that Nextcloud will end up using. To create it, enter the MariaDB shell:

sudo mariadb

Then, create the database:

CREATE DATABASE nextcloud;

Make sure it was actually created (it should appear in the list):

SHOW DATABASES;

Next, we’ll need to ensure Nextcloud will have access to its database. The following command will create a “user grant” that Nextcloud will use:

GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY 'mypassword';

Finally, exit the MariaDB shell to return back to the normal command prompt:

exit

Install Apache

For this build, Apache will serve as the web server for Nextcloud. Install these packages (Apache will be installed automatically as a dependency):

sudo apt install imagemagick-7.q16 php php-apcu php-bcmath php-cli php-common php-curl php-gd php-gmp php-imagick php-intl php-mbstring php-mysql php-zip php-xml

Check to make sure Apache is running:

systemctl status apache2

Test Apache

Visit your URL in your browser to make sure the default page loads, you should see the default Apache start page,

Install Nextcloud

At this point, we’ll install some additional dependencies and then copy Nextcloud’s files to the server.

Install required packages:

The following packages are required for Apache to be able to serve Nextcloud properly:

sudo phpenmod apcu bcmath gmp imagick intl unzip

Download Nextcloud:

Next, download Nextcloud’s latest release file to the server (requires the wget package be installed):

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

Unzip Nextcloud:

We’ll need to use the unzip comand to extract files from the downloaded archive (the unzip package will need to be installed for this to work):

unzip latest.zip

Remove the Nextcloud zip file:

We won’t need latest.zip anymore, so feel free to remove it if you don’t plan on using it again:

rm latest.zip

Move the Nextcloud directory to the appropriate place:

Rename the extracted Nextcloud directory to match your server’s FQDN (optional but recommended):

mv nextcloud nc.learnlinux.cloud

Change ownership for the Nextcloud directory so that Apache will have access to it:

sudo chown -R www-data:www-data nc.learnlinux.cloud


Next, move your Nextcloud directory into the /var/www directory:

sudo mv nc.learnlinux.cloud /var/www/

Disable the default Apache site:

Next, you can disable the default Apache start page since we won’t be needing it:

sudo a2dissite 000-default.conf

Apache Virtual Host Configuration

Next, we’ll create a config file that will instruct Apache how to serve Nextcloud.

Create the config file:

sudo nano /etc/apache2/sites-available/nc.learnlinux.cloud.conf

Add the following to the file:

<VirtualHost *:80>
DocumentRoot "/var/www/nc.learnlinux.cloud"
ServerName nc.learnlinux.cloud

<Directory "/var/www/nc.learnlinux.cloud/">
Options MultiViews FollowSymlinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

TransferLog /var/log/apache2/nc.learnlinux.cloud_access.log
ErrorLog /var/log/apache2/nc.learnlinux.cloud_error.log

</VirtualHost>

Enable the new config file:

sudo a2ensite nc.learnlinux.cloud.conf

Set PHP settings

Edit the PHP config file so we can adjust some settings:

sudo nano /etc/php/8.4/apache2/php.ini

Set the following parameters:

memory_limit = 512M
upload_max_filesize = 200M
max_execution_time = 360
post_max_size = 200M
date.timezone = America/Detroit
opcache.enable=1
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1

Enable required modules for Apache:

sudo a2enmod dir env headers mime rewrite ssl

Enable Caching:

Open the apcu.ini file in an editor:

sudo nano /etc/php/8.4/mods-available/apcu.ini

Add the following to the end of the file:

apc.enable_cli=1

Restart Apache to apply our changes so far:

sudo systemctl restart apache2

Set up Nextcloud

To apply the initial configuration, visit your Nextcloud site and answer the questions. You’ll be asked for a password for the admin user, database name, and database password. Add that information, and Nextcloud will be installed!

Post Install: Tweak Nextcloud Database

Although we just ran through the installer, there’s a few things Nextcloud doesn’t do on a fresh install. To implement these tweaks, first mark the occ script executable (be sure to update the path to match yours):

sudo chmod +x /var/www/nc.learnlinux.cloud/occ

Run the following command to add missing databases indices:

sudo /var/www/nc.learnlinux.cloud/occ db:add-missing-indices

Update mimetypes as well:

sudo /var/www/nc.learnlinux.cloud/occ maintenance:repair --include-expensive

Remove execution bit from occ to increase security:

sudo chmod -x /var/www/nc.learnlinux.cloud/occ

Post Install: Update config file permissions

The following commands will increase security for the config file:

Set ownership:

sudo chown root:www-data /var/www/nc.learnlinux.cloud/config/config.php

Set permissions:

sudo chmod 660 /var/www/nc.learnlinux.cloud/config/config.php

Post Install: Enable Caching

To enable caching, first edit the Nextcloud config file:

sudo nano /var/www/nc.learnlinux.cloud/config/config.php

Add to the file:

'memcache.local' => '\\OC\\Memcache\\APCu',
'default_phone_region' => 'US',

Post Install: Set up Let’s Encrypt

To set up a certificate for encryption, we’ll first install Certbot:

sudo apt install python3-certbot-apache

Apply a certificate to your site:

sudo certbot --apache -d nc.learnlinux.cloud

To fully benefit from our certificate, enable strict transport security:

sudo nano /etc/apache2/sites-available/nc.learnlinux.cloud-le-ssl.conf

Add to the config file:

<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
</IfModule>

Post Install: : Install Redis

Redis is optional, but can add performance benefits. To install it:

sudo apt install redis-server php-redis

Edit the Redis config file:

sudo nano /etc/redis/redis.conf

change: Port 6379 to 0

Uncomment: unixsocket /run/redis/redis-server.sock

Uncomment: unixsocketperm 700 (change to 770)

Add the www-data user to the redis group:

sudo usermod -aG redis www-data

Edit the Nextcloud config file to use Redis:

sudo vim /var/www/nc.learnlinux.cloud/config/config.php

Add the following to the file:

  'filelocking.enabled' => true,
'memcache.locking' => '\\OC\\Memcache\\Redis',
'redis' =>
array(
'host' => '/var/run/redis/redis-server.sock',
'port' => 0,
'timeout' => 0.0,
),

Restart Apache to apply our final changes:

sudo systemctl restart apache2