The fediverse is buzzing with activity, and Mastodon has emerged as one of the most popular decentralized social networking platforms. Unlike centralized platforms like Twitter or Facebook, Mastodon allows you to take control of your social media experience by hosting your own instance. Imagine running your own version of mastodon.social—customized to your liking, with full control over your data and community. Sounds exciting, right?
In this guide, we’ll walk you through the process of setting up your own Mastodon server on a VPS (Virtual Private Server). Whether you’re a hobbyist, a privacy enthusiast, or a community builder, hosting your own Mastodon instance is a rewarding project. Let’s dive in!
Why Host Your Own Mastodon Instance?
Before we get into the nitty-gritty, let’s talk about why you might want to host your own Mastodon server:
- Full Control: You’re the admin. You set the rules, moderate the content, and decide how the server operates.
- Privacy: Keep your data—and your users’ data—out of the hands of big tech.
- Customization: Tailor the platform to your community’s needs, from branding to features.
- Decentralization: Join the fediverse, a network of independent servers that communicate with each other.
Prerequisites
To host a Mastodon server, you’ll need:
- A VPS: We recommend a VPS with at least 2GB of RAM, 2 CPU cores, and 20GB of SSD storage for a small instance. For this guide, we’ll assume you’re using
- (because, why not?). Ubuntu 22.04 LTS is a solid choice for the OS.
- A Domain Name: You’ll need a domain (e.g., yourmastodon.com) to point to your server.
- Basic Linux Skills: Familiarity with SSH, command-line tools, and server management is helpful.
- Time: Set aside a couple of hours to complete the setup.
Power Your Projects with vpszen.com VPS Solutions
Looking for reliable hosting to run your Linux servers and host your next big project? VpsZen.com has you covered with top-tier VPS options tailored to your needs.
Choose from ARM64 VPS Servers for energy-efficient performance, or Root VPS Servers for virtual servers with dedicated resources.
Step 1: Set Up Your VPS
1. SSH into your server and update the system:
apt update && apt upgrade -y
2. Set up a Non-Root user.
For security, create a non-root user (e.g., mastodon) with sudo privileges:
adduser mastodon
usermod -aG sudo mastodon
Log out and reconnect as the new user.
Step 2: Install Dependencies
Mastodon requires several software packages to run. Let’s install them step by step.
1. Install System Packages
Run the following command to install essential tools and libraries:
sudo apt install -y curl wget git build-essential libssl-dev libreadline-dev zlib1g-dev libpq-dev libyaml-dev postgresql postgresql-contrib redis-server redis-tools imagemagick ffmpeg libidn11-dev libicu-dev libprotobuf-dev protobuf-compiler
2. Install Node.js
Mastodon’s web interface relies on Node.js. Install it using the NodeSource repository:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
3. Install Ruby
Mastodon is built with Ruby. Use rbenv to manage Ruby versions:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv install 3.2.2
rbenv global 3.2.2
4. Install Yarn
Add Yarn for JavaScript dependencies:
npm install -g yarn

