Migrations Migrating from Heroku, Render, Hatchbox or Cloud 66? We move your production app within 48 hours →
Self-host Nextcloud on your own server ← Blog vs. competitors • 13 min read

Self-host Nextcloud on your own server

Almokhtar Bekkour

Almokhtar Bekkour, Founder, Systems Architect

September 23, 2026 · 13 min read

Follow this on your own server. Start your 14-day trial

Google Drive and Dropbox are convenient right up to the point where the thing holding your files, your calendar, your contacts and a decade of family photos is a company that can change its pricing, scan your data, or lock the account over an automated flag you can’t appeal. Nextcloud does the same job, file sync across every device, shared folders, a calendar and contacts server, a photo gallery, collaborative documents, as open-source software on a server you control. By the end of this guide you’ll have your own Nextcloud running over HTTPS at your own domain, backed by Postgres instead of SQLite and Redis for file locking, able to accept large uploads, with a backup that actually restores and an upgrade path that won’t brick the install.

Nextcloud behind a reverse proxy has two things a naive Docker setup gets wrong, and both bite later rather than at first boot. The first is the set of .well-known redirects the proxy has to serve, without them Nextcloud’s own security check complains and desktop/mobile clients can’t autodiscover CalDAV and CardDAV. The second is backups: Nextcloud’s state is split across three places, the user files on disk, the database, and config/config.php, and a backup that captures only one of them restores to a broken instance. This setup handles both from the start.

What you’ll need

  • A server. Nextcloud is comfortable on a 2 vCPU / 4 GB VPS for a household or small team; 1 GB is tight once Redis, Postgres and the PHP workers are all running and someone starts syncing. Storage is the real variable, size the disk to the files you intend to keep. Any Ubuntu 24.04 box works.
  • A domain name (or a subdomain of one you already own) with access to its DNS.
  • Docker and the Docker Compose plugin on the server (Step 2).
  • SSH access and a terminal.

Step 1: Create the server and point a domain at it

Create an Ubuntu 24.04 server with at least 2 GB of RAM, and a disk large enough for the files you plan to store. Note its public IP.

In your DNS, add an A record for a subdomain, for example cloud.yourdomain.com, pointing at that IP:

Type: A
Name: cloud
Value: <your server's IP>
TTL: 300

Confirm it resolves before continuing, since TLS depends on it:

dig cloud.yourdomain.com +short

That should print your server’s IP.

Step 2: Install Docker

SSH in and install Docker Engine with the Compose plugin:

curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER

Log out and back in for the group change to take effect, then check both pieces are present:

docker --version
docker compose version

Step 3: Lay out the project and its secrets

Create a directory for the stack and an .env file for everything that shouldn’t live in the compose file:

mkdir -p ~/nextcloud && cd ~/nextcloud

Generate two long random passwords, one for Postgres and one for the Nextcloud admin account:

openssl rand -base64 24
openssl rand -base64 24

Create .env with those values, your domain, and the admin username:

# .env
DOMAIN=cloud.yourdomain.com
POSTGRES_DB=nextcloud
POSTGRES_USER=nextcloud
POSTGRES_PASSWORD=<the first password you generated>
NEXTCLOUD_ADMIN_USER=admin
NEXTCLOUD_ADMIN_PASSWORD=<the second password you generated>

Lock it down so only your user can read it:

chmod 600 .env

Step 4: Write the Docker Compose file

Nextcloud’s default database is SQLite, which the project only recommends for testing. Past that, you want a real database and Redis for Nextcloud’s transactional file locking and caching. The locking matters once more than one client syncs: when two devices touch the same file at once, one finishing a sync while another starts an edit, Nextcloud takes a short-lived lock so the writes don’t interleave and corrupt the file. Without a shared cache it does this in the database, slow and contending with everything else; with Redis it’s fast and out of the way. On any multi-client instance, Redis is the difference between sync that holds together and “file is locked” errors that never clear.

Create docker-compose.yml:

