Setup Nextcloud with Object Storage and Remote Database
Setup Nextcloud with Object Storage and Remote Database

We are starting out with a per installed server with docker.

First thing that we need to do is update the server to we are using Ubuntu 20.04

sudo apt update && sudo apt upgrade -y

With the server updated it is time to get to the project the first part of the project that we are going to be working on is the "control file" this is a .ymal file that will tell docker and docker compose for you would like to set up your apps that you will be using for your project.

sudo nano docker-compose.yml

version: '3.8'

services:
  db:
    image: mariadb:latest
    container_name: nextcloud_db
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    volumes:
      - secure_data:/var/lib/mysql
    networks:
      - nextcloud_network

  minio:
    image: minio/minio:latest
    container_name: minio
    restart: unless-stopped
    environment:
      - MINIO_ACCESS_KEY=${MINIO_ACCESS_KEY}
      - MINIO_SECRET_KEY=${MINIO_SECRET_KEY}
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_data:/data
    command: server /data --console-address ":9001"

  app:
    image: nextcloud:latest
    container_name: nextcloud_app
    ports:
      - 8080:80
    restart: always
    environment:
      - MYSQL_HOST=db
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - OBJECTSTORE_CLASS=OC\\Files\\ObjectStore\\S3
      - OBJECTSTORE_BUCKET=your-bucket-name
      - OBJECTSTORE_KEY=${MINIO_ACCESS_KEY}
      - OBJECTSTORE_SECRET=${MINIO_SECRET_KEY}
      - OBJECTSTORE_HOSTNAME=192.168.0.7
      - OBJECTSTORE_PORT=9000
      - OBJECTSTORE_SSL=false
      - OBJECTSTORE_REGION=us-east-1
      - OBJECTSTORE_USE_PATH_STYLE=true
    volumes:
      - nextcloud_data:/var/www/html
      - ./init_nextcloud.sh:/init_nextcloud.sh:ro
    entrypoint: ["/bin/bash", "/init_nextcloud.sh"]
    networks:
      - nextcloud_network
    depends_on:
      - db
      - minio

volumes:
  secure_data:
    driver: local
    driver_opts:
      type: none
      device: /var/lib/docker-volumes/mariadb_secure_data
      o: bind

  nextcloud_data:
    driver: local
    driver_opts:
      type: none
      device: /var/lib/docker-volumes/nextcloud_data
      o: bind

  minio_data:
    driver: local
    driver_opts:
      type: none
      device: /var/lib/docker-volumes/minio_data
      o: bind

networks:
  nextcloud_network:
version: '3.8'

services:
  db:
    image: mariadb:latest
    container_name: nextcloud_db
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    volumes:
      - secure_data:/var/lib/mysql
    networks:
      - nextcloud_network

  minio:
    image: minio/minio:latest
    container_name: minio
    restart: unless-stopped
    environment:
      - MINIO_ACCESS_KEY=${MINIO_ACCESS_KEY}
      - MINIO_SECRET_KEY=${MINIO_SECRET_KEY}
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_data:/data
    command: server /data --console-address ":9001"

  app:
    image: nextcloud:latest
    container_name: nextcloud_app
    ports:
      - 8080:80
    restart: always
    environment:
      - MYSQL_HOST=db
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - OBJECTSTORE_CLASS=OC\\Files\\ObjectStore\\S3
      - OBJECTSTORE_BUCKET=your-bucket-name
      - OBJECTSTORE_KEY=${MINIO_ACCESS_KEY}
      - OBJECTSTORE_SECRET=${MINIO_SECRET_KEY}
      - OBJECTSTORE_HOSTNAME=192.168.0.7
      - OBJECTSTORE_PORT=9000
      - OBJECTSTORE_SSL=false
      - OBJECTSTORE_REGION=us-east-1
      - OBJECTSTORE_USE_PATH_STYLE=true
    volumes:
      - nextcloud_data:/var/www/html
      - ./init_nextcloud.sh:/init_nextcloud.sh:ro
    entrypoint: ["/bin/bash", "/init_nextcloud.sh"]
    networks:
      - nextcloud_network
    depends_on:
      - db
      - minio

volumes:
  secure_data:
    driver: local
    driver_opts:
      type: none
      device: /var/lib/docker-volumes/mariadb_secure_data
      o: bind

  nextcloud_data:
    driver: local
    driver_opts:
      type: none
      device: /var/lib/docker-volumes/nextcloud_data
      o: bind

  minio_data:
    driver: local
    driver_opts:
      type: none
      device: /var/lib/docker-volumes/minio_data
      o: bind

networks:
  nextcloud_network:

sudo nano .env

MYSQL_ROOT_PASSWORD=your_root_password
MYSQL_DATABASE=your_database
MYSQL_USER=your_user
MYSQL_PASSWORD=your_password
MINIO_ACCESS_KEY=your-access-key
MINIO_SECRET_KEY=your-secret-key
MYSQL_ROOT_PASSWORD=your_root_password
MYSQL_DATABASE=your_database
MYSQL_USER=your_user
MYSQL_PASSWORD=your_password
MINIO_ACCESS_KEY=your-access-key
MINIO_SECRET_KEY=your-secret-key

sudo nano init_nextcloud.sh

this only need to load one time

#!/bin/bash

