Skip to content

How to Deploy Mastodon on a VPS

Updated Nov 2026

verified on Ubuntu 26.04 · Nov 2026
We earn commissions when you shop through the links below. Full disclosure →

Self-host a Mastodon server with the official Docker Compose file behind Caddy HTTPS, an owner account from the CLI, plus backup and restore.

Before you start
  • A VPS with 4 GB RAM recommended (the catalog floor is 2 GB)
  • A fresh Ubuntu 24.04 or 26.04 server with root/sudo SSH access
  • A domain you will keep for good: it becomes part of every handle
  • An SMTP relay account if anyone besides you will sign up
Need a box for this guide? Kamatera's free tier lets you spin one up now.Start free on Kamatera → (opens in new tab)

What Mastodon is

Mastodon is a decentralised microblogging server built on ActivityPub: your server hosts accounts that follow and are followed by people on other servers. It is AGPL-3.0, a Ruby on Rails application with a Node.js streaming service, PostgreSQL and Redis.

This guide uses the Compose file shipped with release v4.7.6, adds Caddy, creates the owner account from the command line and backs the server up and restores it. Read these two points before starting:

  • LOCAL_DOMAIN is permanent. It is the domain in every handle (@you@social.example.com) and other servers remember it.
  • Mail is not set up here. Sign-up confirmation, password reset and notification emails need an SMTP service you provide (see Mail below).

Server sizing

The catalog floor is 2 GB of RAM; on the catalog's test box an idle Mastodon used about 850 MB and the install took about 2.2 GB of disk. That is an empty server. Federation fills the database and public/system with other people's posts and media, so plan for disk growth and run media cleanup regularly.

For choosing a provider and tier, see Best VPS for self-hosting.

Prepare the server

Update the system and install the few tools this guide uses:

sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get install -y curl openssl ufw dnsutils

Working as root? Create a sudo user first and continue as that user:

adduser deploy
usermod -aG sudo deploy

Open SSH and the two ports the reverse proxy needs:

sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable

The upstream Compose file already binds the web and streaming ports to 127.0.0.1; Caddy will be the public door. Docker publishes container ports around ufw, so a port bound to all interfaces would be reachable from the internet whatever the firewall says.

Where to host itaffiliate disclosure
Hetzner Cloudrun it on
2 vCPU · 4 GB RAM · 80 GB SSD · $23.59/mo
Get Hetzner Cloud (opens in new tab)
Kamaterafree trial
1 vCPU · 4 GB RAM · 20 GB SSD · $12.00/mo
Start free on Kamatera → (opens in new tab)
DigitalOceanalso works on
2 vCPU · 4 GB RAM · 80 GB SSD · $24.00/mo
Deploy on DigitalOcean → (opens in new tab)

Paid link — we earn a commission if you shop through it.

Install Docker

The convenience script installs Docker Engine and the Compose plugin (Docker and Compose on Ubuntu explains the details):

command -v docker >/dev/null || curl -fsSL https://get.docker.com | sudo sh
sudo docker compose version

Install Mastodon

Fetch the release's Compose file, which pins the v4.7.6 images, and prepare the media directory (the containers run as UID 991). Set your hostname and owner details:

mkdir -p ~/mastodon && cd ~/mastodon
[ -s docker-compose.yml ] || curl -fsSL --retry 5 -o docker-compose.yml \
  https://raw.githubusercontent.com/mastodon/mastodon/v4.7.6/docker-compose.yml
mkdir -p public/system && sudo chown -R 991:991 public/system
if [ ! -f .env ]; then
  ( umask 077
    {
      echo "DOMAIN=social.example.com"
      echo "OWNER_USER=alice"
      echo "OWNER_EMAIL=alice@example.com"
    } > .env )
fi
grep image: docker-compose.yml

Generate .env.production. The secrets must never change once the server holds data, so the guard keeps a re-run from replacing them:

cd ~/mastodon
. ./.env
if [ ! -f .env.production ]; then
  ( umask 077
    cat > .env.production <<EOF
LOCAL_DOMAIN=$DOMAIN
REDIS_HOST=redis
DB_HOST=db
DB_USER=postgres
DB_NAME=mastodon_production
SECRET_KEY_BASE=$(openssl rand -hex 64)
ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY=$(openssl rand -hex 32)
ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT=$(openssl rand -hex 32)
ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY=$(openssl rand -hex 32)
EOF
  )
  vapid=$(sudo docker compose run --rm -T web bundle exec rails mastodon:webpush:generate_vapid_key | grep '^VAPID_')
  echo "$vapid" >> .env.production
