Deploy a Secure HTTPS Nextcloud Docker Server

Author

Reads 948

Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.
Credit: pexels.com, Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.

Deploying a secure HTTPS Nextcloud Docker server is a crucial step in setting up a reliable and private cloud storage solution.

To begin, you'll need to pull the official Nextcloud Docker image from the Docker Hub registry. This image is maintained by the Nextcloud community and is a great starting point for your deployment.

With the image pulled, you can then create a new Docker container using the following command: `docker run -d --name nextcloud -p 80:80 nextcloud`. This command creates a new container named "nextcloud" and maps port 80 on the host machine to port 80 in the container.

Next, you'll need to configure the container to use HTTPS by creating a new file called `nextcloud.conf` in the `/etc/nginx/conf.d/` directory. This file will contain the SSL configuration for your Nextcloud server.

Getting Started

To get started with Nextcloud on Docker, you'll want to start by running the command to start the containers in the background from the "nextcloud" directory where your "docker-compose.yaml" file is located.

The Docker images will start downloading, which can take anywhere from seconds to a few minutes depending on your internet connection speed.

You should see a screen informing you that your dockerized Nextcloud installation has been successfully deployed and is up and running.

Prerequisites

Credit: youtube.com, PROCESIO - Getting Started | Introduction & Prerequisites

Before you start, make sure you have a few things in place. You'll need a Linux server, which can be a physical one, a virtual machine, or a cloud server.

To get started with Docker and Docker Compose, you'll need to install them on your Linux server. If you're using Ubuntu or CentOS, you can follow the installation guides for Docker and Docker Compose.

Having some knowledge of Docker and Docker Compose will also come in handy.

You'll also need a domain name to deploy your project under, and HTTPS will be enabled for security.

Don't worry if you're not a Linux expert yet – some experience with shell and Linux commands is all you need.

To send email notifications and password reset emails, you'll need access to an SMTP service like Sendgrid.

Lastly, be prepared to spend some time and have patience, as this process requires some effort.

Setting Up Environment

To set up the environment for your Nextcloud deployment, you'll need to create a new folder and switch to it. This is where your Docker Compose deployment files will be placed.

Credit: youtube.com, Getting Started: Setting up Virtual Environment

Using your preferred text editor, create a “.env” file in this folder. This file will contain the environment variables needed to configure your Nextcloud containerized instance. The “.env” file lists key pair values used to store configuration parameters and environment variables for your Dockerized application.

Here are the environment variables you'll need to set in the “.env” file:

  • MYSQL_ROOT_PASSWORD: The password for the MariaDB root superuser account.
  • MYSQL_USER: The name of the user interacting with the Nextcloud database.
  • MYSQL_PASSWORD: The password for the user interacting with the Nextcloud database.
  • MYSQL_DATABASE: The name of the Nextcloud database.
  • MYSQL_HOST: The service name set in the “docker-compose.yaml” file for the MariaDB container.
  • REDIS_HOST: The service name set in the “docker-compose.yaml” file for the Redis container.
  • OVERWRITEPROTOCOL: The protocol of the proxy (Caddy) service, which will be HTTPS in our case.
  • TRUSTED_PROXIES: Allows the Nextcloud container to get the visitor's real IP address sent by the Caddy container.
  • APACHE_DISABLE_REWRITE_IP: Disables the IP addresses to be rewritten.
  • OVERWRITEHOST: The proxy hostname.

Make sure to replace the values for the above variables with the ones you want.

Docker Configuration

You can auto configure your Nextcloud Server installation via environment variables, which allows you to preconfigure everything that would otherwise be prompted for by the Nextcloud Installation Wizard.

The Nextcloud image supports this feature, making it a convenient option for initial setup.

To get a fully featured and functional setup, using a compose.yaml file with Docker Compose is the easiest way to go.

Create Network

This network will provide network visibility for all containers in your Nextcloud installation, allowing them to interact with each other.

The network will also isolate the Nextcloud stack from other Docker containers on your host, preventing any potential conflicts.

We create a Docker network by specifying its name, in this case, "nextcloud_network."

Auto Configuration via Env Variables

Credit: youtube.com, Docker Environment Configs, Variables, and Entrypoints

Auto configuration via environment variables is a powerful feature of the Nextcloud image. It allows you to preconfigure the installation by setting environment variables, which can save you time and effort.

You can use environment variables to set everything that would otherwise be prompted for by the Nextcloud Installation Wizard, as well as a few other key parameters relevant to initial installation. This includes things like database credentials and server settings.