# Function to wait for Nextcloud installation
wait_for_nextcloud() {
  echo "Waiting for Nextcloud to initialize..."
  while ! [ -f /var/www/html/config/config.php ]; do
    sleep 5
  done
}

# Function to check if Nextcloud is already installed
is_nextcloud_installed() {
  grep -q "'installed' => true," /var/www/html/config/config.php
}

# Function to check if MinIO configuration already exists
is_minio_configured() {
  grep -q "'objectstore' => array(" /var/www/html/config/config.php
}

# Wait for Nextcloud to initialize
wait_for_nextcloud

# Check if Nextcloud is installed
if is_nextcloud_installed; then
  echo "Nextcloud is already installed."

  # Check if MinIO configuration exists
  if is_minio_configured; then
    echo "MinIO configuration already exists in config.php. Skipping configuration."
  else
    echo "Applying MinIO configuration to config.php..."

    # Backup the original config.php
    cp /var/www/html/config/config.php /var/www/html/config/config.php.bak

    # Add MinIO configuration to config.php
    sed -i "/);/i \
    'objectstore' => array(\
      'class' => 'OC\\\\Files\\\\ObjectStore\\\\S3',\
      'arguments' => array(\
        'bucket' => '${OBJECTSTORE_BUCKET}',\
        'key' => '${OBJECTSTORE_KEY}',\
        'secret' => '${OBJECTSTORE_SECRET}',\
        'hostname' => '${OBJECTSTORE_HOSTNAME}',\
        'port' => ${OBJECTSTORE_PORT},\
        'use_ssl' => ${OBJECTSTORE_SSL},\
        'region' => '${OBJECTSTORE_REGION}',\
        'use_path_style' => ${OBJECTSTORE_USE_PATH_STYLE},\
      ),\
    )," /var/www/html/config/config.php

    # Set permissions
    chown www-data:www-data /var/www/html/config/config.php
    echo "MinIO configuration added successfully."
  fi
else
  echo "Nextcloud is not installed yet. Skipping MinIO configuration."
fi

# Start Apache (or PHP-FPM for some images)
exec apache2-foreground
#!/bin/bash

# Function to wait for Nextcloud installation
wait_for_nextcloud() {
  echo "Waiting for Nextcloud to initialize..."
  while ! [ -f /var/www/html/config/config.php ]; do
    sleep 5
  done
}

# Function to check if Nextcloud is already installed
is_nextcloud_installed() {
  grep -q "'installed' => true," /var/www/html/config/config.php
}

# Function to check if MinIO configuration already exists
is_minio_configured() {
  grep -q "'objectstore' => array(" /var/www/html/config/config.php
}

# Wait for Nextcloud to initialize
wait_for_nextcloud

# Check if Nextcloud is installed
if is_nextcloud_installed; then
  echo "Nextcloud is already installed."

  # Check if MinIO configuration exists
  if is_minio_configured; then
    echo "MinIO configuration already exists in config.php. Skipping configuration."
  else
    echo "Applying MinIO configuration to config.php..."

    # Backup the original config.php
    cp /var/www/html/config/config.php /var/www/html/config/config.php.bak

    # Add MinIO configuration to config.php
    sed -i "/);/i \
    'objectstore' => array(\
      'class' => 'OC\\\\Files\\\\ObjectStore\\\\S3',\
      'arguments' => array(\
        'bucket' => '${OBJECTSTORE_BUCKET}',\
        'key' => '${OBJECTSTORE_KEY}',\
        'secret' => '${OBJECTSTORE_SECRET}',\
        'hostname' => '${OBJECTSTORE_HOSTNAME}',\
        'port' => ${OBJECTSTORE_PORT},\
        'use_ssl' => ${OBJECTSTORE_SSL},\
        'region' => '${OBJECTSTORE_REGION}',\
        'use_path_style' => ${OBJECTSTORE_USE_PATH_STYLE},\
      ),\
    )," /var/www/html/config/config.php

    # Set permissions
    chown www-data:www-data /var/www/html/config/config.php
    echo "MinIO configuration added successfully."
  fi
else
  echo "Nextcloud is not installed yet. Skipping MinIO configuration."
fi

# Start Apache (or PHP-FPM for some images)
exec apache2-foreground

sudo chmod +x init_nextcloud.sh

use the chmod command to restrict permissions for the file to that of the user that has made the file

sudo chmod 600 .env

Before we start up the project we are gooing to need to make some more folders so that we can set the permissions for them

sudo mkdir -p /var/lib/docker-volumes/mariadb_secure_data

sudo mkdir -p /var/lib/docker-volumes/nextcloud_data

sudo mkdir -p /var/lib/docker-volumes/minio_data

Next we will set the permissions of the to folders that we have made with chmod but this time we are going to set them to 700 which means that the root account is the only one that can interact with the folders

sudo chmod 700 /var/lib/docker-volumes/mariadb_secure_data

sudo chmod -R 700 /var/lib/docker-volumes/nextcloud_data

sudo chmod -R 700 /var/lib/docker-volumes/minio_data

last we are going to use chown to set ownership of a folder where nextcloud will store your data.

sudo chown -R www-data:www-data /var/lib/docker-volumes/nextcloud_data

With all of this done it is time to start this project up

sudo docker compose up