Step 3: Configure PostgreSQL
Mastodon uses PostgreSQL as its database. Let’s set it up.
1. Create a Database user:
sudo -u postgres psql
Inside the PostgreSQL prompt, create a user and database:
CREATE USER mastodon WITH PASSWORD 'your_secure_password';
CREATE DATABASE mastodon_production OWNER mastodon;
ALTER USER mastodon WITH ENCRYPTED PASSWORD;
\q
2. Test the Connection
Verify the database works:
psql -U mastodon -d mastodon_production -W
Enter the password you set, and you should connect successfully.
Step 4: Install Mastodon
Now it’s time to download and configure Mastodon.
1. Clone the Mastodon Repository
Move to the home directory and clone the latest stable release (check the Mastodon GitHub for the current version):
cd ~
git clone https://github.com/mastodon/mastodon.git
cd mastodon
git checkout v4.2.7 # Replace with the latest stable version
2. Install Ruby Dependencies
Install the required gems:
gem install bundler
bundle install
3. Install JavaScript Dependencies
Install Yarn packages:
yarn install
Step 5: Configure Mastodon
Mastodon needs a configuration file to know how to run.
1. Run the Setup Wizard
Start the interactive setup:
RAILS_ENV=production bundle exec rake mastodon:setup
Follow the prompts:
- Domain: Enter your domain (e.g., yourmastodon.com).
- Database: Use localhost, mastodon_production, mastodon, and the password you set.
- Redis: Use defaults (localhost, port 6379).
- SMTP: Enter your email server details (e.g., Gmail SMTP or a service like SendGrid).
This generates a .env.production file in the ~/mastodon directory.
2. Precompile Assets
Compile the static files:
RAILS_ENV=production bundle exec rake assets:precompile
Step 6: Set Up Nginx and SSL
Your server needs a web server (Nginx) and SSL (via Let’s Encrypt) to serve Mastodon securely.
1. Install Nginx
sudo apt install -y nginx
2. Configure Nginx
Create a new Nginx config file:
sudo nano /etc/nginx/sites-available/mastodon
Add the following (replace yourmastodon.com with your domain):
server {
listen 80;
server_name yourmastodon.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Enable the config:
sudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled
sudo nginx -t && sudo systemctl restart nginx
3. Install Certbot for SSL
Secure your server with HTTPS:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourmastodon.com
Follow the prompts to configure SSL. Certbot will update your Nginx config automatically.
Step 7: Run Mastodon Services
Mastodon runs three main processes: the web server, streaming server, and Sidekiq (for background jobs). Set them up as systemd services.
1. Create Service Files
Web Service:
sudo nano /etc/systemd/system/mastodon-web.service
Add
[Unit]
Description=Mastodon Web
After=network.target
[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/mastodon
Environment="RAILS_ENV=production"
ExecStart=/home/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb
Restart=always
[Install]
WantedBy=multi-user.target
Streaming service:
sudo nano /etc/systemd/system/mastodon-streaming.service
Add:
[Unit]
Description=Mastodon Streaming
After=network.target
[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/mastodon
Environment="RAILS_ENV=production"
ExecStart=/usr/bin/npm run start
Restart=always
[Install]
WantedBy=multi-user.target
Sidekiq service:
sudo nano /etc/systemd/system/mastodon-sidekiq.service
Add:
[Unit]
Description=Mastodon Sidekiq
After=network.target
[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/mastodon
Environment="RAILS_ENV=production"
ExecStart=/home/mastodon/.rbenv/shims/bundle exec sidekiq
Restart=always
[Install]
WantedBy=multi-user.target
2. Enable and start services
sudo systemctl daemon-reload
sudo systemctl enable mastodon-web mastodon-streaming mastodon-sidekiq
sudo systemctl start mastodon-web mastodon-streaming mastodon-sidekiq
Step 8: Test Your Instance
Open your browser and navigate to https://yourmastodon.com. If everything’s set up correctly, you’ll see the Mastodon welcome page. Sign up as the first user—you’re now the admin!
Step 9: Fine-Tune and Maintain
Add an Admin User: Run this to grant admin privileges:
cd ~/mastodon
RAILS_ENV=production bundle exec rails mastodon:make_admin USERNAME=yourusername
- Backups: Regularly back up your PostgreSQL database and /home/mastodon/mastodon/public/system directory.
- Updates: Keep Mastodon updated by pulling the latest code from GitHub and re-running the setup steps.
Conclusion
Congratulations! You’ve just set up your own Mastodon server on a VPSZen VPS. You’re now part of the fediverse, with a fully functional, self-hosted social network. Whether you’re building a niche community or experimenting with decentralized tech, this is just the beginning.
Got questions or need help? Drop a comment below or reach out to the VPSZen support team. Happy tooting!