The Nextcloud image supports a range of environment variables that can be used for auto configuration. Some of the most useful ones include:

  • APACHE_DISABLE_REWRITE_IP (not set by default): Set to 1 to disable rewrite IP.
  • TRUSTED_PROXIES (empty by default): A space-separated list of trusted proxies. CIDR notation is supported for IPv4.
  • OVERWRITEHOST (empty by default): Set the hostname of the proxy. Can also specify a port.
  • OVERWRITEPROTOCOL (empty by default): Set the protocol of the proxy, http or https.
  • OVERWRITECLIURL (empty by default): Set the cli url of the proxy (e.g. https://mydnsname.example.com)
  • OVERWRITEWEBROOT (empty by default): Set the absolute path of the proxy.
  • OVERWRITECONDADDR (empty by default): Regex to overwrite the values dependent on the remote address.
  • FORWARDED_FOR_HEADERS (empty by default): HTTP headers with the original client IP address

By using these environment variables, you can automate the configuration process and get up and running with Nextcloud quickly and easily.

Converting to Alpine Image

If you're using a non-Alpine image and want to switch to an Alpine-based image, you'll need to be aware of potential permissions problems with your existing volumes.

The Alpine images use a different user ID for www-data, which can cause issues.

Changing the ownership of the /var/www/html (or $NEXTCLOUD_DATA_DIR) folder is necessary to make it compatible with Alpine.

You'll need to adjust the permissions of this folder to resolve the permission errors.

After changing the permissions, restart the container and the permission errors should disappear.

Storage and Data

Credit: youtube.com, Setting Up NextCloud on Docker w/ NGINX and Cloudflare for Remote Access!

Nextcloud stores all your files and data in a special folder, and it's essential to understand how this works, especially if you're using a Docker setup.

The default data directory is located at /var/www/html/data, but you can change this to a custom location by setting the NEXTCLOUD_DATA_DIR variable.

If you're using a Docker setup, be aware that the data directory is stored in an unnamed Docker volume, which means it will persist even if the container crashes or is deleted.

To use a custom data directory, you can set the NEXTCLOUD_DATA_DIR variable to a different location. This is useful if you want to separate your data from the Docker container.

Nextcloud also allows you to use object storages, such as OpenStack Swift or Amazon S3, as primary storage. This means you can store your files in a cloud-based storage solution instead of locally on your server.

To use an external object store, you'll need to set up the following variables: OBJECTSTORE_S3_BUCKET, OBJECTSTORE_S3_REGION, OBJECTSTORE_S3_HOST, OBJECTSTORE_S3_PORT, OBJECTSTORE_S3_KEY, OBJECTSTORE_S3_SECRET, and OBJECTSTORE_S3_STORAGE_CLASS.

Credit: youtube.com, How to Set Up Your Own Nextcloud Server...Step-by-Step!

Here's a table summarizing the variables you need to set up for S3 object storage:

Remember to set up these variables correctly to ensure your data is stored securely and efficiently.

Security and Encryption

To ensure your Nextcloud installation is secure, consider using a reverse proxy in front of it. This will encrypt all traffic to clients, making it a more secure option.

Using a reverse proxy also gives you the flexibility to mount manually generated certificates or use a fully automated solution that generates and renews certificates for you. This can save you time and effort in managing your Nextcloud setup.

One popular fully automated setup uses the nginx-proxy and acme-companion containers, which can be found in the examples section of the documentation.

Ssl Encryption

SSL encryption is a must for any Nextcloud installation. It ensures that all data transmitted between your cloud storage and clients is secure.

To introduce encryption, you can use a reverse proxy in front of your Nextcloud installation. This is the recommended approach, as it encrypts all traffic to clients.

Credit: youtube.com, SSL, TLS, HTTP, HTTPS Explained

A reverse proxy can be configured to use manually generated certificates, or you can use a fully automated solution that generates and renews certificates for you. One such example is using the nginx-proxy and acme-companion containers, which can be found in the examples section of the documentation.

If you're looking to set up trusted domains, you can use the NEXTCLOUD_TRUSTED_DOMAINS environment variable. This will add the specified domains to the configuration after installation. The variable accepts a space-separated list of domains.

Email and SMTP

Email and SMTP setup is a crucial part of getting your Nextcloud Docker instance up and running.

To use an external SMTP server, you need to provide the connection details. This includes the hostname of the SMTP server, which is set via the SMTP_HOST variable.

You should not use the Nexcloud Web UI to configure external SMTP server parameters if you've already set these variables via Docker.

Credit: youtube.com, How to Configure SMTP server in Nextcloud for Password Reset and Email notification using Gmail

To configure Nextcloud to use SMTP, you'll need to set the following variables: SMTP_HOST, MAIL_FROM_ADDRESS, and MAIL_DOMAIN. These are the minimum required parameters.

Here are the variables you'll need to set for SMTP configuration:

  • SMTP_HOST: The hostname of the SMTP server.
  • MAIL_FROM_ADDRESS: Set the local-part for the 'from' field in the emails sent by Nextcloud.
  • MAIL_DOMAIN: Set a different domain for the emails than the domain where Nextcloud is installed.

Note that the SMTP_PORT variable is optional, but you may need to set it if your SMTP server requires a specific port for secure connections.

Database and Frontend

The database and frontend are the backbone of your Nextcloud setup. The database service uses the mariadb:10.5.9 image as its blueprint and is stored in a volume named NCMariaDB.

For persistent storage, the database stores its data in /var/lib/mysql. The database and frontend containers share the common network, enabling communication between them. This is crucial for a seamless experience.

To ensure data safety, the frontend service uses a volume named NCData to persist data in /var/www/html. The depends_on array in the frontend service ensures that the database starts before Nextcloud, avoiding connection issues.

Nc Frontend

Credit: youtube.com, Basic Database, Backend, and Frontend Decoupling

To make sure you keep your data safe, and don't lose it in an unfortunate event of a simple container restart, data must be persisted. Nextcloud saves its data/information in /var/www/html, so this location is made persistent using a volume named NCData.

The depends_on array is an interesting one, making sure that the values, i.e. the services mentioned in it are deployed first before the current one starts. Here, I'm making sure that the database is starting before Nextcloud itself, to avoid any connection issues.

There are two networks, the first one is net, which was also part of the reverse-proxy deployment. This is necessary because the reverse-proxy needs to be able to communicate with the proxied services, i.e. Nextcloud.

To host your Nextcloud instance, set LETSENCRYPT_HOST, VIRTUAL_HOST & NEXTCLOUD_TRUSTED_DOMAINS to the domain/subdomain you want.

NC Database

The NCDatabase is the database service that uses the mariadb:10.5.9 image as its blueprint.

To set up the database, you'll need to create a volume named NCMariaDB, which is mounted at /var/lib/mysql, where MariaDB stores its data.

Credit: youtube.com, How do NoSQL databases work? Simply Explained!

You'll also need to set environment variables in the .env file, such as MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, and MYSQL_USER.

You can generate a random password for MariaDB using openssl.

The common network is shared between the database and frontend services, allowing communication between containers.

The database image used is nextcloud:21.0.0, which is the latest version at the time of writing.

You can set the admin user and password in the .env file to avoid creating an account through the web UI.

The database stores various types of Nextcloud data, including user accounts, file metadata, sharing permissions, app configurations, and more.

MariaDB is designed to handle larger datasets and higher concurrent connections efficiently.

A Docker volume keeps MariaDB data surviving between container restarts.

Web Server and Reverse Proxy

To get HTTPS Nextcloud up and running with Docker, you'll need to set up a web server and reverse proxy. Apache and Nginx are two popular choices for this job.

A reverse proxy can be set up using a separate Compose file, allowing you to restart or update web-services without disturbing your reverse proxy setup. This approach is recommended when using Linode.

Credit: youtube.com, Nextcloud Nginx Proxy Manager in 10 Minutes!

If you're using a reverse proxy, you can deploy several web services on the same server. This is not optional because you need a Let's Encrypt container for SSL. Open the .env file in your favorite text editor and start changing the values as needed.

There are two ways to set up the Nginx reverse proxy. If you're not using Linode, please go through my article on how to set up nginx-reverse-proxy with Docker. If you are using Linode, I recommend using our reverse-proxy-jwilder StackScript to easily deploy your server.

To set up a reverse proxy with Nginx, you'll need to create a custom "nginx.conf" file and paste the contents below. Then, save the file and exit. This file makes sure that you can upload files as big as 1 GB in size (the default is 2 MB).

Here are the parameters you can use to set up a reverse proxy with Nginx:

  • OVERWRITEPROTOCOL: Set the protocol of the proxy to HTTPS.
  • OVERWRITEHOST: Set the hostname of the proxy.
  • OVERWRITEWEBROOT: Set the absolute path of the proxy.

For Caddy to work as a reverse proxy server, it needs to scan Docker metadata, looking for labels indicating that it should serve a specific container. You'll need to mount the Docker socket on the host with that of the container.

Here's an example of how to set up the Caddy service section in your "docker-compose.yaml" file:

```yml

caddy:

image: caddy:latest

ports:

  • "80:80"
  • "443:443"

volumes:

  • ./caddy/Caddyfile:/etc/caddy/Caddyfile
  • ./caddy/data:/data
  • ./caddy/config:/etc/caddy

labels:

  • "traefik.enable=true"
  • "traefik.http.routers.nextcloud.rule=Host(`nextcloud.example.com`)"
  • "traefik.http.routers.nextcloud.service=nextcloud"
  • "traefik.http.services.nextcloud.loadbalancer.server.port=80"

```

Note that you'll need to replace the "nextcloud.example.com" with your actual domain name.

Deployment and Migration

Credit: youtube.com, How to Migrate Your Docker Container to Another Server

You can migrate an existing Nextcloud installation to Docker by defining your infrastructure in a compose.yaml file and running it with docker compose up -d. This will get you the base installation, volumes, and database.

To restore your database, use a mysqldump and run the following commands: docker compose cp ./database.dmp db:/dmp, docker compose exec db sh -c "mysql --user USER --password PASSWORD nextcloud < /dmp", and docker compose exec db rm /dmp.

You'll also need to edit your config.php and copy your data to the correct location. This can be done by running docker compose cp ./data/ app:/var/www/html and docker compose exec app chown -R www-data:www-data /var/www/html/data.

Here are the database and Nextcloud versions recommended for deployment: mariadb:10.5.9 and nextcloud:21.0.0.

To deploy the services, create a new directory and a file called docker-compose.yaml. This file should declare the traefik, ncdatabase, and nextcloud services.

Existing Installation Migration

Migrating an existing Nextcloud installation to a Docker setup can be a bit tricky, but don't worry, I've got you covered.

Credit: youtube.com, Webinar: Simplify Your Life - API Deployment and Migration

First, define your Nextcloud infrastructure in a compose.yaml file and run it with docker compose up -d to get the base installation, volumes, and database.

To restore your database, you'll need to use a mysqldump file. This is where things can get a bit complicated, but trust me, it's worth it.

You'll need to copy the mysqldump file to the database container, then execute it using the mysql command. Here's the exact command to use: docker compose exec db sh -c "mysql --user USER --password PASSWORD nextcloud < /dmp"

Next, you'll need to delete the dump file from the container to free up space.

After that, you can copy the dump file back to the host machine and repeat the process.

To copy your data, you'll need to use the docker compose cp command. This command allows you to copy files from the host machine to the container, and vice versa.

Here's a step-by-step guide to copying your data:

1. Copy the data directory from the host machine to the container: docker compose cp ./data/ app:/var/www/html

Credit: youtube.com, Data Migration Process - Basics

2. Change the ownership of the data directory to the www-data user: docker compose exec app chown -R www-data:www-data /var/www/html/data

3. Copy the theming directory from the host machine to the container: docker compose cp ./theming/ app:/var/www/html

4. Change the ownership of the theming directory to the www-data user: docker compose exec app chown -R www-data:www-data /var/www/html/theming

5. Copy the config.php file from the host machine to the container: docker compose cp ./config/config.php app:/var/www/html/config

6. Change the ownership of the config.php file to the www-data user: docker compose exec app chown -R www-data:www-data /var/www/html/config

If you want to preserve the metadata of your files, such as timestamps, you can use the cp command with the --preserve option. This will ensure that the metadata is copied along with the files.

Here's an example of how to use the cp command with the --preserve option:

cp --preserve --recursive ./data/ /path/to/nextcloudVolume/data

Finally, you'll need to copy any custom apps you're using to the container. You can do this using the docker compose cp command, just like you did with the data and theming directories.

Finally, Deploying

Credit: youtube.com, What's your #cloud #migration #strategy?

Finally, deploying your Nextcloud server with Docker involves a few key steps. You'll need to create a network for your containers, which is used in the docker-compose.yaml file.

To deploy the containers, you can either use Git to clone the repository or download the necessary files using wget. If using Git, clone the whole repository and navigate to the Nextcloud directory.

There are four containers that should be running on your server once deployed: the nginx container, the letsencrypt companion container, the mariadb container, and the actual Nextcloud container.

The images used for deployment are mariadb:10.5.9 and nextcloud:21.0.0. You can download the skeleton files, including the compose file and env files, from the public GitHub repository.

To bring up the stack, run the command `docker-compose up -d` to run it in detached mode, or `docker-compose up` to see the logs in your terminal.

Here's a quick rundown of the containers that should be running:

If all goes correctly, you should be able to access your cloud instance at the domain you selected.

Post Deployment

Credit: youtube.com, Video10 Post Deployment Support JPA

After deploying your Nextcloud instance, you should first open your browser and head over to the domain it's hosted on, which is specified in the VIRTUAL_HOST variable.

You'll see a screen that's similar to the one described, unless you set the admin user using the compose file.

One thing to note is that the database environment variables are shared through the common .env file, so you won't see the usual web install UI that asks for database information.

You should uncheck the checkbox on this screen and install any necessary apps later on from the app list.

Jennie Bechtelar

Senior Writer

Jennie Bechtelar is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for distilling complex concepts into accessible language, Jennie has established herself as a go-to expert in the fields of important and industry-specific topics. Her writing portfolio showcases a depth of knowledge and expertise in standards and best practices, with a focus on helping readers navigate the intricacies of their chosen fields.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.