Setting up Nextcloud application on a QNAP NAS

In this article, you’ll find everything you need to set up the Nextcloud application on a QNAP NAS using the QNAP Container Station application and, more specifically, Docker.

The set up is specific to the QNAP NAS of the TS-x31P3 Series which has a constraint limiting the use of Docker images in general and consequently blocking the use of the Nextcloud AIO image.

What’s a NAS

A NAS (Network Attached Storage) is a network storage server that stores, backs up, shares, secures and facilitates access to files (photos, videos, music, documents), as well as running applications to add more services (vpn, sftp, web server, etc.).

What’s Nexcloud

Nextcloud is a open source software offering a platform for file storage and sharing services as well as online applications.

Objective

The objective of this approach is to be able to have a private cloud (based on Nextcloud application) allowing the whole family to store, share and manage their data while using an existing QNAP NAS.

The Nextcloud application must be accessible locally using the following HTTPS address: https://110.110.110.151

Note: To have access to the Nextcloud application from Internet, you’ll need to add a NAT rule on your router or Internet box to redirect traffic from ports 80 and 443 to the address 110.110.110.151

Constraints specific to QNAP NAS

The specific QNAP NAS constraint of the TS-x31P3 Series is as follows:

  • This NAS has an ARM v7 processor but the QNAP Container Station application has been modified to implement a page size of 32k instead of the default

The majority of existing Docker images don’t work, including Nextcloud AIO (which is Nextcloud’s recommended Docker image).

The error message displayed is : /bin/sh: error while loading shared libraries: libc.so.6: ELF load command address/offset not page-aligned

QNAP NAS specification

  • OS : QTS 5.X
  • CPU : Quad-Core ARM Cortex-A15
  • Memory : 4GB
  • IP Static : 110.110.110.110

Process

We’ll be using the following components:

  • QNAP Container Station application (Docker and Docker Compose)
  • Nextcloud v27 application (archive)
  • A static IP for Nextcloud access (110.110.110.151)

The steps are as follows:

  1. Create a specific user for Docker
  2. Create a shared directory for Docker
  3. Enable SSH access on QNAP NAS
  4. Install QNAP Container Station application
  5. SSH connection to QNAP NAS
  6. Create a Dockerfile
  7. Creating a Docker Compose file
  8. Create Nextcloud application configuration files
  9. Creation of an initialization script for the Nextcloud application
  10. Creation of a configuration file for all environment variables
  11. Set up a startup script on the QNAP NAS

Details

Create a specific user for Docker

Create a specific user for Docker to limit rights on all QNAP NAS components and services:

  1. Go to Main Menu > ControlPanel > Privilege > Users
  2. Click on the Create button, then on the Create a User option
  3. Fill in the form and choose the name (username) dockeruser
  4. Click on the Create button

Note: Save the PUID (e.g. 501) and PGID (e.g. 100), as these will be required for the rights of the Docker container containing the Nextcloud application.

20231107_Blog_use_nextcloud_on_qnap_01

Create a shared directory for Docker

To create a specific shared directory for the data and configuration files of the Docker container containing the Nextcloud application, follow these steps:

  1. Go to Main Menu > ControlPanel > Privilege > Shared Folders
  2. Click on the Create button, then on the Create A Shared Folder option
  3. Fill in the details and click on the Next button
    1. Folder Name : Folder name
    2. Comment : Comment for the directory
    3. Disk Volume : Disk on which the folder will be created
    4. Path: Directory path
  4. In the Configure access privileges for users window, select the RW (read & write) option for your administration user and for the dockeruser user, and click on the Next button
  5. In the Properties window, select the desired options and click on the Finish button

20231107_Blog_use_nextcloud_on_qnap_02

Enable SSH access on QNAP NAS

To be able to connect to the QNAP NAS via SSH, you must first activate the service by following the steps below:

  1. Go to Main Menu > ControlPanel > Network & File Services > Telnet / SSH
  2. Check the Allow SSH connection option and enter the desired port number
  3. Click on the Apply button

