Self-hosting a Mastodon instance gives you full control over your social media data, moderation policies, and federation settings. Instead of relying on a public instance run by someone else, you own the server and the content it hosts. This article explains how to deploy a Mastodon instance on a Virtual Private Server (VPS) running Ubuntu 22.04. You will learn the exact steps to install, configure, and secure your own Mastodon server.
Key Takeaways: Self-Hosting Mastodon on a VPS
- Ubuntu 22.04 LTS with Docker Compose: The fastest and most reliable method to deploy Mastodon on a VPS.
- Environment file (.env.production) configuration: Required to set database credentials, secret keys, and domain name for secure operation.
- Nginx reverse proxy with Let’s Encrypt SSL: Ensures all traffic to your instance is encrypted and the server is accessible via your chosen domain.
What You Need Before Starting the Installation
Self-hosting Mastodon requires a VPS with at least 4 GB of RAM and 2 CPU cores. The recommended operating system is Ubuntu 22.04 LTS. You also need a domain name pointed to the VPS IP address. This guide uses Docker Compose to manage Mastodon and its dependencies. Docker Compose simplifies updates and reduces configuration errors. You must have root or sudo access to the VPS.
Steps to Deploy Mastodon on a VPS
Follow these steps in order. Do not skip any step. Each step builds on the previous one.
- Update the server and install prerequisites
Log in to your VPS via SSH. Runsudo apt update && sudo apt upgrade -yto update all packages. Then install curl, git, and Docker usingsudo apt install curl git docker.io docker-compose-plugin -y. Enable Docker to start on boot withsudo systemctl enable docker. - Clone the Mastodon repository
Navigate to your home directory withcd ~. Clone the official Mastodon repository by runninggit clone https://github.com/mastodon/mastodon.git. Change into the Mastodon directory withcd mastodon. - Copy and edit the environment configuration file
Runcp .env.production.sample .env.productionto create the environment file. Open the file with a text editor like nano:nano .env.production. Set the following values:
–LOCAL_DOMAINto your domain name (example:social.yourdomain.com)
–DB_HOSTtodb
–DB_USERtomastodon
–DB_NAMEtomastodon_production
–DB_PASSto a strong random password
–SECRET_KEY_BASE,OTP_SECRET, andVAPID_PRIVATE_KEYandVAPID_PUBLIC_KEYcan be generated later. - Generate secret keys
Rundocker compose run --rm web rake secretthree times to generateSECRET_KEY_BASE,OTP_SECRET, and the VAPID keys. For VAPID keys, rundocker compose run --rm web rake mastodon:webpush:generate_vapid_key. Copy each output into the.env.productionfile under the matching variable names. - Start the Mastodon services
Rundocker compose up -dto start the database, web, sidekiq, and streaming services in the background. Wait two minutes for the database to initialize. - Run the database migration
Executedocker compose run --rm web rails db:migrateto create the database tables. Then rundocker compose run --rm web rails assets:precompileto compile static assets. - Configure Nginx as a reverse proxy
Install Nginx withsudo apt install nginx -y. Copy the sample Nginx configuration from the Mastodon directory:sudo cp ~/mastodon/dist/nginx.conf /etc/nginx/sites-available/mastodon. Edit the file withsudo nano /etc/nginx/sites-available/mastodon. Replaceexample.comwith your domain name. Enable the site by runningsudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/. Then test the configuration withsudo nginx -tand restart Nginx withsudo systemctl restart nginx. - Set up SSL with Let’s Encrypt
Install Certbot withsudo apt install certbot python3-certbot-nginx -y. Runsudo certbot --nginx -d yourdomain.comand follow the prompts to obtain and install a free SSL certificate. Automatic renewal is enabled by default. - Create the admin account
After the services are running, create your admin user by runningdocker compose run --rm web rails mastodon:users:create. Provide an email, username, and password. Confirm the account when prompted. - Verify the instance is live
Open a web browser and navigate tohttps://yourdomain.com. You should see the Mastodon login screen. Log in with the admin credentials you created.
Common Mistakes and How to Avoid Them
Docker Compose Fails with Permission Denied
This error occurs when the current user is not added to the Docker group. Run sudo usermod -aG docker $USER and log out and back in. Then retry the Docker commands.
Mastodon Web Interface Shows a 502 Bad Gateway
The web service may not have started correctly. Check the logs with docker compose logs web. Common causes include missing secret keys in .env.production or a database that has not fully migrated. Re-run the migration step and restart the services with docker compose restart.
Emails Are Not Sent from the Instance
Mastodon requires an SMTP server to send confirmation and notification emails. Add SMTP settings to your .env.production file. For example, if using Mailgun: set SMTP_SERVER=smtp.mailgun.org, SMTP_PORT=587, SMTP_LOGIN=your_login, SMTP_PASSWORD=your_password, and SMTP_AUTH_METHOD=plain. Then restart the services.
Self-Hosted Mastodon vs Managed Mastodon Hosting
| Item | Self-Hosted on VPS | Managed Hosting |
|---|---|---|
| Cost | VPS monthly fee ($10–$50) plus domain | Monthly subscription ($20–$100) |
| Control | Full root access, custom moderation | Limited to provider settings |
| Maintenance | You handle updates, backups, and security | Provider handles maintenance |
| Scalability | Manual upgrade of VPS resources | Automatic scaling options |
| Technical skill required | Linux command line, Docker, Nginx | Minimal technical knowledge |
Self-hosting gives you complete data ownership and the ability to customize your instance. Managed hosting is easier to start with but limits your control. Choose based on your comfort with server administration.
You can now run your own Mastodon instance with full administrative control. After the instance is live, configure your moderation policies and customize the site title and description in Administration > Site Settings. For ongoing maintenance, set up automatic backups of the PostgreSQL database using docker compose exec db pg_dump -U mastodon mastodon_production > backup.sql and schedule it with a cron job.