AlmaLinuxRHEL Based

How To Install Mastodon on AlmaLinux 9

Install Mastodon on AlmaLinux 9

Mastodon, an open-source, decentralized social network, has gained significant popularity as an alternative to traditional social media platforms. As more users seek control over their online presence, self-hosting Mastodon instances has become increasingly attractive. This guide will walk you through the process of installing Mastodon on AlmaLinux 9, a robust and stable Linux distribution perfect for hosting such services.

AlmaLinux 9, known for its reliability and compatibility with enterprise-grade applications, provides an excellent foundation for running a Mastodon server. By following this tutorial, you’ll be able to set up your own Mastodon instance, giving you full control over your social media experience and data.

Prerequisites

Before we begin the installation process, ensure you have the following:

  • A server running AlmaLinux 9 with at least 2GB of RAM and 2 CPU cores
  • Root or sudo access to the server
  • A domain name pointing to your server’s IP address
  • Basic familiarity with Linux command line operations

You’ll also need to have the following software packages installed:

  • Git
  • Curl
  • Wget
  • Gnupg2

Additionally, you should have an SSL certificate for your domain. We’ll cover obtaining a free SSL certificate using Let’s Encrypt later in this guide.

Preparing the Server

Start by updating your AlmaLinux 9 system to ensure you have the latest packages and security updates:

sudo dnf update -y

Next, install the necessary dependencies:

sudo dnf install -y git curl wget gnupg2 make gcc gcc-c++ kernel-devel

Configure the firewall to allow HTTP, HTTPS, and SSH traffic:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

Installing Ruby

Mastodon requires Ruby 3.0.0 or later. We’ll use RVM (Ruby Version Manager) to install and manage Ruby versions:

gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm install 3.0.0
rvm use 3.0.0 --default

Verify the Ruby installation:

ruby --version

Setting Up PostgreSQL

Mastodon uses PostgreSQL as its database. Install and configure it as follows:

sudo dnf install -y postgresql postgresql-server postgresql-contrib
sudo postgresql-setup --initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql

Create a database and user for Mastodon:

sudo -u postgres psql

In the PostgreSQL prompt, run:

CREATE USER mastodon CREATEDB;
\q

Installing Node.js and Yarn

Mastodon requires Node.js and Yarn. Add the Node.js repository and install both:

curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
sudo dnf install -y nodejs
sudo npm install -g yarn

Installing and Configuring Nginx

Nginx will serve as the web server for your Mastodon instance:

sudo dnf install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx

Create a new Nginx configuration file for Mastodon:

sudo nano /etc/nginx/conf.d/mastodon.conf

Add the following configuration, replacing ‘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;
  location /.well-known/acme-challenge/ { allow all; }
  location / { return 301 https://$host$request_uri; }
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name example.com;

  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;

  ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

  root /home/mastodon/live/public;

  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

  location / {
    try_files $uri @proxy;
  }

  location ~ ^/(emoji|packs|system/accounts/avatars|system/media_attachments/files) {
    add_header Cache-Control "public, max-age=31536000, immutable";
    try_files $uri @proxy;
  }

  location /sw.js {
    add_header Cache-Control "public, max-age=0";
    try_files $uri @proxy;
  }

  location @proxy {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Proxy "";
    proxy_pass_header Server;

    proxy_pass http://127.0.0.1:3000;
    proxy_buffering off;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;

    tcp_nodelay on;
  }

  location /api/v1/streaming {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Proxy "";

    proxy_pass http://127.0.0.1:4000;
    proxy_buffering off;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;

    tcp_nodelay on;
  }

  error_page 500 501 502 503 504 /500.html;
}

Save the file and exit the editor. Then, test the Nginx configuration:

sudo nginx -t

If there are no errors, reload Nginx:

sudo systemctl reload nginx

Installing Mastodon

Now, let’s install Mastodon itself:

sudo adduser --disabled-login mastodon
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)
bundle config deployment 'true'
bundle config without 'development test'
bundle install -j$(getconf _NPROCESSORS_ONLN)
yarn install --pure-lockfile

Next, configure Mastodon:

RAILS_ENV=production bundle exec rake mastodon:setup

This interactive setup will guide you through configuring your Mastodon instance. Make sure to save the generated configuration file.

Setting Up Systemd Services

Create systemd service files for Mastodon. First, exit the mastodon user session:

exit

Now, create the service files:

sudo nano /etc/systemd/system/mastodon-web.service

Add the following content:

[Unit]
Description=mastodon-web
After=network.target

[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment="RAILS_ENV=production"
Environment="PORT=3000"
ExecStart=/home/mastodon/.rvm/bin/rvm default do bundle exec puma -C config/puma.rb
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target

Create similar files for mastodon-sidekiq and mastodon-streaming services. Then, enable and start the services:

sudo systemctl enable mastodon-web mastodon-sidekiq mastodon-streaming
sudo systemctl start mastodon-web mastodon-sidekiq mastodon-streaming

Configuring SSL with Let’s Encrypt

To secure your Mastodon instance, obtain an SSL certificate using Let’s Encrypt:

sudo dnf install -y epel-release
sudo dnf install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com

Follow the prompts to obtain and install the SSL certificate.

Post-Installation Steps

Create an admin account for your Mastodon instance:

cd /home/mastodon/live
RAILS_ENV=production bundle exec rails mastodon:make_admin USERNAME=your_username

You can now log in to your Mastodon instance and customize its settings through the admin interface.

Install Mastodon on AlmaLinux 9

Troubleshooting Common Issues

If you encounter issues during the installation process, here are some common problems and their solutions:

Database Connection Problems

If Mastodon can’t connect to the PostgreSQL database, ensure that the database user and permissions are set correctly. You may need to edit the pg_hba.conf file to allow local connections.

Ruby Version Conflicts

If you encounter Ruby version conflicts, make sure you’re using the correct version as specified in the Mastodon requirements. You can switch Ruby versions using RVM:

rvm use 3.0.0

Nginx Configuration Errors

If Nginx fails to start or reload, check your configuration file for syntax errors:

sudo nginx -t

This command will point out any issues in your Nginx configuration.

Maintenance and Upgrades

To keep your Mastodon instance secure and up-to-date, regularly update your system and Mastodon itself. To update Mastodon:

cd /home/mastodon/live
git pull
bundle install
yarn install
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake assets:precompile

After updating, restart the Mastodon services:

sudo systemctl restart mastodon-web mastodon-sidekiq mastodon-streaming

Regularly back up your Mastodon data, including the PostgreSQL database and user-uploaded files.

Congratulations! You have successfully installed Mastodon. Thanks for using this tutorial install Mastodon social network on AlmaLinux 9 system. For additional help or useful information, we recommend you check the Mastodon website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button