How to Install NextCloud on Debian 9 Stretch with LAMP

This tutorial is going to show you how to install NextCloud on Debian 9 with LAMP stack. NextCloud is a free and open-source self-hosted cloud storage solution, providing an alternative to third-party cloud storage services like Dropbox.


To follow this tutorial, you need the following:

Step 1: Download NextCloud

Download the NextCloud server zip archive onto your Debian 9 system with the following command. The latest stable version is 12.0.2 at time of this writing.


There may be a newer version when you follow this tutorial. Go to and click the download button to check out the latest version. To get the download link of NextCloud, simply right-click on Download NextCloud button and select Copy Link Location as shown in the screenshot below.

nextcloud download

Extract it to /var/www/ directory with unzip.

sudo apt install unzip

sudo unzip -d /var/www/

Now we make www-data (Apache user) as the owner of /var/www/nextcloud/.

sudo chown www-data:www-data /var/www/nextcloud -R

Step 2: Create a Database and User in MariaDB

Log into MariaDB database server with the following command:

sudo mariadb -u root

Then create a database for NextCloud using the MariaDB command below. This tutorial name the database nextcloud. You can use whatever name you like. Don’t leave out the semicolon at the end.

create database nextcloud;

Then create a separate user. Again, you can use your preferred name for this user. Replace your-password with your preferred password.

grant all privileges on nextcloud.* to nextclouduser@localhost identified by 'your-password';

The above command will create the user and grant all privileges. Now flush MariaDB privileges and exit.

flush privileges;


Step 3: Enable Binary Logging in MariaDB

Edit MariaDB configuration file.

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Add the following three lines in [mysqld] section.

log-bin        = /var/log/mysql/mariadb-bin
log-bin-index  = /var/log/mysql/mariadb-bin.index
binlog_format  = mixed

The format of binary log must be mixed. Save and close the file. Then restart MariaDB service.

sudo systemctl restart mariadb

Now binary log is enabled in MariaDB.

Step 4: Create an Apache Virtual Host File for Nextcloud

We will create a nextcloud.conf file in /etc/apache2/sites-available directory.

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

Copy and paste the following lines in the file. Replace the red text with your actual domain name. You also need to point your domain name to the IP address of your Debian 9 server in DNS.

<VirtualHost *:80>
 DocumentRoot "/var/www/nextcloud"

 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www/nextcloud/>
 Options +FollowSymlinks
 AllowOverride All

 <IfModule mod_dav.c>
 Dav off

 SetEnv HOME /var/www/nextcloud
 SetEnv HTTP_HOME /var/www/nextcloud
 Satisfy Any



Save and close the file. Then enable this virtual host using the command below.

sudo a2ensite nextcloud

We also need to enable some Apache modules.

sudo a2enmod rewrite headers env dir mime setenvif ssl

Install needed PHP modules.

sudo apt install php7.0-common php7.0-mysql php7.0-gd php7.0-json php7.0-curl php7.0-zip php7.0-xml php7.0-mbstring

Restart Apache so that the above Apache and PHP modules can be loaded.

sudo systemctl restart apache2

Step 5: Enabling HTTPS

Now you can access the Nextcloud web install wizard in your browser by entering the domain name for your Nextcloud installation.


Before entering any sensitive information, we should enable secure HTTPS connection on Nextcloud. We can obtain a free TLS certificate from Let’s Encrypt.

Install certbot (Let’s Encrypt) client from Debian 9 repository.

sudo apt install certbot python-certbot-apache

Now issue the following command to obtain a free TLS/SSL certificate. Replace the red-colored text with your actual data.

sudo certbot --apache --agree-tos --redirect --hsts --email your-email-address -d


  • –apache: Use the apache plugin.
  • –agree-tos: Agree terms of service.
  • –redirect: Automatically redirect all HTTP traffic to HTTPS.
  • –hsts: Add the Strict-Transport-Security header.

Within a few seconds, you shall see a message like below, which means the TLS certificate was successfully obtained and installed. The TLS configuration scores A in SSL test.

nextcloud https setup

Finish the Installation in your Web Browser

Now in your web browser address bar, type in your NextCloud domain name. You will see that it’s automatically redirected to https.

nextcloud debian 9 install with lamp

You will need to create an admin account. The data folder is where user’s files are stored. For security reasons, it’s best to place the data directory outside of Nextcloud web root, such as /var/www/nextcloud-data. which can be created with the following command:

sudo mkdir /var/www/nextcloud-data/

Then make sure the Apache user (www-data) has write permission to the data directory.

sudo chown www-data:www-data /var/www/nextcloud-data -R

Then enter the database username, database name and password you created earlier to connect Nextcloud to MariaDB database.

Once it’s done, you will see the Web interface of Nextcloud. Congrats! You can now start using it as your private cloud storage.

nextcloud debian 9 apache

I hope this tutorial helped you install NextCloud on Debian 9 with LAMP stack. As always, if you found this post useful, then subscribe to our free newsletter to get new tutorials.

Rate this tutorial
[Total: 5 Average: 5]