How to Install Mastodon on a VPS with Docker: Your Own Federated Social Network
One morning your social account is limited without explanation. The timeline was full yesterday; today you cannot reply to anyone because an algorithm decided your account looks "suspicious". You file an appeal and get a template answer back. That is when it sinks in: the account you tended for years was never really yours — it lives on someone else's server, under rules that can change overnight without asking you.
Mastodon flips that around. It is an open-source social network server built on ActivityPub: you hold the domain, you write the rules, and your account lives on your server. People on other servers can still follow and reply to you, because Mastodon servers connect to each other in the same federated network. No ads, no algorithm-shuffled timeline — posts arrive in chronological order, the way social media worked before it became an advertising machine.
This guide installs Mastodon on a VPS using the project's own official Docker Compose file: Postgres for the database, Redis for queues, plus the web, streaming, and Sidekiq services. To be upfront: Mastodon is not the lightest install. There is an interactive setup wizard that asks a lot of questions, and a few configuration values — domain, SMTP, media storage — are decisions you have to make yourself before starting. This guide follows the official documentation and the project's official Compose file step by step, including the parts that genuinely need interactive answers.
Prerequisites
- A Linux VPS with Docker and Docker Compose v2 installed.
- SSH access to the VPS with
sudorights. - A domain (or subdomain) already pointing at the VPS IP, for example
social.example.com. This guide uses that domain as an example; replace it with yours. The domain matters a great deal: once chosen, it becomes your server's identity in the federation and cannot simply be changed later. - An SMTP service or transactional email provider (such as Mailgun, SendGrid, or Amazon SES) for confirmation emails and notifications. The official documentation lists SMTP as a prerequisite — without working email, sign-ups and password resets will not work.
- A VPS with at least 4 GB of RAM and 2 vCPUs for a small instance. Mastodon runs several processes at once (a Rails web app, a Node.js streaming server, Sidekiq, Postgres, Redis), so a 1 GB VPS will struggle.
Check Docker and Compose first:
docker --version
docker compose version
Expected result: both commands print a version number without errors.
Also check Git, since the official Compose file comes from the Mastodon repository:
git --version
If Git is missing, install it with sudo apt update && sudo apt install -y git on Ubuntu/Debian.
Step 1: fetch the official files from the Mastodon repository
Create a working folder and clone the official repository:
sudo mkdir -p /opt/mastodon
cd /opt/mastodon
sudo git clone https://github.com/mastodon/mastodon.git .
sudo git checkout v4.7.5
The last command pins the code to stable release v4.7.5 (the latest stable release at the time of writing, November 2026). Do not use the main branch for production — it holds development code that may not be stable.
Check what came with the clone:
ls -la /opt/mastodon | grep -E "docker-compose|env.production|dist"
Expected result: docker-compose.yml, .env.production.sample, and a dist folder containing Nginx and systemd templates. That docker-compose.yml is the one to use as-is — it is designed for production server deployment, not local development.
Step 2: understand the official docker-compose.yml
Open the Compose file and look at its layout before running anything:
sudo nano docker-compose.yml
The five main services defined there:
db— Postgres 14 Alpine, with a./postgres14volume andPOSTGRES_HOST_AUTH_METHOD=trust. The database only joins Docker's internal network and is not exposed to the host.redis— Redis 7 Alpine, volume./redis:/data, also internal-only.web— imageghcr.io/mastodon/mastodon:v4.7.5, commandbundle exec puma -C config/puma.rb, listening on127.0.0.1:3000. This serves the web pages and the API.streaming— imageghcr.io/mastodon/mastodon-streaming:v4.7.5, commandnode ./streaming/index.js, on127.0.0.1:4000. This pushes timeline updates in real time.sidekiq— the same image asweb, commandbundle exec sidekiq. The background worker that sends email, processes federation queues, and runs scheduled jobs.
All three Mastodon services read their configuration from a single file: .env.production. There is also an es (Elasticsearch) service in the file, but it is commented out — full-text search is optional and memory-hungry, so leave it off for a first install.
Close the editor without changing anything for now. The one thing to watch: the v4.7.5 image tags must match the tag you checked out in Step 1. If you checked out a different version, adjust the three image: ghcr.io/mastodon/mastodon... lines in this file.
Step 3: create an empty .env.production file
Mastodon's setup wizard needs .env.production to already exist before it runs. Create the empty file:
sudo touch .env.production
The wizard will fill this file with every configuration value: domain, database credentials, secret keys, and SMTP settings. Do not fill it in manually from examples found online — the wizard generates the values, including random secret keys.
Step 4: start Postgres and Redis first
The setup wizard creates the database schema, so both supporting services must be running before the wizard is called:
sudo docker compose up -d db redis
sudo docker compose ps
Expected result: the db and redis containers show as Up, with healthchecks turning healthy after a few seconds. If they still say starting, wait a moment and repeat ps.
Check both directly:
sudo docker compose exec db pg_isready -U postgres
sudo docker compose exec redis redis-cli ping
Expected result: accepting connections for Postgres and PONG for Redis. These two commands mirror the healthchecks written in the official Compose file, so if both pass, the wizard will not fail halfway through because the database was not ready.
Step 5: run the interactive setup wizard
This is the longest step — and it genuinely cannot be fully automated. Run:
sudo docker compose run --rm web bundle exec rake mastodon:setup
The wizard asks a series of questions. Your answers are written to .env.production. The questions, following the official documentation:
- Domain name — enter your domain, for example
social.example.com. Again: this value becomes your server's identity in the federation and cannot be safely changed afterwards. - Single user mode — answer
nunless you really want a one-person instance. This mode disables sign-ups and redirects the front page to your profile. - PostgreSQL configuration — the wizard offers defaults that match the official Compose file: host
db, userpostgres, databasepostgres, port5432. Accept these defaults. - Redis configuration — accept the defaults: host
redis, port6379. - S3 / object storage — answer
nfor a first install. Media (uploaded images and videos) will be stored in the local./public/systemfolder. S3 only becomes relevant if you plan to store media outside the VPS. - SMTP configuration — enter your SMTP server, port, login, and sender address. Fill this in correctly: sign-up confirmations and notifications go through here. If you do not have SMTP yet, stop the wizard here, set up an SMTP service first, then run the wizard again.
- Admin user — the wizard offers to create an admin account right away. Answer
y, enter a username and email address; a random password is generated and shown in the terminal. Write that password down — it appears on screen only once.
Once all questions are answered, the wizard writes .env.production, then precompiles assets and creates the database schema. This takes a few minutes on the first run. When the wizard finishes without errors, check the configuration file:
sudo grep -E "^(LOCAL_DOMAIN|DB_HOST|REDIS_HOST|S3_ENABLED)" .env.production
Expected result: LOCAL_DOMAIN=social.example.com (your domain), DB_HOST=db, REDIS_HOST=redis, and S3_ENABLED=false.
One important note about the secret keys: the wizard generates SECRET_KEY_BASE, OTP_SECRET, and VAPID keys for push notifications. These live in .env.production — never share them, and never change them once the instance is running. Changing SECRET_KEY_BASE logs out every active browser session.
Step 6: start the whole stack
Now bring up every service:
sudo docker compose up -d
sudo docker compose ps
Expected result: five containers — db, redis, web, streaming, sidekiq — show as Up and healthy after a minute or two. The web service takes longer on its first start because it loads the freshly compiled assets.
Test from inside the VPS that the web and streaming services respond:
curl -s http://127.0.0.1:3000/health
echo
curl -s http://127.0.0.1:4000/api/v1/streaming/health
echo
Expected result: both commands print OK. These are the same endpoints the official Compose file uses for its healthchecks — if both answer OK, Mastodon is alive; only the reverse proxy remains.
If a container keeps restarting, look at its logs before guessing:
sudo docker compose logs --tail=50 web
Step 7: set up Nginx and HTTPS
Mastodon should not be exposed directly on port 3000 — it belongs behind a reverse proxy with HTTPS. The Mastodon repository already ships an Nginx template in dist/nginx.conf, and the official documentation uses this template. Copy and enable it:
sudo cp /opt/mastodon/dist/nginx.conf /etc/nginx/sites-available/mastodon
sudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon
sudo rm -f /etc/nginx/sites-enabled/default
Open the file and replace every occurrence of example.com with your domain:
sudo nano /etc/nginx/sites-available/mastodon
This template already contains two things that are easy to miss when writing an Nginx configuration from scratch: a proxy to 127.0.0.1:3000 for the web and API, and a dedicated /api/v1/streaming location proxied to 127.0.0.1:4000 for real-time connections. Do not delete the streaming block — without it, timelines will not update on their own and you will have to refresh manually.
Issue a Let's Encrypt certificate, then enable the SSL lines in the template as the official documentation describes:
sudo certbot certonly --nginx -d social.example.com
Once the certificate exists at /etc/letsencrypt/live/social.example.com/, open the Nginx file again and uncomment these two lines (using your own domain):
ssl_certificate /etc/letsencrypt/live/social.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/social.example.com/privkey.pem;
Test the configuration and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
Expected result: nginx -t answers test is successful. Now open https://social.example.com in a browser — your Mastodon front page should appear.
Step 8: create the admin account and fill in server information
If you already created an admin through the wizard in Step 5, skip account creation and log in with the password you wrote down. If not, create one with tootctl — the official command from the "Setting up your new instance" documentation:
sudo docker compose run --rm web bin/tootctl accounts create myname --email you@example.com --confirmed --role Owner
Replace myname and the email address with yours. This command prints a random password in the terminal — write it down, then approve the account:
sudo docker compose run --rm web bin/tootctl accounts modify myname --approve
Note that the role used is Owner, not admin — since Mastodon 4.0, roles use a customizable system, and the highest built-in role is called Owner. Browser sign-ups are disabled by default, so the first account genuinely has to be created from the command line like this.
After logging in, fill in the server information under Preferences → Administration → Site settings: contact name, business email, instance description, and rules of conduct. The official documentation says filling this in is not technically required, but it matters socially — people considering joining your server will read this page first before signing up.
Finally, decide on sign-ups: keep them closed (only you and people you invite), or open them with manual approval. For a personal instance, closed sign-ups are the sensible choice — you can open them later from the admin panel at any time.
Step 9: set up backups for data and configuration
Everything Mastodon holds is spread across several places, and all of it needs backing up:
./postgres14— the entire database: accounts, posts, follow relationships../redis— queues and cache; it can be rebuilt, but backing it up costs nothing../public/system— all media uploaded by users..env.production— holds every secret key. Without this file, a database backup cannot be used in a fresh install.
Back up the database with a Postgres dump, not by copying the folder while the container is running:
cd /opt/mastodon
sudo docker compose exec db pg_dump -U postgres -d postgres -Fc > ~/mastodon-db-backup.dump
sudo tar -czf ~/mastodon-files-backup.tar.gz public/system .env.production
ls -lh ~/mastodon-*.dump ~/mastodon-*.tar.gz
Store both archives outside the VPS. Schedule this to repeat (for example a daily cron job for the database dump and a weekly one for media), and make sure the periodic backup archives genuinely end up outside the VPS; a backup that stays on the same server does not help when that server itself has a problem.
To update Mastodon to a new release, the pattern is: check out the new tag in the repository, adjust the image tags in docker-compose.yml, then pull and restart:
cd /opt/mastodon
sudo git fetch --tags
sudo git checkout v4.7.6
sudo docker compose pull
sudo docker compose up -d
Replace v4.7.6 with the actual release tag. If the release notes mention database migrations, also run sudo docker compose run --rm web bundle exec rake db:migrate before calling the update done. Always read the release notes first — some releases need extra steps.
Troubleshooting
The setup wizard fails with a database connection error. Make sure db and redis are running and healthy before calling the wizard (sudo docker compose ps, then pg_isready and redis-cli ping as in Step 4). The most common cause: the wizard was run before Postgres finished its first start, which can take a dozen seconds.
curl http://127.0.0.1:3000/health does not answer OK. Check the web logs: sudo docker compose logs --tail=80 web. If the logs mention a missing environment variable, .env.production was probably never filled in by the wizard — run mastodon:setup again. If the logs mention assets that were not compiled, the wizard may have stopped midway; running the same wizard again is safe.
The browser shows an Nginx 502 error. Nginx is running but cannot reach the web service at 127.0.0.1:3000. Check that the web container is alive (sudo docker compose ps) and the port is actually listening (sudo ss -tlnp | grep 3000). Also check that your Nginx file came from the dist/nginx.conf template — that template uses proxy_pass http://127.0.0.1:3000, not a Docker container name.
Confirmation emails never arrive. Check the SMTP settings in .env.production (SMTP_SERVER, SMTP_PORT, SMTP_LOGIN, SMTP_FROM_ADDRESS). Test delivery from inside the web container with sudo docker compose exec web bin/tootctl email test you@example.com — this sends a test email to the address you give. If your SMTP provider blocks port 25, use port 587 with TLS as that provider documents.
The timeline does not update on its own; you have to refresh manually. The /api/v1/streaming location in Nginx is not pointing at 127.0.0.1:4000, or the streaming container is unhealthy. Check curl -s http://127.0.0.1:4000/api/v1/streaming/health (it should print OK) and make sure your Nginx file came from the official template rather than being written from scratch.
tootctl accounts create refuses with a role error. Role names are case-sensitive and must be one of the built-in roles (Owner, Admin, Moderator) or a custom role you already created. For the first account, use --role Owner exactly as written in the documentation.
After an update, the web container restarts in a loop. Read the release notes for the version you moved to — some releases change environment variables or require a migration. Run sudo docker compose run --rm web bundle exec rake db:migrate, then check the web logs. If it still fails, put the image tags back to the previous version in docker-compose.yml and run docker compose up -d — this is why the backup in Step 9 is made before updating.
Summary table
| Component | Function | Port / Path | Notes |
|---|---|---|---|
| web | Mastodon app (Rails + Puma) | 127.0.0.1:3000 | Image ghcr.io/mastodon/mastodon:v4.7.5 |
| streaming | Real-time timeline updates (Node.js) | 127.0.0.1:4000 | Image ghcr.io/mastodon/mastodon-streaming:v4.7.5 |
| sidekiq | Background worker: email, federation queues | internal | Same image as web, command bundle exec sidekiq |
| db | Postgres 14 | internal | Volume ./postgres14; healthcheck pg_isready |
| redis | Redis 7 (cache + queues) | internal | Volume ./redis:/data; healthcheck redis-cli ping |
| .env.production | All configuration + secret keys | /opt/mastodon/.env.production | Generated by the mastodon:setup wizard; never share it |
| Nginx on host | Reverse proxy + HTTPS | 80 / 443 | Template from dist/nginx.conf in the repo |
| public/system | Uploaded user media | /opt/mastodon/public/system | Included in backups; S3 optional |
Check before you finish
- ☐ All five containers (
db,redis,web,streaming,sidekiq) areUpand healthy. - ☐
curl http://127.0.0.1:3000/healthand the streaming health endpoint both answerOK. - ☐
https://social.example.com(replace with your domain) opens with a valid certificate. - ☐ The admin account logs in; the random password from the wizard or
tootctlhas been replaced with one you remember. - ☐ A test email from
tootctl email testreaches the inbox. - ☐ Server information (description, contact, rules) is filled in under Site settings.
- ☐ The first backup (Postgres dump plus an archive of
public/systemand.env.production) is made and stored outside the VPS.
A first federated server does demand more patience than a single-container app — the wizard is long, there are five services, and SMTP has to be right. But once everything is green, what you have is not just an account on someone else's platform: you have your own address in the fediverse, with rules you wrote yourself. After a week of running your own instance, what feels most different — the chronological timeline with no ads, or full control over who can join? Tell us in the comments, and if any step above does not work on your VPS, paste the error message so it can be discussed together.
Read also: the Rnevio guide on installing PeerTube on a VPS with Docker for self-hosted video uses the same federation pattern for video, while the companion guide in this batch covers installing Lemmy on a VPS with Docker as a federated link aggregator.


Diskusi
Ada pertanyaan atau pengalaman yang ingin dibagikan?
Komentar ditinjau sebelum ditampilkan. Mari berdiskusi dengan sopan dan sesuai topik.
Tulis komentar