fi
grep -o '^[A-Z_]*' .env.production

The upstream file lets PostgreSQL trust every connection and gives the database only the internal Docker network, with no published port. Keep it that way.

Add Caddy in an override file. Streaming requests go to their own service:

cd ~/mastodon
. ./.env
cat > docker-compose.override.yml <<'EOF'
services:
  caddy:
    image: caddy:2
    restart: always
    ports: ["80:80", "443:443"]
    networks: [external_network]
    volumes: ["./Caddyfile:/etc/caddy/Caddyfile:ro", "caddy_data:/data"]
volumes:
  caddy_data:
EOF
cat > Caddyfile <<EOF
$DOMAIN {
    encode zstd gzip
    handle /api/v1/streaming* {
        reverse_proxy streaming:4000
    }
    handle {
        reverse_proxy web:3000
    }
}
EOF

Create the database schema, then start everything:

cd ~/mastodon
sudo docker compose up -d db redis
for i in $(seq 1 40); do
  sudo docker compose exec -T db pg_isready -U postgres >/dev/null 2>&1 && break
  sleep 2
done
sudo docker compose run --rm -T web bundle exec rails db:setup
cd ~/mastodon
sudo docker compose up -d
sudo docker compose ps

HTTPS and domain

Create an A record for your hostname (this guide uses social.example.com) pointing at the server's public IP, and check that it resolves:

dig +short social.example.com

Caddy requests the certificate once the name resolves and renews it by itself (see Automatic HTTPS with Caddy). Wait for it, with a bounded loop so a broken setup fails instead of hanging:

cd ~/mastodon
. ./.env
out=000
for i in $(seq 1 40); do
  out=$(curl -s -o /dev/null -w '%{http_code} %{ssl_verify_result}' "https://$DOMAIN/health")
  [ "$out" = "200 0" ] && break
  sleep 5
done
echo "HTTPS status and certificate check: $out"
[ "$out" = "200 0" ]

200 0 means the page answered and the certificate verified.

Create the owner account

No mail is needed for this. tootctl creates a confirmed account with the Owner role and prints a random password once; the block saves it to a private file. Then close public registration, because without working mail new users could not confirm their address:

cd ~/mastodon
. ./.env
( umask 077
  sudo docker compose exec -T web bin/tootctl accounts create "$OWNER_USER" \
    --email "$OWNER_EMAIL" --confirmed --role Owner > owner.txt )
sudo docker compose exec -T web bin/tootctl accounts approve "$OWNER_USER"
sudo docker compose exec -T web bin/tootctl settings registrations close
grep -c 'New password' owner.txt

Sign in over HTTPS with that password, the way a browser does:

cd ~/mastodon
cat > check-login.sh <<'EOF'
. ./.env
password=$(sed -n 's/^New password: //p' owner.txt)
jar=$(mktemp)
token=$(curl -s -c "$jar" -b "$jar" "https://$DOMAIN/auth/sign_in" | grep -o 'name="authenticity_token" value="[^"]*"' | head -1 | cut -d'"' -f4)
curl -s -c "$jar" -b "$jar" -o /dev/null -w 'sign-in: %{http_code} -> %{redirect_url}\n' "https://$DOMAIN/auth/sign_in" \
  --data-urlencode "authenticity_token=$token" --data-urlencode "user[email]=$OWNER_EMAIL" --data-urlencode "user[password]=$password"
curl -s -b "$jar" -o /dev/null -w 'preferences page while signed in: %{http_code}\n' "https://$DOMAIN/settings/preferences/appearance"
rm -f "$jar"
EOF
bash check-login.sh

A redirect away from /auth/sign_in and a 200 on the preferences page mean the session works. Open https://social.example.com, sign in (cat ~/mastodon/owner.txt), change the password, enable two-factor authentication and delete owner.txt. If you change the password, the check script above will no longer sign in.

Mail

Until SMTP is configured, treat this as a server for accounts you create with tootctl. Resetting a password also goes through the command line: sudo docker compose exec web bin/tootctl accounts modify alice --reset-password.

To send mail you need a relay from an email provider, and DNS records (SPF, DKIM) for your sending domain as that provider describes. Many VPS hosts block outbound port 25, so use the relay's submission port. Add to .env.production:

