How to deploy NextCloud in your Linux Server with docker and SSL
Tags: nextcloud, owncloud, linux, docker
Published: 2022-05-14 10:51:19

When you create your account and store your documents, images, etc in Google drive or dropbox, you are not the master of your data. Generally, we say that we use Google or Microsoft one drive for free but it’s not free. We pay for those free spaces with the personal data they collected. It’s where NextCloud comes in. In simple words, Nextcloud is your cloud infrastructure under your control.

## What is NextCloud ?

**NextCloud** is open-source software that allows you to run your personnel cloud service like dropbox. It gives you access to all your files wherever you are. It allows you to share and collaborate on documents, send and receive email, manage your calendar and have video chats. You can install the Nextcloud server software free on your Linux server and the client’s software on your Windows, OS X, or Linux machine, Android, and IOS mobile phone.

The main drawback here is you need to pay for your Linux server to your VPS provider and you will be responsible for your server maintenance unless your choose Nextcloud Enterprise which comes with the support. For example, if you choose the Contabo provider, you can have your fully functional cloud solution with 200GB SSD, 8GO of RAM, and 4vCPU for only $6.99 a month.

## Pre-requisite before the installation of Nextcloud

As we will deploy our NextCloud instance with docker, you need to have:
- A Linux Server with SSH and root access
- Docker and docker-compose installed on that server
- A domain name pointed to that server

## Deployment of NextCloud

```
POSTGRES_PASSWORD=yourdbstrongpassword
POSTGRES_DB=nextcloud
POSTGRES_USER=nextcloud
```

**db.env file**

```
version: "3.9"

services:
  nextcloud_db:
    image: postgres:alpine
    restart: always
    volumes:
      - nextcloud_dbdata:/var/lib/postgresql/data
    env_file:
      - db.env

  redis:
    image: redis:alpine
    restart: always

  nextcloud_web:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html
    environment:
      - VIRTUAL_HOST=cloud.yourdomain.com
      - LETSENCRYPT_HOST=cloud.yourdomain.com
      - LETSENCRYPT_EMAIL=yourmail # <===== For let's encrypt
      - POSTGRES_HOST=nextcloud_db
      - REDIS_HOST=redis
    env_file:
      - db.env
    depends_on:
      - nextcloud_db
      - redis

  cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html
    entrypoint: /cron.sh
    depends_on:
      - nextcloud_db
      - redis

volumes:
  nextcloud_dbdata:
  nextcloud:

#Use this configuration in production with nginx-proxy container
networks:
  default:
    external:
      name: nginx-proxy
```
**docker-compose.yml file**

Login to your Linux server and type the following command:

```
mkdir nextcloud && cd nextcloud
nano db.env
```
Copie the content of the **db.env** in the above GitHub gist and paste it into the newly created file. After this, create a **docker-compose.yml** file and copy the content of the docker-compose.yml in the above Github gist and paste it into.

**Note**: Don’t forget the change the environment variables **VIRTUAL_HOST** and **LETSENCRYPT_HOST** with your domain name and **LETSENCRYPT_EMAIL** with your email address.

Now, it’s time to create the docker network that would be used to drive secure traffic to our Nextcloud instance through our domain name.

```
docker network create nginx-proxy
```
Then let’s start our Nextcloud instance.

```
docker-compose up --build -d
```

![docker-compose ps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uoe5s4itvkbbgcrujtir.png)

Our Nextcloud instance is now running but is not accessible from the internet. We will now configure Nginx-proxy to drive traffic to our Nextcloud instance. We will explain our docker-compose file after making our instance fully functional.

## Installation of Nginx-proxy and acme-compagnon
Nginx proxy is a container running Nginx and docker-gen which is a service that generates reverse proxy configs for Nginx and reloads Nginx when containers are started or stopped.

This container is mounted on a docker socket to capture all events created by docker to be able to proxied any container with an env variable **VIRTUAL_HOST** define. All containers that want to be proxied by Nginx-proxy must be connected to the same network with it. To know more about Nginx-proxy, [visit the GitHub of the project](https://github.com/nginx-proxy/nginx-proxy).

ACME-compagnon is a compagnon for Nginx-proxy responsible to automate the creation, renewal, and use of SSL certificates for proxied Docker containers through ACME protocol. For more information about acme-compagnon, [visit the GitHub of the project](https://github.com/nginx-proxy/acme-companion).

I have a ready-to-use template for Nginx-proxy in my repository. You just need to clone and run it. I also use this template in all my projects. With this configuration, it’s easy to make things work in less than a minute. Just use the following command.

```
cd ~
git clone https://gitlab.com/tderick/nginx-proxy-conf.git 
docker-compose up --build -d
```
Now, your Nextcloud instance is running, and you can access it via your domain name.


![NextCloud first page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0jkkfeg0dfwtu4e2dxc.png)

When you install Nextcloud, it doesn’t come with an admin account by default. You need to create it. Just fill in the form on the first page and hit the **install** button. Don’t put email as username. if you do, you have the following error:

![Error page when we use email as username](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxlbvz8bvbge9gwpis89.png)

After putting in a username and password, we will arrive at the following page listing the recommended apps to install in our instance.


![Recommend app to be installed in our nextcloud instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysxhlkdnrrenmibilw55.png)

We can see there are applications for:
- Calendar
- Contacts
- Mail
- Online edition and collaboration

We will install other applications later. Just hit the **install recommended apps** button.


![Nextcloud Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1bxj8gs648pf3n73umz.png)

Our nextcloud instance is now installed and ready to use. Now, we can explain our docker-compose file to understand the magic behind this.

## Docker-compose file explanation

In this docker-compose file, we use version 3.9 and we expose three services, two volumes, and one default external network.

## nextcloud_db service

Nextcloud support multiple DBMS: MySQL, MariaDB, Oracle, PostgresSQL. It’s up to you to choose your favorite DBMS. We choose to use Postgres as it is a very powerful solution.

## redis service

Redis is an excellent modern memory cache solution to use for distributed caching. It’s used by Nextcloud to significantly improve the Nextcloud server performance with memory caching where frequently-requested objects are stored for faster retrieval.

## nextcloud_web service

It’s the official Nextcloud container with all the features offered.

## cron service

[Cron](https://fr.wikipedia.org/wiki/Cron) is a simple time-based job scheduler that runs small tasks on its own without the intervention of the user or the administrator. Cron is also an important part for Nextcloud to be running efficiently.

## Wrap up

In this tutorial, we explain to you how to deploy your Nextcloud instance in your Linux server with docker-compose and secure it with a free SSL certificate issued by Let’s Encrypt. Another thing you can do now is to deploy a [keycloak SSO ](https://temfack.com/keycloak-sso-on-linux-server-with-docker-and-ssl/)solution next to this to centralize authentication among all your application. It’s pretty easy at this step and doesn’t affect the previous installation. If you are instead a considerable fan of Owncloud, check my other blog post  about [the deployment of OwnCloud on a Linux server with docker](https://temfack.com/how-to-deploy-owncloud-on-linux-server-with-docker/).  If you have any questions, leave a comment.

If you like this tutorial, you can [buy me coffee](https://www.buymeacoffee.com/tderick). In the upcoming tutorial, we will more explore Nextcloud.