services:
  nextcloud:
    image: nextcloud:30.0.2-apache
    restart: unless-stopped
    environment:
      - POSTGRES_HOST=db
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - REDIS_HOST=redis
      - NEXTCLOUD_ADMIN_USER=${NEXTCLOUD_ADMIN_USER}
      - NEXTCLOUD_ADMIN_PASSWORD=${NEXTCLOUD_ADMIN_PASSWORD}
      - NEXTCLOUD_TRUSTED_DOMAINS=${DOMAIN}
      - OVERWRITEPROTOCOL=https
      - OVERWRITECLIURL=https://${DOMAIN}
    volumes:
      - nextcloud_data:/var/www/html
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    networks:
      - nextcloud-net

  cron:
    image: nextcloud:30.0.2-apache
    restart: unless-stopped
    entrypoint: /cron.sh
    volumes:
      - nextcloud_data:/var/www/html
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    networks:
      - nextcloud-net

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    volumes:
      - nextcloud_db:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 10
    networks:
      - nextcloud-net

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    networks:
      - nextcloud-net

  caddy:
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    environment:
      - DOMAIN=${DOMAIN}
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - nextcloud
    networks:
      - nextcloud-net

networks:
  nextcloud-net:

volumes:
  nextcloud_data:
  nextcloud_db:
  caddy_data:
  caddy_config:

The choices that matter here:

  • The image is pinned to 30.0.2-apache, not latest. The apache variant bundles a web server so there’s no separate PHP-FPM wiring; Caddy just proxies to port 80. Pinning matters more here than for most apps because of how upgrades work (Step 7), a latest tag that jumps two major versions on a restart is a broken install, not an upgrade.
  • NEXTCLOUD_TRUSTED_DOMAINS must be the exact hostname you’ll reach Nextcloud at. Nextcloud refuses requests whose Host header isn’t in that list, a deliberate guard against host-header attacks, so a mismatch shows up as an “access through untrusted domain” page instead of your login.
  • OVERWRITEPROTOCOL=https and OVERWRITECLIURL tell Nextcloud it’s served over HTTPS at a known URL even though the proxy terminates TLS and talks to it over plain HTTP internally. Without OVERWRITEPROTOCOL it builds http:// links for assets and shares, which browsers block as mixed content. OVERWRITECLIURL (Nextcloud’s overwrite.cli.url) is the base URL used when links are generated with no incoming request to infer the host from, cron-generated emails get their links from it.
  • Only Caddy publishes ports to the host. Nextcloud, Postgres and Redis are reachable only on the internal Docker network.

Why the cron container exists

Nextcloud has a queue of background work, trimming file versions, generating previews, running app maintenance, sending notification emails, that it expects to run every five minutes. A fresh install uses “AJAX” cron, which only fires when someone has a browser tab open, so an instance nobody is actively looking at quietly stops maintaining itself: previews never generate, the trash never empties, federated shares drift. The fix is a dedicated cron process. The official image ships one, run the same image with its entrypoint set to /cron.sh, sharing the nextcloud_data volume so it sees the same code and config, and it invokes cron.php on a five-minute timer. After first boot, set Administration settings → Basic settings → Background jobs to Cron so Nextcloud knows the external runner is handling it. The companion container plus that setting is what keeps a self-hosted Nextcloud maintaining itself.

Step 5: Write the Caddyfile and start everything

Caddy handles the Let’s Encrypt certificate automatically once DNS points at the server and ports 80 and 443 are reachable. It also streams request bodies rather than buffering them, so large uploads flow through with no size cap to configure, where Nginx would need an explicit client_max_body_size (and a matching PHP upload_max_filesize) or multi-gigabyte uploads would fail at the proxy.

The redirects are the part people miss. Nextcloud publishes CalDAV, CardDAV, WebFinger and NodeInfo discovery under /.well-known/, and clients (and Nextcloud’s own security check) expect requests there to be redirected to the real endpoints. Serve them at the proxy. Create Caddyfile:

{$DOMAIN} {
	redir /.well-known/carddav /remote.php/dav/ 301
	redir /.well-known/caldav /remote.php/dav/ 301
	redir /.well-known/webfinger /index.php/.well-known/webfinger 301
	redir /.well-known/nodeinfo /index.php/.well-known/nodeinfo 301

	reverse_proxy nextcloud:80
}

Without the first two redirects, adding a calendar or contacts to a phone by typing just the server URL fails, because the client probes /.well-known/caldav, gets a 404, and never finds /remote.php/dav/. Without the WebFinger and NodeInfo lines, the admin overview shows a .well-known security warning that never clears.

Start the stack and confirm Caddy got a certificate:

docker compose up -d
docker compose logs -f caddy

You should see a line about a certificate obtained for your domain. Then open https://cloud.yourdomain.com, you’ll land already logged in as the admin account from your .env (the image runs the install on first boot using those variables), with a valid padlock. Go to Administration settings → Overview and confirm the security check is clean; the .well-known warnings should be gone because Caddy is handling them.