SMTP_SERVER=smtp.your-provider.example
SMTP_PORT=587
SMTP_LOGIN=your-login
SMTP_PASSWORD=your-password
SMTP_FROM_ADDRESS=notifications@social.example.com

Then run sudo docker compose up -d. This guide does not test mail delivery.

Backups

Following the Mastodon documentation, back up the PostgreSQL database, .env.production, uploaded files in public/system, and optionally Redis. The secrets and your users' data are in these files, so they are owner-readable only:

cd ~/mastodon
( umask 077
  sudo docker compose exec -T db pg_dump -U postgres -Fc mastodon_production > ~/mastodon-db-$(date +%F).dump
  sudo docker compose exec -T redis redis-cli save
  sudo tar czf - public/system redis > ~/mastodon-files-$(date +%F).tar.gz
  tar czf ~/mastodon-config-$(date +%F).tar.gz .env .env.production docker-compose.yml docker-compose.override.yml Caddyfile )
ls -l ~/mastodon-db-*.dump ~/mastodon-files-*.tar.gz ~/mastodon-config-*.tar.gz

Copy all three off the server.

Restore

This deletes the database, Redis data and media and rebuilds them from the backup. Rehearse it right after backing up, before the server has real users:

cd ~/mastodon
test -s ~/mastodon-db-$(date +%F).dump && test -s ~/mastodon-files-$(date +%F).tar.gz &&
  sudo docker compose down && sudo rm -rf postgres14 redis public/system
sudo tar xzf ~/mastodon-files-$(date +%F).tar.gz
sudo docker compose up -d db
for i in $(seq 1 40); do
  sudo docker compose exec -T db pg_isready -h 127.0.0.1 -U postgres >/dev/null 2>&1 && break
  sleep 2
done
sudo docker compose exec -T db createdb -U postgres mastodon_production
sudo docker compose exec -T db pg_restore -U postgres -d mastodon_production --no-owner < ~/mastodon-db-$(date +%F).dump

Start the stack, rebuild home feeds and sign in again:

cd ~/mastodon
sudo docker compose up -d
for i in $(seq 1 60); do
  curl -s -H 'X-Forwarded-Proto: https' http://127.0.0.1:3000/health | grep -q OK && break
  sleep 3
done
sudo docker compose exec -T web bin/tootctl feeds build
sleep 5
bash check-login.sh

On a new server, point the same domain at it, unpack the config archive into ~/mastodon first, then run the commands above.

Upgrades

Read the release notes for every version you are skipping, since some need extra steps. Take a backup, change the three Mastodon image tags in docker-compose.yml (web, streaming, sidekiq), then pull and run the migrations around the restart:

cd ~/mastodon
sudo docker compose pull
sudo docker compose run --rm -T -e SKIP_POST_DEPLOYMENT_MIGRATIONS=true web bundle exec rails db:migrate
sudo docker compose up -d
sudo docker compose run --rm -T web bundle exec rails db:migrate
sudo docker compose ps

Keep the PostgreSQL major version; changing it is a dump-and-restore job.

Media cleanup

Cached media from other servers is the part that grows. Remove old copies on a schedule:

cd ~/mastodon
sudo docker compose exec -T web bin/tootctl media remove --days 7

Troubleshooting

502 from Caddy. Check sudo docker compose ps and sudo docker compose logs web. The web service takes a while to boot.

403 or "Blocked host". The address you use must equal LOCAL_DOMAIN.

The live timeline does not update. Check the streaming service and that Caddy sends /api/v1/streaming to port 4000.

Uploads fail. public/system must be owned by UID 991.

No certificate. Check the DNS record and that inbound 80 and 443 are open at your provider too.

Verification and next steps

You are done when https://social.example.com loads with a valid certificate, the owner can sign in and post, you have restored a backup once, and you have decided about mail and registrations. Write your server rules before opening sign-ups. See the Mastodon entry.

Next steps

Common questions

How long does it take to follow "How to Deploy Mastodon on a VPS"?

We estimate about 60 minutes of hands-on time for "How to Deploy Mastodon on a VPS"; this is an editorial estimate, not a measurement.

Was the "How to Deploy Mastodon on a VPS" guide tested?

The "How to Deploy Mastodon on a VPS" guide was verified end-to-end on Ubuntu 26.04 (Nov 2026).

Analytics cookies only (Google Analytics, PostHog). No ad networks, no cross-site tracking. See our privacy policy.