Note: In our case, we’ll use port 23422 for SSH access.

20231107_Blog_use_nextcloud_on_qnap_03

Install QNAP Container Station application

To use Docker on the QNAP NAS, you need to install the QNAP Container Station application, following these steps:

  1. Go to Main Menu > App Center > QNAP Store > All Apps
  2. Find the Container Station (Utilities) application and click on the + Install option

Note: A shortcut named Container Station should be present on the home page of your QNAP NAS.

20231107_Blog_use_nextcloud_on_qnap_04

SSH connection to QNAP NAS

To connect to the QNAP NAS via SSH, simply use the following command: ssh <useradmin>@<ip NAS QNAP> -p <port> In our case, the command would be: ssh admin@110.110.110.110 -p 23422

Let’s take this opportunity to create the first directories needed for Docker and the Docker container containing the Nextcloud application

  1. Go to the Docker shared directory: cd /share/Docker
  2. Create the nextcloud directory, which will store all data associated with the Nextcloud application: mkdir /share/Docker/nextcloud
  3. Create the scripts directory, which will store all the scripts and configuration files needed to build the Docker image and container containing the Nextcloud application: mkdir /share/Docker/scripts

Create a Dockerfile

To set up the Nextcloud application on the QNAP NAS, we’re going to create the required elements.

Creating the directories needed for the Docker image

We’re going to create an nextcloud_app directory in which we’ll put all the elements needed to build the Docker image containing the Nextcloud application :

  1. Create an nextcloud_app directory: mkdir /share/Docker/scripts/nextcloud_app
  2. Create a config subdirectory to store all the configuration files required for the Nextcloud application: mkdir /share/Docker/scripts/nextcloud_app/config

Create a Dockerfile

Create a file named Dockerfile in the directory /share/Docker/scripts/nextcloud_app.

The contents of the file Dockerfile :

 1# Based on ubuntu image
 2FROM ubuntu:22.04
 3
 4# Default value
 5ARG build_TZ=Europe/Paris
 6ARG build_PUID=501
 7ARG build_PGID=100
 8
 9# Config environment
10ENV TZ=$build_TZ
11ENV DEBIAN_FRONTEND noninteractive
12ENV PUID=$build_PUID
13ENV PGID=$build_PGID
14
15# Work directory
16WORKDIR /
17
18# Update Ubuntu system
19RUN apt-get update -y && apt-get upgrade -y
20
21# Install tools (apache2, mariadb and php)
22RUN apt-get install sudo vim wget cron curl ffmpeg apache2 mariadb-server libapache2-mod-php \
23php-gd php-mysql php-curl php-mbstring php-intl php-gmp php-bcmath php-xml php-imagick php-zip \
24php-bz2 php-ldap php-smbclient php-imap php-apcu -y
25
26# Clean APT cache
27RUN apt-get clean
28RUN rm -rf /var/lib/apt/lists/*
29
30# Install Nextcloud in the folder /var/www
31RUN wget https://download.nextcloud.com/server/releases/latest-27.tar.bz2 -O /tmp/nextcloud-27.tar.bz2
32RUN tar -xjvf /tmp/nextcloud-27.tar.bz2 -C /tmp
33RUN cp -r /tmp/nextcloud /var/www
34RUN chown -R www-data:www-data /var/www/nextcloud
35
36# Copy the template nextcloud config files for run_nextcloud.sh later use
37RUN cp -r /tmp/nextcloud/config /tmp/nextcloud_config
38
39# Clean nextcloud temporary folder
40RUN rm -rf /tmp/nextcloud
41RUN rm /tmp/nextcloud-27.*
42
43# Manage SSL certificate
44RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt \
45-subj /C=FR/ST=France/L=Paris/O=NAS/OU=Nextcloud/CN=nas@nas.nas
46
47# Create specific folder
48RUN mkdir /scripts
49
50# Copy config files
51COPY ./config /config
52# Prepare execution of the run_nextcloud.sh script
53RUN chmod +x /config/run_nextcloud.sh
54
55# Configuration Crontab
56RUN echo exit 0 > /usr/sbin/policy-rc.d
57RUN echo "*/5  *  *  *  * php -f /var/www/nextcloud/cron.php" | crontab -u www-data -
58
59# Expose ports for Nextcloud application
60EXPOSE 80
61EXPOSE 443
62EXPOSE 3478
63
64# Define Entrypoint to execute the run_nextcloud.sh script
65ENTRYPOINT ["bash","/config/run_nextcloud.sh",">","/var/log/nextcloud/run_nextcloud.log"]

