Loading...

Self-Hosting Nextcloud on a Hetzner Ubuntu Server

Jonas Scholz - Co-Founder von sliplane.ioJonas Scholz
6 min

Ever wished for a Dropbox or Google Drive alternative that YOU fully control? Enter Nextcloud — a fantastic open-source tool that lets you host your own secure cloud storage solution! 🎉 Hosting Nextcloud yourself puts privacy back in your hands, and today I'll show you exactly how it's done using Docker on an Linux Ubuntu server.

For this post, we're using an affordable server from Hetzner, which has quickly become one of my favorite providers for side projects and small self-hosting setups. We'll cover everything step-by-step—from initial server setup, firewall handling and Docker installation, all the way to a smoothly running Nextcloud instance complete with automatic HTTPS certificates. If you're looking for a simple, secure, and private cloud storage solution, this guide is for you!

Ready to own your files and drop the big tech dependency?

Watch the video tutorial

Step 1: Prepare Your Linux Server

Start by creating a Linux server instance. Ubuntu is a simple and reliable choice. Log into your server via SSH.

Once connected, ensure your system is current:

sudo apt-get update
sudo apt-get upgrade -y

Step 2: Setup a Firewall for Security

It is important to restrict the ports your server accepts connections on. Only the essential services (SSH on port 22, HTTP on port 80, and HTTPS on port 443) should be open:

sudo apt install ufw -y
sudo ufw allow 22    # SSH access
sudo ufw allow 80    # HTTP
sudo ufw allow 443   # HTTPS
sudo ufw enable

You can verify your rules are correct:

sudo ufw status verbose

Make sure that you also use the Firewall provided by your hosting provider, as Docker likes to circumvent UFW rules. Alternatively you can follow these steps from StackOverflow.


Step 3: Install Docker on Ubuntu Server

Docker is an easy tool to run applications inside isolated containers. Install Docker on Ubuntu by following these steps.

First, install the required packages and Docker's official GPG key so your system trusts Docker's packages:

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

Then add Docker's official repository to your server:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo $VERSION_CODENAME) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

Now install Docker and Docker Compose Plugin:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

Check that the Docker installation succeeded:

sudo docker run hello-world

Step 4: Setup Caddy Web Server to manage HTTPS

Caddy serves Nextcloud securely by providing automatic HTTPS certificates. Install Caddy with these commands:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
| sudo tee /etc/apt/sources.list.d/caddy-stable.list

sudo apt update
sudo apt install caddy -y

Once installed, configure Caddy using a config file called Caddyfile:

sudo nano /etc/caddy/Caddyfile

Enter the following simple configuration in the file (use your registered domain name):

yourdomain.com {
    reverse_proxy localhost:8000
}

If you haven't got a domain yet, temporarily use:

:80 {
    reverse_proxy localhost:8000
}

Save the Caddyfile and restart Caddy:

sudo systemctl restart caddy

Step 5: Deploy Nextcloud using Docker Compose

Create a directory for your Nextcloud deployment and enter that directory:

mkdir ~/nextcloud
cd ~/nextcloud

Create docker-compose.yml with the following content:

services:
  nextcloud:
    depends_on:
      - postgres
    image: nextcloud:apache
    environment:
      - POSTGRES_HOST=postgres
      - POSTGRES_PASSWORD=nextcloud
      - POSTGRES_DB=nextcloud
      - POSTGRES_USER=nextcloud
    ports:
      - 8000:80
    restart: always
    volumes:
      - nc_data:/var/www/html

  postgres:
    image: postgres:alpine
    environment:
      - POSTGRES_PASSWORD=nextcloud
      - POSTGRES_DB=nextcloud
      - POSTGRES_USER=nextcloud
    restart: always
    volumes:
      - db_data:/var/lib/postgresql/data
    expose:
      - 5432

volumes:
  db_data:
  nc_data:

Start Nextcloud and PostgreSQL with Docker Compose:

sudo docker compose up -d

Docker will pull images automatically and start the containers in the background:

Docker Compose


Step 6: Accessing Nextcloud

Once the docker containers start successfully, open your web browser and go to your domain (https://yourdomain.com) or use your IP. You will land on Nextcloud's initial installation page:

Nextcloud Signup

Pick a super safe password for the admin account. Then installing will take a few seconds, maybe a minute. Once you get redirected you can pick the plugins you want:

Nextcloud Installation Page

I'd suggest picking all to test Nextcloud out, you can always remove them:)

Then after installing them the setup is done and you get redirected to the dashboard:

Nextcloud Dashboard

You're done! You can now use your public Nextcloud instance. You can also use the Nextcloud app on your phone or desktop to sync files :)


Security Considerations

Running containers publicly means securing your server carefully. Consider adding:

  • Regular updates & security patches
  • Fail2ban for SSH protection
  • Strong passwords and access controls
  • Regular server and data backups

An Easier Alternative to Manual Self-Hosting

If manually setting up containers, managing security and servers is challenging, consider using a dedicated self-hosting provider like Sliplane. Sliplane is easy and affordable (starting at €9/month). It helps you host applications like Nextcloud without deep technical setup. You can follow the Nextcloud Setup guide and host a secure and scalable Nextcloud instance within 2 minutes:

Welcome to the container cloud

Sliplane makes it simple to deploy containers in the cloud and scale up as you grow. Try it now and get started in minutes!