Dec 02, 2025
Ariffud M. & Valentinas C.
15min Read
Mastodon has become a popular alternative to traditional social media platforms. As a decentralized, open-source social network, Mastadon empowers users to operate their own servers, giving them complete control to customize their social media experience.
In this article, you’ll learn how to set up a Mastodon server on a virtual private server (VPS), from establishing an SMTP server to downloading the Mastodon installer and inviting users to join your instance.
By the end of this guide, you’ll have your own Mastodon server up and running, ready to connect with your community.
To host a Mastodon server, you should prepare a couple of key components.
First and foremost, you require a Linux VPS running on Ubuntu 22.04 or Debian 11. While multiple hosting providers are available, choose a reliable one for optimal server performance.
Hostinger’s Mastodon VPS service is an excellent choice for installing a Mastodon server. With up to 8 CPU cores, 32 GB of RAM, and 400 GB of disk space, it provides powerful hardware to handle Mastodon server’s demands.
Additionally, our Mastodon VPS plans grant you root access so you can customize your own server to fit your specific needs. With just a few clicks, you can install various Linux distributions, including different versions of Ubuntu and Debian.
This service implements advanced security measures to protect your server, including a Monarx-powered malware scanner to prevent malicious attacks and firewall and DDoS protection to safeguard your hosted sites against suspicious traffic.
Our Mastodon hosting is also scalable. We suggest getting the KVM 2 plan, which includes a dual-core CPU, 8 GB of RAM, and 100 GB of storage. You can then scale up as your Mastodon server grows and needs more resources.

In addition to a VPS, you’ll need a domain name so people can easily find and join your Mastodon instance. We recommend getting one from the same provider so you can manage your VPS and domain in one place.
If you purchase a domain from Hostinger, follow our tutorial on pointing it to your VPS. After that, ensure the DNS records propagate worldwide, which you can verify using an online tool like DNS Checker.
Instantly check domain name availability.
With all prerequisites in place, you can proceed with the Mastodon instance setup. For this tutorial, we’ll use a VPS running Ubuntu 22.04.
A relay SMTP server is an essential component for any Mastodon server. It automatically sends emails, such as confirmation links, to your users. We’ll set up an SMTP server using Brevo. It offers a free plan to send up to 300 emails daily.
Here are the steps:



apt install postfix libsasl2-modules

Then, enter your domain name in the dedicated field to set the system mail name.

nano /etc/postfix/main.cf
Find the relayhost = line and replace it with the following:
# outbound relay configurations smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_security_level = may header_size_limit = 4096000
Save the file and close nano by pressing Ctrl + X → Y → Enter.
nano /etc/postfix/sasl_passwd
Add the SMTP relay host, username, and password as below. Replace smtp_username and smtp_password with your details:
[smtp-relay.brevo.com]:587 smtp_username:smtp_password
postmap /etc/postfix/sasl_passwd systemctl restart postfix chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
apt install bsd-mailx echo "This is a test email." | mailx -r from-address -s hello to-address
Check your recipient’s inbox to see if the email was indeed sent.
You need to set up strong security measures to safeguard your Mastodon server from unauthorized access and maintain its integrity. Here’s how:
ssh-keygen -t rsa -b 4096
After generating the keys, copy the public key to your server. Replace username and your_server_ip with your credentials:
ssh-copy-id username@your_server_ip
Hostinger VPS customers can use Kodee, our AI-powered assistant, to verify if public keys have been added. Simply ask, “What are my public SSH keys on my VPS?” If Kodee returns keys, you’ve succeeded. Moreoever, you can also ask it to add or remove SSH keys from your server via chat, all without commands.
apt update && apt upgrade -y
apt install fail2ban
Configure it by creating a local configuration file:
nano /etc/fail2ban/jail.local
Then, add these lines inside the file:
[DEFAULT] destemail = your@email.here sendername = Fail2Ban [sshd] enabled = true port = 22 mode = aggressive
Restart Fail2ban to apply the changes:
systemctl restart fail2ban
apt install -y iptables-persistent
During the installation, you’ll be asked to save the current IPv4 and IPv6 rules. Select No for both.