Creating a Docker Compose file

To make it easier to use Docker to build the Nextcloud application, we’re going to use Docker Compose to create a file named docker-compose.yml in the /share/Docker/scripts/nextcloud_app directory.

This file will enable us to define the build of the Docker image and the Docker network to be used by the Docker container containing the Nextcloud application.

The contents of the file docker-compose.yml :

 1version: "3.7"
 2
 3services:
 4  # Nextcloud application
 5  nextcloud:
 6    image: nextcloud-qnas-img:v27
 7    # Build configuration
 8    build:
 9      context: .
10      dockerfile: Dockerfile
11      # Default argument for the build
12      args:
13        build_PUID : 501
14        build_PGID : 100
15        build_TZ : Europe/Paris
16    # Name of the application for QNAP Container Station
17    container_name: nextcloud-qnas
18    # Define the Static IP from QNET Network
19    networks:
20      qnet-static-eth0:
21        ipv4_address: 110.110.110.151
22    # Define the Env file to initialise environment variable for the container
23    env_file:
24      - ./config/.env
25    # Define all the volume to store the data and logs into the shared folder (and not into the container)
26    volumes:
27      - /share/Docker/nextcloud/nc_data:/var/www/nextcloud/data
28      - /share/Docker/nextcloud/nc_apps:/var/www/nextcloud/apps
29      - /share/Docker/nextcloud/db_data:/var/lib/mysql
30      - /share/Docker/nextcloud/nc_log:/var/log/nextcloud
31      - /share/Docker/nextcloud/db_log:/var/log/mysql
32    # Define the exposed port
33    ports:
34      - 80:80
35      - 443:443
36      - 3478:3478
37    # Don't restart the container only if we manually stop it
38    restart: unless-stopped
39
40# Define the network based en NAS QNAP interfaces
41networks:
42  qnet-static-eth0 :
43    driver: qnet
44    driver_opts:
45      iface: "eth0"
46    ipam:
47      driver: qnet
48      options:
49        iface: "eth0"
50      config:
51        - subnet: 110.110.110.0/24
52          gateway: 110.110.110.1

Note on volumes:

  • The /share/Docker/nextcloud/nc_data directory will contain all Nextcloud application user data
  • The /share/Docker/nextcloud/nc_apps directory will contain all the Nextcloud application’s internal applications
  • The /share/Docker/nextcloud/db_data directory will contain all the MariaDB database data used by the Nextcloud application
  • The /share/Docker/nextcloud/nc_log directory will contain all Nextcloud application logs
  • The /share/Docker/nextcloud/db_log directory will contain all logs from the MariaDB database used by the Nextcloud application

Note: The objective is to keep the data from the mariaDB database and the Nextcloud application in the shared directory, to make backups easier and to be able to dissociate the data from the container.

Create Nextcloud application configuration files

In order to configure the services required by the Nextcloud application when running the Docker container, we’ll prepare the following configuration files:

I. For the Apache2 service :

  1. Create the configuration directory mkdir /share/Docker/scripts/nextcloud_app/config/apache2
  2. Create the configuration file nextcloud.conf in the created directory

The contents of the file nextcloud.conf :

 1<VirtualHost *:80>
 2    ServerName ${NC_HOSTNAME}
 3    Redirect permanent / https://${NC_HOSTNAME}/
 4</VirtualHost>
 5
 6<VirtualHost *:443>
 7     ServerName ${NC_HOSTNAME}
 8     DocumentRoot ${ROOT_NC}/
 9