Step 6: Back up the files, the database and the config together

This is the step that decides whether a dead server is an afternoon or a disaster, and the one most people get half-right. Nextcloud’s state lives in three places: the user files on disk, the Postgres database (accounts, shares, metadata, calendar and contact entries, which file belongs to whom), and config/config.php (the instance’s own config, including the secret that decrypts anything server-side encryption touched). Here the files and config.php both live inside the nextcloud_data volume at /var/www/html, so one tar of that captures both; the database is separate. Restore a database without the matching files and every file is a broken pointer; restore files without the database and Nextcloud doesn’t know they exist.

The other half of getting this right is taking all of it from the same point in time. Put Nextcloud in maintenance mode first so nothing writes a file or a row mid-backup:

docker compose exec --user www-data nextcloud php occ maintenance:mode --on

docker compose exec -T db pg_dump -U nextcloud -Fc nextcloud > "nextcloud-db-$(date +%F).dump"

docker run --rm -v nextcloud_nextcloud_data:/data -v $(pwd):/backup alpine \
  tar czf /backup/nextcloud-data-$(date +%F).tar.gz -C /data .

docker compose exec --user www-data nextcloud php occ maintenance:mode --off

occ is Nextcloud’s command-line admin tool, and it has to run as the web-server user (www-data) that owns the files, which is why every occ call here is docker compose exec --user www-data nextcloud php occ .... Running it as root leaves files Nextcloud can’t read afterward.

Put the whole sequence in a cron job that runs off-hours, since maintenance mode takes the instance offline for the length of the snapshot, and copy both artifacts to object storage or another machine. A backup that lives only on the server it protects doesn’t survive the failure that destroys the server. To restore onto a fresh box: bring the stack up once so the volumes exist, turn on maintenance mode, pg_restore the dump into the database, extract the tarball back into the nextcloud_data volume, turn maintenance mode off. Both halves must come from the same backup run.

Step 7: Upgrade Nextcloud without breaking it

Here is the single most important rule for Nextcloud, the one that wrecks the most self-hosted installs: Nextcloud upgrades one major version at a time, and will not skip. You go 28 → 29 → 30, in order. You cannot go 28 → 30 directly; the upgrader checks the version it’s coming from and refuses the jump, and if you force it by pointing the image at a too-new tag you get an instance that won’t start and a database mid-migration. This is exactly why Step 4 pins an explicit tag instead of latest.

So an upgrade is deliberate. Take a Step 6 backup first, then check what you’re on:

docker compose exec --user www-data nextcloud php occ status

Then bump the tag in docker-compose.yml by exactly one major version, for example from 30.0.2-apache to 31.0.0-apache, and pull and recreate just that container plus its cron twin:

docker compose pull nextcloud cron
docker compose up -d nextcloud cron

The official image runs the upgrade on boot when it detects a newer version in the same major line or the next one up. Watch it finish and confirm:

docker compose logs -f nextcloud
docker compose exec --user www-data nextcloud php occ status

occ status should report the new version and maintenance: false. To move across several majors, repeat the whole loop, one major, let it settle, back up, next major, rather than reaching for a shortcut that doesn’t exist.

The shortcut: do it with Railyard

Everything above, Nextcloud backed by its own Postgres and Redis, a persistent data volume, the cron companion, the .well-known redirects and HTTPS already wired up, is also a one-click template:

https://app.railyard.run/deploy?template=nextcloud-with-postgres

That gives you the same architecture this guide builds by hand: the Nextcloud app with a dedicated Postgres and a Redis for file locking, data on a persistent volume so your files survive restarts and redeploys, background jobs on a real cron process, and TLS at your own domain, with the reverse-proxy redirects already in place so the security check is clean on the first visit. The two things this guide spends the most care on, the proxy .well-known routing and a backup that captures files, database and config together, are already right.

If you want to understand what runs on your server first, what happens on your server when you connect walks through it, and databases and backups explains how scheduled backups work across every template. For how the one-click link format itself works, see deploy button.

Troubleshooting

The page says “access through untrusted domain.” The hostname isn’t in Nextcloud’s trusted-domains list. Confirm NEXTCLOUD_TRUSTED_DOMAINS in .env is the exact domain you’re visiting; to add one after first boot, run docker compose exec --user www-data nextcloud php occ config:system:set trusted_domains 1 --value=cloud.yourdomain.com.