nano /etc/iptables/rules.v4
Copy these lines into the file:
*filter # Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0. -A INPUT -i lo -j ACCEPT -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT # Accept all established inbound connections. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow all outbound traffic; you can modify this to only allow certain traffic. -A OUTPUT -j ACCEPT # Allow HTTP and HTTPS connections from anywhere, including standard ports for websites and SSL. -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT # Allow HTTP/3 connections from anywhere (optional). -A INPUT -p udp --dport 443 -j ACCEPT # Allow SSH connections. # The -dport number should be the same port number you set in sshd_config. -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT # Allow ping. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT # Allow destination unreachable messages, especially code 4 (fragmentation required) is required or PMTUD breaks. -A INPUT -p icmp -m icmp --icmp-type 3 -j ACCEPT # Log iptables denied calls. -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 # Reject all other inbound; default deny unless explicitly allowed policy. -A INPUT -j REJECT -A FORWARD -j REJECT COMMIT
nano /etc/iptables/rules.v6
Then, add the following lines:
*filter # Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0. -A INPUT -i lo -j ACCEPT -A INPUT ! -i lo -d ::1/128 -j REJECT # Accept all established inbound connections. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow all outbound traffic; you can modify this to only allow certain traffic. -A OUTPUT -j ACCEPT # Allow HTTP and HTTPS connections from anywhere, including the standard ports for websites and SSL. -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT # Allow HTTP/3 connections from anywhere (optional). -A INPUT -p udp --dport 443 -j ACCEPT # Allow SSH connections. # The -dport number should be the same port number you set in sshd_config. -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT # Allow ping. -A INPUT -p icmpv6 -j ACCEPT # Log iptables denied calls. -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 # Reject all other inbound; default deny unless explicitly allowed policy. -A INPUT -j REJECT -A FORWARD -j REJECT COMMIT
iptables-restore < /etc/iptables/rules.v4 ip6tables-restore < /etc/iptables/rules.v6
Next, set up the Mastodon server environment. This involves configuring system repositories and packages, creating a dedicated user, and installing necessary software.
Here’s the guide:
apt install -y curl wget gnupg apt-transport-https lsb-release ca-certificates
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
wget -O /usr/share/keyrings/postgresql.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc echo "deb [signed-by=/usr/share/keyrings/postgresql.asc] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main"> /etc/apt/sources.list.d/postgresql.list
apt update apt install -y imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core g++ libprotobuf-dev protobuf-compiler pkg-config gcc autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev nginx nodejs redis-server redis-tools postgresql postgresql-contrib certbot python3-certbot-nginx libidn11-dev libicu-dev libjemalloc-dev
corepack enable yarn set version classic
adduser --disabled-login mastodon
Switch to the mastodon user:
su - mastodon
git clone https://github.com/rbenv/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc exec bash git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
Once done, install Ruby and set it as the global version:
RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.2.3 rbenv global 3.2.3
gem install bundler --no-document
A crucial part of hosting a dedicated Mastodon server is creating a PostgreSQL database, which stores your instance’s essential data. Follow the instructions below:
exit
sudo -u postgres -i psql
CREATE DATABASE mastodon;
CREATE USER mastodon; ALTER USER mastodon WITH ENCRYPTED PASSWORD 'your_preferred_password';
ALTER USER mastodon createdb; ALTER DATABASE mastodon OWNER TO mastodon;
q
It’s time to download the installer, set up necessary Mastodon dependencies, and run the setup wizard to configure your instance. Here are the steps to complete the Mastodon installation:
su - mastodon
git clone https://github.com/mastodon/mastodon.git live && cd live git checkout $(git tag -l | grep '^v[0-9.]*$' | sort -V | tail -n 1)
bundle config deployment 'true' bundle config without 'development test' bundle install -j$(getconf _NPROCESSORS_ONLN) yarn install --pure-lockfile
RAILS_ENV=production bundle exec rake mastodon:setup
The wizard will ask you a series of questions:
Once done, you’ll be given a generated admin password. Store this password securely to log in as the server owner later.
After installing Mastodon on your VPS, you need to configure the NGINX web server, issue an SSL certificate for secure connections, and set up systemd services to manage the Mastodon processes. Here are the steps:
exit
certbot certonly --nginx -d example.com
Follow the on-screen instructions to configure SSL automatically.
cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon rm /etc/nginx/sites-enabled/default
nano /etc/nginx/sites-available/mastodon
Uncomment the ssl_certificate and ssl_certificate_key lines. Then, find every instance of example.com with Ctrl + W and replace them with your domain name.

