Complete Guide to Setting Up Your Own Mastodon Server
In the age of decentralized social media, Mastodon stands out as a popular, open-source platform. This guide will walk you through the process of setting up your own Mastodon server, giving you full control over your social media experience.
Prerequisites
Before we begin, ensure you have:
- A server running Ubuntu 20.04 LTS or newer
- Root or sudo access to your server
- A domain name pointed to your server’s IP address
- Basic knowledge of command-line operations
Need a reliable server to host your Mastodon instance? Check out Servers Guru for high-performance servers at competitive prices.
Step 1: Update Your System
First, let’s ensure your system is up-to-date:
sudo apt update
sudo apt upgrade -y
Step 2: Install Required Dependencies
Mastodon requires several packages to run properly:
sudo apt install -y curl wget gnupg apt-transport-https lsb-release ca-certificates
Step 3: Install Node.js
Mastodon requires Node.js. Let’s install it:
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
Step 4: Install PostgreSQL
Mastodon uses PostgreSQL as its database:
sudo apt install -y postgresql postgresql-contrib
Step 5: Install Redis
Redis is used for caching and sidekiq:
sudo apt install -y redis-server
Step 6: Install Ruby
Mastodon is built with Ruby on Rails. Install Ruby using rbenv:
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.0.3
rbenv global 3.0.3
Step 7: Create a Mastodon User
Create a dedicated user for Mastodon:
sudo adduser --disabled-login mastodon
Step 8: Install Mastodon
Switch to the Mastodon user and clone the Mastodon repository:
sudo su - mastodon
git clone https://github.com/tootsuite/mastodon.git live
cd live
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)
Step 9: Install Mastodon Dependencies
Install the required gems and node packages:
bundle config deployment 'true'
bundle config without 'development test'
bundle install -j$(getconf _NPROCESSORS_ONLN)
yarn install --pure-lockfile
Step 10: Configure Mastodon
Generate a configuration file:
RAILS_ENV=production bundle exec rake mastodon:setup
Follow the prompts to set up your Mastodon instance.
Step 11: Set Up Nginx
Install Nginx:
sudo apt install -y nginx
Create a Nginx configuration file for Mastodon:
sudo nano /etc/nginx/sites-available/mastodon
Add the following configuration (replace example.com
with your domain):
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
server_name example.com;
root /home/mastodon/live/public;
# the rest of the Nginx configuration...
}
Enable the site:
sudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/
Step 12: Set Up SSL with Let’s Encrypt
Install Certbot:
sudo apt install -y certbot python3-certbot-nginx
Obtain an SSL certificate:
sudo certbot --nginx -d example.com
Step 13: Start Mastodon Services
Create systemd service files for Mastodon’s components (web, sidekiq, streaming).
Step 14: Final Steps
Restart Nginx and start Mastodon services:
sudo systemctl restart nginx
sudo systemctl start mastodon-web mastodon-sidekiq mastodon-streaming
Congratulations! Your Mastodon server should now be up and running.
Optimizing Your Mastodon Server
For the best performance:
-
Allocate Sufficient Resources: Ensure your server has enough CPU and RAM. Servers Guru offers a range of high-performance options suitable for Mastodon hosting.
-
Regular Backups: Implement a robust backup strategy for your Mastodon data and database.
-
Monitor Performance: Use tools like Prometheus and Grafana to monitor your server’s health.
-
Content Delivery Network (CDN): Consider using a CDN to serve static assets and improve global performance.
Remember, running a Mastodon server requires ongoing maintenance and updates. Stay informed about the latest Mastodon releases and security patches.
By hosting your own Mastodon instance, you’re contributing to a decentralized social media landscape. Enjoy your new, self-hosted social media platform!