The admin overview shows .well-known warnings that won’t go away. Caddy isn’t serving the redirects. Confirm the four redir lines are in the Caddyfile, that it’s mounted into the container, and that you reloaded Caddy (docker compose restart caddy) after editing it. It’s cosmetic for browsing but means CalDAV/CardDAV autodiscovery is broken for clients.

Assets or share links come out as http:// and get blocked as mixed content. OVERWRITEPROTOCOL=https isn’t taking effect. Confirm it’s set on the nextcloud service and that Caddy forwards X-Forwarded-Proto (it does by default). For links in cron-generated emails, also confirm OVERWRITECLIURL points at your real https:// URL.

“File is locked” errors that never clear, or sync stalls with two devices. Redis isn’t being used for locking, or isn’t reachable. Check REDIS_HOST=redis is set and docker compose logs redis shows it running, then confirm Nextcloud picked it up with docker compose exec --user www-data nextcloud php occ config:system:get memcache.locking.

Previews never generate and the trash never empties. Background jobs aren’t running. Confirm the cron container is up (docker compose ps) with the /cron.sh entrypoint, and that Background jobs in the admin settings is Cron, not AJAX. With AJAX, maintenance only runs while a tab is open.

Large uploads fail partway through. With Caddy in front this is rarely the proxy, since it streams bodies with no cap, so look at PHP limits inside the container: docker compose exec --user www-data nextcloud php -i | grep -E "upload_max_filesize|post_max_size", and the server’s free disk, a full data volume shows up as a failed upload too.

Postgres keeps restarting, or Nextcloud can’t connect. Check docker compose logs db first. The usual cause is a POSTGRES_PASSWORD changed in .env after the volume was first initialized; Postgres only reads those variables on the very first boot with an empty data directory, so the volume still holds the old password. Set it back, or remove the nextcloud_db volume to initialize fresh (which erases the database, so only before you have real data).

FAQ

Do I need Postgres and Redis, or is the default enough? SQLite with AJAX cron works for a single user kicking the tyres, but it doesn’t hold up once more than one device syncs or the instance is actually relied on. Postgres and Redis from the start is the honest default and avoids a painful migration later.

Can I run this without a domain, just the server’s IP? No, in practice. Caddy’s automatic HTTPS needs a real domain, Let’s Encrypt won’t issue for a bare IP, and Nextcloud’s trusted-domains and overwrite URLs assume a stable hostname. Don’t sync real files to an instance that isn’t behind HTTPS.

How much does this cost to run? The server, roughly $12–24/month for a 2–4 GB VPS, plus whatever disk your files need. Nextcloud itself is free, open-source software with no per-user or per-gigabyte fee, which is the whole point against Drive and Dropbox storage tiers.

Will my phone and desktop clients work? Yes. The official Nextcloud desktop, iOS and Android apps point at https://cloud.yourdomain.com and sync files, and thanks to the .well-known redirects you can add the calendar and contacts to your phone’s built-in CalDAV/CardDAV accounts with just the server URL.

What has to be backed up, exactly? Three things from the same point in time: the user files, the Postgres database, and config/config.php. Here the files and config.php both sit inside the nextcloud_data volume, so one tar of that plus a pg_dump covers all three, taken in maintenance mode so nothing changes mid-backup. Lose config.php and, if you ever enabled server-side encryption, the key that decrypts your files goes with it.

Either path gets you the same thing: your own cloud, your own files, calendar, contacts and photos, and a storage bill that doesn’t climb every time you add a person or fill another gigabyte.

Why people pick Railyard for this

  • Your own cloud account. Deploy to DigitalOcean, Hetzner, Vultr, AWS, OVHcloud, or any Linux server over SSH, billed directly by your provider with no markup.
  • One flat price per team. Launch is $22 a month with 2 servers and 1 seat. Scale is $45 with 5 servers and 5 seats, Team is $79 with 10 servers and 15 seats, and Agency is $149 with 20 servers and unlimited seats. Unlimited apps on every plan, no charge per app, per deploy or per gigabyte, and a 14-day trial to start.
  • One-click templates. Deploy open-source apps, like databases, analytics and dev tools, to your own server in one click.
  • Managed databases. Postgres, MySQL and Redis with scheduled, restore-verified backups and point-in-time recovery.
  • Push-to-deploy. Deploy from GitHub, GitLab or the CLI, with automatic HTTPS and zero-downtime releases.

Skip the server setup

Launch the Nextcloud template on Railyard in one click and get your own cloud storage with HTTPS already wired up.

14-day trial on every plan. One flat price per team, your servers at your provider's price.

Theme
Colors