Once done, save the file and exit the text editor.
systemctl reload nginx
cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
systemctl daemon-reload systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming
Check their statuses:
systemctl status --now mastodon-web mastodon-sidekiq mastodon-streaming
You should see an output indicating that all services are active and running without issues:

chown mastodon:www-data /home/mastodon/ chmod 0710 -v /home/mastodon/
You can now open your new Mastodon server and customize it to your needs, ensuring your instance can host users and manage content. Here are the instructions:



To create these accounts, go back to your terminal and access the Mastodon root directory:
su - mastodon cd /home/mastodon/live
Then, run the following commands to create a new Mastodon account:
RAILS_ENV=production bin/tootctl accounts create alice --email alice@example.com --confirmed --role Owner
Replace alice, alice@example.com, and Owner with the new account’s desired username, email, and role. Here are brief explanations of each role:
Once your Mastodon server is set up and running, you can invite users to join your instance. This process involves generating invite links, allowing users to sign up via said links, and moderating user activities.
Generate invite links
You can quickly generate invite links via the Mastodon admin dashboard by following these steps:

Register a user account
Users will be directed to the signup page when they click the invite link. After entering a username, password, and email address, your server will automatically send a verification email so users can complete their registration.

Moderate user accounts
Mastodon provides tools to manage user interactions, address inappropriate behavior, and enforce community guidelines. Here are some tips to effectively moderate your community:

Installing additional features like Elasticsearch and hCaptcha on your Mastodon instance can significantly improve user experience and security. Here’s how to configure these features.
Install Elasticsearch
Mastodon supports full-text search with Elasticsearch so that users can find public statuses, their posts, mentions, favorites, bookmarks, and account details.
apt install openjdk-17-jre-headless
wget -O /usr/share/keyrings/elasticsearch.asc https://artifacts.elastic.co/GPG-KEY-elasticsearch echo "deb [signed-by=/usr/share/keyrings/elasticsearch.asc] https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list
apt update apt install elasticsearch
systemctl daemon-reload systemctl enable --now elasticsearch
nano /home/mastodon/live/.env.production
Include the following variables:
ES_ENABLED=true ES_HOST=localhost ES_PORT=9200 ES_PRESET=single_node_cluster # Use the appropriate preset for your setup: single_node_cluster, small_cluster, or large_cluster # ES_USER= # ES_PASS=
systemctl restart mastodon-sidekiq systemctl reload mastodon-web
su - mastodon cd live RAILS_ENV=production bin/tootctl search deploy
Set up hCaptcha
Mastodon can use CAPTCHA technology to help mitigate bot signups. Currently, this decentralized social media platform supports only hCaptcha.
nano /home/mastodon/live/.env.production
Add the following values, replacing your_site_key and your_secret_key with your hCaptcha keys:
HCAPTCHA_SITE_KEY=your_site_key HCAPTCHA_SECRET_KEY=your_secret_key
systemctl restart mastodon-sidekiq systemctl reload mastodon-web