10     Alias / "${ROOT_NC}/"
11
12     SSLEngine on
13     SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
14     SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
15
16     <Directory ${ROOT_NC}/>
17       Options +FollowSymlinks
18        AllowOverride All
19        Require all granted
20          <IfModule mod_dav.c>
21            Dav off
22          </IfModule>
23        SetEnv HOME ${ROOT_NC}
24        SetEnv HTTP_HOME ${ROOT_NC}
25        SetEnv HTTPS_HOME ${ROOT_NC}
26     </Directory>
27
28     ErrorLog ${APACHE_LOG_DIR}/error.log
29     CustomLog ${APACHE_LOG_DIR}/access.log combined
30
31</VirtualHost>

III. For the MariaDB service :

  1. Create the configuration directory mkdir /share/Docker/scripts/nextcloud_app/config/mariadb
  2. Create the 50-server.conf configuration file in the directory you’ve created, using the desired template and modifying the line starting with datadir with the following line datadir = ${ROOT_DB_DATA}

III. For the Nextcloud application :

  1. Create the mkdir /share/Docker/scripts/nextcloud_app/config/nextcloud configuration directory.
  2. Create configuration files named autoconfig.php and docker.config.php in the created directory to automatically configure the Nextcloud application on the first run

The contents of the file autoconfig.php :

 1<?php
 2$AUTOCONFIG = array(
 3  "dbtype"        => "mysql",
 4  "dbname"        => "${MYSQL_DATABASE}",
 5  "dbuser"        => "${MYSQL_USER}",
 6  "dbpass"        => "${MYSQL_PASSWORD}",
 7  "dbhost"        => "localhost",
 8  "dbtableprefix" => "oc_",
 9  "adminlogin"    => "admin",
10  "adminpass"     => "${NC_ADMIN_PASSWORD}",
11  "directory"     => "${ROOT_NC_DATA}",
12  "trusted_domains" => 
13  array (
14    0 => "${STATIC_IP}",
15    1 => "${NC_HOSTNAME}",
16  ),
17);

The contents of the file docker.config.php :

 1<?php
 2$CONFIG = array (
 3  'overwriteprotocol' => 'https',
 4  'overwritewebroot' => '/',
 5  'overwrite.cli.url' => 'https://${NC_HOSTNAME}',
 6  'htaccess.RewriteBase' => '/',
 7  'mysql.utf8mb4' => true,
 8  'default_language' => 'fr',
 9  'force_language' => 'fr',
10  'default_locale' => 'fr_FR',
11  'default_phone_region' => 'FR',
12  'force_locale' => 'fr_FR',
13  'knowledgebaseenabled' => true,
14  'allow_user_to_change_display_name' => true,
15  'auth.bruteforce.protection.enabled' => true,
16  'auth.bruteforce.protection.testing' => false,
17  'ratelimit.protection.enabled' => true,
18  'auth.webauthn.enabled' => true,
19  'auth.storeCryptedPassword' => true,
20  'lost_password_link' => 'disabled',
21  'mail_domain' => '${MAIL_DOMAINE}',
22  'updatechecker' => true,
23  'updater.server.url' => 'https://updates.nextcloud.com/updater_server/',
24  'updater.release.channel' => 'stable',
25  'has_internet_connection' => true,
26  'connectivity_check_domains' => [
27    'www.nextcloud.com',
28    'www.startpage.com',
29    'www.eff.org',
30    'www.edri.org'
31  ],
32  'log_type' => 'file',
33  'log_type_audit' => 'file',
34  'logfile' => '${ROOT_NC_LOG}/nextcloud.log',
35  'logfile_audit' => '${ROOT_NC_LOG}/audit.log',
36  'logfilemode' => 0640,
37  'loglevel' => 2,
38  'loglevel_frontend' => 2,
39  'syslog_tag' => 'Nextcloud',
40  'syslog_tag_audit' => 'Nextcloud',
41  'logdateformat' => 'F d, Y H:i:s',
42  'logtimezone' => '${TZ}',
43  'customclient_desktop' =>
44	'https://nextcloud.com/install/#install-clients',
45'customclient_android' =>
46	'https://play.google.com/store/apps/details?id=com.nextcloud.client',
47'customclient_ios' =>
48	'https://itunes.apple.com/us/app/nextcloud/id1125420102?mt=8',
49'customclient_ios_appid' =>
50		'1125420102',
51  'defaultapp' => 'dashboard,files',
52  'appstoreenabled' => true,
53  'apps_paths' => [
54    [
55      'path'=> OC::$SERVERROOT . '/apps',
56      'url' => '/apps',
57      'writable' => true,
58    ],
59  ],
60  'enable_previews' => false,
61  'memcache.local' => '\OC\Memcache\APCu',
62);

