mastodon social

How to Install a Mastodon Social Server: A Beginner’s Guide

Installing a Mastodon Social server can seem daunting, but with the right guidance, even a novice Linux user can do it. Here’s a detailed, step-by-step guide to help you through the process:

Step 1: Preparing Your Server

  1. Choose a VPS Provider: Select a VPS (Virtual Private Server) provider. Some popular choices include DigitalOcean, Linode, and AWS. Ensure your VPS has at least 4GB of RAM, 2 CPU cores, and 20GB of storage.
  2. Access Your Server: Once you’ve set up your VPS, you’ll need to access it via SSH. Open a terminal on your local machine and connect to your VPS using the following command:
   ssh root@your_server_ip

Step 2: Setting Up the Basics

  1. Update Your Server: Before installing anything, update your package list and upgrade existing packages:
   apt update && apt upgrade -y
  1. Create a New User: For security reasons, create a new user (e.g., mastodon) and give them sudo privileges:
   adduser mastodon
   usermod -aG sudo mastodon
  1. Switch to the New User: Log out from the root account and log in as the new user:
   su - mastodon

Step 3: Install Dependencies

  1. Install Basic Tools: Install necessary tools and libraries:
   sudo apt install git curl wget -y
  1. Install Node.js: Install Node.js, which is required for Mastodon:
   curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
   sudo apt install -y nodejs
  1. Install Yarn: Yarn is a package manager for JavaScript:
   npm install --global yarn
  1. Install Ruby: Install Ruby and Bundler, which are needed for Mastodon:
   sudo apt install -y gnupg2
   gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
   \curl -sSL https://get.rvm.io | bash -s stable
   source ~/.rvm/scripts/rvm
   rvm install 3.0.2
   rvm use 3.0.2 --default
   gem install bundler
  1. Install PostgreSQL: Mastodon uses PostgreSQL as its database:
   sudo apt install -y postgresql postgresql-contrib
  1. Install Redis: Redis is used for caching and background job processing:
   sudo apt install -y redis-server
  1. Install Nginx: Nginx will serve as the web server:
   sudo apt install -y nginx

Step 4: Setting Up PostgreSQL

  1. Create a PostgreSQL User and Database:
   sudo -u postgres psql
   CREATE USER mastodon CREATEDB;
   \q

Step 5: Installing Mastodon

  1. Clone the Mastodon Repository:
   git clone https://github.com/tootsuite/mastodon.git live
   cd live
  1. Checkout the Latest Stable Release:
   git checkout $(git tag | grep -v - | sort -V | tail -n 1)
  1. Install Ruby Dependencies:
   bundle install -j$(getconf _NPROCESSORS_ONLN) --deployment --without development test
  1. Install JavaScript Dependencies:
   yarn install --pure-lockfile

Step 6: Configuring Mastodon

  1. Setup Environment Configuration:
   cp .env.production.sample .env.production
   nano .env.production

Edit the configuration file with your database details, domain, email server settings, and other relevant information.

  1. Precompile Assets:
   RAILS_ENV=production bundle exec rails assets:precompile

Step 7: Setting Up the Database

  1. Create and Migrate the Database:
   RAILS_ENV=production bundle exec rails db:setup

Step 8: Setting Up the Web Server

  1. Configure Nginx: Create an Nginx configuration file for Mastodon:
   sudo nano /etc/nginx/sites-available/mastodon

Add the following configuration:

   server {
       listen 80;
       listen [::]:80;
       server_name your_domain;

       location / {
           proxy_pass http://127.0.0.1:3000;
           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 $scheme;
       }

       location /api/v1/streaming {
           proxy_pass http://127.0.0.1:4000;
           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 $scheme;
       }
   }
  1. Enable the Configuration and Restart Nginx:
   sudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon
   sudo systemctl restart nginx

Step 9: Running Mastodon

  1. Set Up Systemd Services: Create systemd service files for Mastodon:
   sudo nano /etc/systemd/system/mastodon-web.service

Add the following content:

   [Unit]
   Description=Mastodon web
   After=network.target

   [Service]
   Type=simple
   User=mastodon
   Environment="RAILS_ENV=production"
   WorkingDirectory=/home/mastodon/live
   ExecStart=/usr/local/bin/bundle exec rails s -b 127.0.0.1 -p 3000
   Restart=always

   [Install]
   WantedBy=multi-user.target
  1. Create Service for Streaming:
   sudo nano /etc/systemd/system/mastodon-streaming.service

Add the following content:

   [Unit]
   Description=Mastodon streaming
   After=network.target

   [Service]
   Type=simple
   User=mastodon
   Environment="NODE_ENV=production"
   WorkingDirectory=/home/mastodon/live
   ExecStart=/usr/local/bin/npm run start
   Restart=always

   [Install]
   WantedBy=multi-user.target
  1. Create Service for Sidekiq:
   sudo nano /etc/systemd/system/mastodon-sidekiq.service

Add the following content:

   [Unit]
   Description=Mastodon sidekiq
   After=network.target

   [Service]
   Type=simple
   User=mastodon
   Environment="RAILS_ENV=production"
   WorkingDirectory=/home/mastodon/live
   ExecStart=/usr/local/bin/bundle exec sidekiq
   Restart=always

   [Install]
   WantedBy=multi-user.target
  1. Start and Enable Services:
   sudo systemctl daemon-reload
   sudo systemctl enable mastodon-web mastodon-streaming mastodon-sidekiq
   sudo systemctl start mastodon-web mastodon-streaming mastodon-sidekiq

Step 10: Securing Your Installation

  1. Obtain an SSL Certificate: Use Certbot to obtain a free SSL certificate:
   sudo apt install certbot python3-certbot-nginx
   sudo certbot --nginx -d your_domain
  1. Renew SSL Certificate Automatically:
   sudo certbot renew --dry-run

Step 11: Final Checks

  1. Access Your Mastodon Instance: Open your web browser and navigate to https://your_domain. You should see the Mastodon setup screen.
  2. Create Your Admin Account: Follow the on-screen instructions to create your admin account and finalize the setup.

Troubleshooting

  1. Check Logs: If something goes wrong, check the logs for errors:
   journalctl -u mastodon-web
   journalctl -u mastodon-streaming
   journalctl -u mastodon-sidekiq
  1. Fix Common Issues: If you encounter permission issues, ensure that all files and directories are owned by the mastodon user. If the web server isn’t responding, double-check your Nginx configuration.

By following these steps carefully, you should have a fully functioning Mastodon Social server. Remember, each step builds on the previous one, so take your time and double-check your work.

Other Recent Posts