After finishing the Mastodon server tutorial, we suggest implementing these best practices to ensure optimal instance performance over time.
As the name suggests, the Mastodon admin CLI lets you manage your instance via the command line. Before you start using it, make sure to switch to the mastodon user and navigate to the correct directory:
su - mastodon cd live
Base CLI commands
The base CLI provides general commands to interact with your Mastodon instance:
Accounts CLI commands
The following commands let you manage user accounts and their roles:
Cache CLI commands
Use the cache CLI to manage and clear various caches on your Mastodon server:
Settings CLI commands
The settings CLI allows you to configure server-wide settings and preferences:
You can find complete commands and their explanations in the Mastodon admin CLI documentation.
Maintaining your Mastodon server involves regularly running cleanup tasks to free up space and resources and backing up data to protect them from accidental loss or corruption.
Run periodic cleanup tasks
Mastodon generates temporary files, such as remote media and preview cards, that can accumulate over time. Cleaning these up periodically via a cron job will help improve server performance.
su - mastodon
crontab -e
If this is your first time doing so, you’ll be prompted to select an editor. Choose your preferred one.
At the bottom of the file, add the following lines to schedule the cleanup tasks:
@weekly RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove @weekly RAILS_ENV=production /home/mastodon/live/bin/tootctl preview_cards remove
crontab -l
This will display all scheduled tasks for the Mastodon user.
Automatically back up Mastodon data on Hostinger
Hosting Mastodon on Hostinger’s VPS allows you to easily create backups using our built-in features.
To set up automatic backups, activate the Daily Backups option first, and then configure your preferred backup frequency under Auto-backups settings.

Manually back up Mastodon data
Alternatively, you can manually back up important data, such as application secrets, databases, user-uploaded files, and Redis dumps.
cp /home/mastodon/live/.env.production /path/to/backup/directory
pg_dump -U mastodon mastodon > /path/to/backup/directory/mastodon_backup.sql
cp -r /home/mastodon/live/public/system /path/to/backup/directory
cp /var/lib/redis/dump.rdb /path/to/backup/directory
Upgrading your Mastodon server to the latest version enables you to benefit from new features, performance enhancements, and security updates.
To view the latest version available, visit the Mastodon GitHub releases page. Review the changelog and specific upgrade instructions for the release you plan to install.
After that, check the currently installed version with these commands:
su - mastodon cd /home/mastodon/live git describe --tags
You should see your current Mastodon installation’s version tag:
v4.2.9
Now, you can start upgrading your server version with these steps:
git fetch --tags
git checkout v4.2.10
Follow the specific instructions provided in the release notes when upgrading your instance.
exit
systemctl restart mastodon-sidekiq systemctl reload mastodon-web systemctl restart mastodon-streaming
In this article, you’ve learned how to make a Mastodon server, from preparing the environment to installing the server and additional features. You’ve also explored several tips to optimize your instance’s performance, such as cleaning up junk files and backing up necessary data.
Establishing a dedicated server for Mastodon provides an enjoyable social media experience with complete control over your instance. Be sure to update your server whenever new versions are released to maintain a reliable and engaging platform for your community.
Running your own Mastodon server allows you to control content and moderation, ensure privacy, and create a customizable social media experience for your community without the limitations of centralized platforms.
The cost to host a Mastodon server varies. For example, a VPS can cost $5 to $20/month, while a domain name can range from $1 to $10/year. There may be additional expenses for using third-party services to enhance functionalities.
A Mastodon server requires a Linux VPS or dedicated server with at least 2 CPU cores, 3 GB RAM, and 25 GB of disk storage. The server should use Ubuntu 22.04 or Debian 11 and have software like PostgreSQL, Redis, Ruby, and NGINX installed.
You can generate income from a Mastodon server through membership fees, donations, or sponsorships. Carefully plan and engage with the community to ensure sustainability without compromising user experience.