IV. For the PHP service :

  1. Create the configuration directory mkdir /share/Docker/scripts/nextcloud_app/config/php
  2. Create a configuration file named 20-pdo_mysql.ini in the created directory

The contents of the file 20-pdo_mysql.ini:

 1; configuration for php mysql module
 2; priority=20
 3extension=pdo_mysql.so
 4
 5[mysql]
 6mysql.allow_local_infile=On
 7mysql.allow_persistent=On
 8mysql.cache_size=2000
 9mysql.max_persistent=-1
10mysql.max_links=-1
11mysql.default_port=3306
12mysql.default_socket=/var/lib/mysql/mysql.sock
13mysql.default_host=localhost
14mysql.connect_timeout=60
15mysql.trace_mode=Off

Creation of an initialization script for the Nextcloud application

In order to initialize the Nextcloud application when the container is executed, we’ll set up the run_nextcloud.sh script in the /share/Docker/scripts/nextcloud_app/config directory.

The contents of the file run_nextcloud.sh :

  1#!/bin/sh
  2
  3# ENV
  4ROOT_CONF="/config"
  5
  6# Check one environment variable (if .env is ok)
  7if [ -z "${ROOT_DB_DATA}" ]; then
  8    echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - Environment variable issues"
  9    exit 1
 10fi
 11
 12
 13# Check data folder to do the initialisation only if one or more folders are emppty
 14CHECK_FOLDER_MARIADB=$(ls "${ROOT_DB_DATA}" | wc -l)
 15CHECK_FOLDER_NC=$(ls "${ROOT_NC_DATA}" | wc -l)
 16CHECK_FOLDER_NC_CONF=$(ls "${ROOT_NC_CONF}" | wc -l)
 17if [ $CHECK_FOLDER_MARIADB -eq 0 ] || [ $CHECK_FOLDER_NC -eq 0 ] || [ $CHECK_FOLDER_NC_CONF -eq 0 ]; then
 18
 19    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - Init Nextcloud"
 20
 21    # Delete old data
 22    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - Delete existing Data"
 23    rm -rf $ROOT_DB_DATA/*
 24    rm -rf $ROOT_NC_DATA/*
 25    rm -rf $ROOT_NC_CONF/*
 26
 27    # Copy config files
 28    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - Copy config files"
 29    # Mariadb
 30    cat $ROOT_CONF/mariadb/50-server.cnf | sed 's|${ROOT_DB_DATA}|'"${ROOT_DB_DATA}"'|g' > /etc/mysql/mariadb.conf.d/50-server.cnf
 31    # Apache2 and PHP
 32    echo "apc.enable_cli = 1" >> /etc/php/8.1/apache2/php.ini
 33    cat $ROOT_CONF/php/20-pdo_mysql.ini > /etc/php/8.1/apache2/conf.d/20-pdo_mysql.ini
 34    cat $ROOT_CONF/apache2/nextcloud.conf | sed 's|${NC_HOSTNAME}|'"${NC_HOSTNAME}"'|g' \
 35        | sed 's|${ROOT_NC}|'"${ROOT_NC}"'|g' \
 36        > /etc/apache2/sites-available/nextcloud.conf
 37    # Nextcloud
 38    cp /tmp/nextcloud_config/* $ROOT_NC_CONF/.
 39    cat $ROOT_CONF/nextcloud/autoconfig.php | sed 's|${NC_HOSTNAME}|'"${NC_HOSTNAME}"'|g' \
 40        | sed 's|${ROOT_NC_DATA}|'"${ROOT_NC_DATA}"'|g' \
 41        | sed 's|${MYSQL_DATABASE}|'"${MYSQL_DATABASE}"'|g' \
 42        | sed 's|${MYSQL_USER}|'"${MYSQL_USER}"'|g' \
 43        | sed 's|${MYSQL_PASSWORD}|'"${MYSQL_PASSWORD}"'|g' \
 44        | sed 's|${STATIC_IP}|'"${STATIC_IP}"'|g' \
 45        | sed 's|${NC_ADMIN_PASSWORD}|'"${NC_ADMIN_PASSWORD}"'|g' \
 46        > ${ROOT_NC_CONF}/autoconfig.php
 47    cat $ROOT_CONF/nextcloud/docker.config.php | sed 's|${NC_HOSTNAME}|'"${NC_HOSTNAME}"'|g' \
 48        | sed 's|${MAIL_DOMAINE}|'"${MAIL_DOMAINE}"'|g' \
 49        | sed 's|${ROOT_LOG}|'"${ROOT_LOG}"'|g' \
 50        | sed 's|${TZ}|'"${TZ}"'|g' \
 51        | sed 's|${ROOT_NC_LOG}|'"${ROOT_NC_LOG}"'|g' \
 52        | sed 's|${ROOT_NC_APPS}|'"${ROOT_NC_APPS}"'|g' \
 53        > ${ROOT_NC_CONF}/docker.config.php
 54
 55    # Manage right on config files
 56    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - Manage right on config files"
 57    chmod 644 /etc/mysql/mariadb.conf.d/50-server.cnf
 58    chmod 644 /etc/php/8.1/apache2/conf.d/20-pdo_mysql.ini
 59    chmod 644 /etc/php/8.1/apache2/php.ini
 60    chmod 644 /etc/apache2/sites-available/nextcloud.conf
 61    chmod 644 ${ROOT_NC_CONF}/*
 62
 63    # Init MariaDB service
 64    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - MariasDB : Install DB (mysql)"
 65    mariadb-install-db --user=mysql
 66    if [ $? -ne 0 ]; then echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - MariasDB Install"; exit 1 ; fi
 67
 68    # Start MariaDB service
 69    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - MariasDB : Start service"
 70    service mariadb start
 71    if [ $? -ne 0 ]; then echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - MariasDB Start"; exit 1 ; fi
 72
 73    # Create nextcloud user into MariaDB
 74    QRY_SQL="CREATE USER IF NOT EXISTS '${MYSQL_USER}'@'localhost' IDENTIFIED BY '${MYSQL_PASSWORD}';
 75    CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
 76    GRANT ALL PRIVILEGES ON ${MYSQL_DATABASE}.* TO '${MYSQL_USER}'@'localhost';
 77    FLUSH PRIVILEGES;"
 78
 79    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - MariasDB : Create user et database"
 80    mariadb -u root -e "${QRY_SQL}"
 81    if [ $? -ne 0 ]; then echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - MariasDB exec QRY"; exit 1 ; fi
 82
 83
 84    # Init Apache2 service
 85    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - Apache2 : Init conf"
 86    echo "ServerName ${STATIC_IP}" >> /etc/apache2/apache2.conf
 87    a2ensite nextcloud.conf
 88    a2enmod rewrite headers env dir mime ssl
 89
 90    # Start Apache2 service
 91    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - Apache2 : Start service"
 92    service apache2 start
 93    if [ $? -ne 0 ]; then echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - apache2 service start"; exit 1 ; fi
 94
 95    # Manage folders rights
 96    echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - Manage folders chown"
 97    chown -R www-data:www-data ${ROOT_NC}
 98    chown -R www-data:www-data ${ROOT_NC_DATA}
 99    chown -R www-data:www-data ${ROOT_NC_CONF}
100    chown -R www-data:www-data ${ROOT_NC_APPS}
101    chown -R mysql:mysql ${ROOT_DB_DATA}
102    chmod -R 766 ${ROOT_NC_LOG}
103    chmod -R 766 ${ROOT_DB_LOG}
104    chown -R www-data:www-data ${ROOT_NC_LOG}
105    chown -R www-data:www-data ${ROOT_DB_LOG}
106    
107
108    # Execute Nextcloud for the first time with HTTPS (initialize admin user and data folder)
109    openssl s_client -showcerts -connect ${NC_HOSTNAME}:443 </dev/null | sed -n -e '/-.BEGIN/,/-.END/ p' > /tmp/nextcloud.pem
110    curl --cacert /tmp/nextcloud.pem https://${NC_HOSTNAME}
111    rm /tmp/nextcloud.pem
112
113    # Start Cron service
114    service cron start
115    if [ $? -ne 0 ]; then echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - cron service start"; exit 1 ; fi
116
117else
118    # If init already done : Start services
119    service mariadb start
120    if [ $? -ne 0 ]; then echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - mariadb service start"; exit 1 ; fi
121    service apache2 start
122    if [ $? -ne 0 ]; then echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - apache2 service start"; exit 1 ; fi
123    service cron start
124    if [ $? -ne 0 ]; then echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - cron service start"; exit 1 ; fi
125
126fi
127
128# Infinite loop to keep service up
129while [ 1 ]; do 
130    sleep 300
131
132    # Check Apache2 service
133    CHECK_APACHE=$(service apache2 status | grep 'is running' | wc -l)
134    if [ "$CHECK_APACHE" != "1" ]; then
135        echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - Restart apache2 service"
136        service apache2 start
137        CHECK_APACHE_2=$(service apache2 status | grep 'is running' | wc -l)
138        if [ "$CHECK_APACHE_2" != "1" ]; then
139            echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - Service apache2 KO"
140            exit 1
141        fi
142    fi
143
144    # Check MariaDB service
145    CHECK_MARIADB=$(service mariadb status | grep 'Uptime' | wc -l)
146    if [ "$CHECK_MARIADB" != "1" ]; then
147        echo "$(date +"%Y-%m-%d %H:%M:%S") - Info - Restart mariadb service"
148        service mariadb start
149        CHECK_MARIADB_2=$(service mariadb status | grep 'Uptime' | wc -l)
150        if [ "$CHECK_MARIADB_2" != "1" ]; then
151            echo "$(date +"%Y-%m-%d %H:%M:%S") - Error - Service mariadb KO"
152            exit 1
153        fi
154    fi
155
156done
157
158exit 0

Creation of a configuration file for all environment variables

In order to finalize the creation of the Docker image, we need to set up an .env file to centralize all the environment variables required by the container and, more specifically, by the run_nextcloud.sh initialization script.

This environment file is defined in the docker-compose.yml file. The .env file must be created in the /share/Docker/scripts/nextcloud_app/config directory.

The contents of the file .env : (replacing the values required for your environment))

 1PUID=501
 2PGID=100
 3ROOT_DB_DATA=/var/lib/mysql
 4ROOT_NC=/var/www/nextcloud
 5ROOT_NC_DATA=/var/www/nextcloud/data
 6ROOT_NC_CONF=/var/www/nextcloud/config
 7ROOT_NC_LOG=/var/log/nextcloud
 8ROOT_DB_LOG=/var/log/mysql
 9ROOT_NC_APPS=/var/www/nextcloud/apps
10NC_HOSTNAME=<hostname (ex: nextcloud.nas.nas)>
11MYSQL_DATABASE=nextcloud
12MYSQL_USER=<nextcloud username for mariadb>
13MYSQL_PASSWORD=<nextcloud user password for mariadb>
14STATIC_IP=110.110.110.151
15MAIL_DOMAINE=nas@nas.nas
16TZ=Europe/Paris
17NC_ADMIN_PASSWORD=<nextcloud admin password>

Set up a startup script on the QNAP NAS

In order to start the Docker container of the Nextcloud application at the QNAP NAS startup :

  1. Go to Main Menu > ControlPanel > System > Hardware
  2. In the General tab, check the Run user defined processes during startup option
  3. Connect to the QNAS NAS via SSH with an administrator account: ssh <user>@<ip> -p <port>
  4. Run the follow commands to mount the directory /tmp/config
1ubiattach -m 6 -d 2
2/bin/mount -t ubifs ubi2:config /tmp/config
  1. Create the file /tmp/config/autorun.sh with the content as follow :
1#!/bin/sh
2cd /share/Docker/scripts/nextcloud_app
3docker compose up -d
  1. Run the following commands to be able to run the file /tmp/config/autorun.sh and unmount he directory /tmp/config
1chmod +x /tmp/config/autorun.sh
2umount /tmp/config
3ubidetach -m 6

Run the Nextcloud application

The steps for manually running the Nextcloud application are as follows:

  1. Connect SSH to the QNAP NAS with an administrator account: ssh <user>@<ip> -p <port>
  2. Go to the directory containing the Docker image elements: cd /share/Docker/scripts/nextcloud_app
  3. Run the following Docker Compose command: docker compose up -d
  4. Wait a few seconds/minutes and check that the container is running correctly by connecting to the Nextcloud application https://110.110.110.151

You can check that the container is running correctly by checking the information in the QNAP Container Station application, or by using the usual docker commands (stats, logs, ps, …).

20231107_Blog_use_nextcloud_on_qnap_05

20231107_Blog_use_nextcloud_on_qnap_06

20231107_Blog_use_nextcloud_on_qnap_07

Docker commands

Some commands for using Docker :

  • docker build -t nextcloud-qnas-img:v27 . : Builds the image from the Dockerfile in the current directory.
    • docker build -t nextcloud-qnas-img:v27 . --build-arg build_TZ=Europe/Paris --build-arg build_PUID=501 --build-arg build_PGID=100 : Build command with argument
  • docker ps : List of running containers
  • docker ps -a : List of all existing containers
  • docker stats : Statistics on running container usage
  • docker logs nextcloud-qnas : Container log message (standard output)
  • docker create -i -t -v /share/Docker/nextcloud/...:/... -v ... --name nextcloud-qnas nextcloud-qnas-img:v27 : Create an nextcloud-qnas container from the nextcloud-qnas-img:v27 image
  • docker container start -a -i nextcloud-qnas : Start a container by accessing the console directly
  • docker rm nextcloud-qnas : Delete the container

If you need to manually recreate the QNAP NAS network for Docker :

1# Create dhcp network
2docker network create -d qnet --opt=iface=eth0 --ipam-driver=qnet --ipam-opt=iface=eth0 qnet-dhcp-eth0
3
4# Create static network
5docker network create -d qnet --opt=iface=eth0 --ipam-driver=qnet --ipam-opt=iface=eth0 \
6      --subnet=110.110.110.0/24 --gateway=110.110.110.1 qnet-static-eth0

Some commands for using Docker Compose : (You need to be in the directory containing the docker-compose.yaml file)

  • docker compose build --build-arg TZ=Europe/Paris --build-arg PUID=501 --build-arg PGID=100 : Allows build to be performed in the same way as the Docker command
  • docker compose up -d : Create and start defined containers
  • docker compose down : Stop and delete defined containers
  • docker compose start : Start defined containers
  • docker compose stop : Stop defined containers