How to Intall a Mastodon server on Digital Ocean
I should preface this by telling you all this is NOT a beginner tutorial. You are expected to have a basic understanding of:
- linux
- command line
- ruby (not much)
- Servers
- Databases
But...
Installing Mastodon in Ubuntu 20.04
Requirements
Server requirements:
Minimum Hardware Requirements:
- 2GB RAM
- 2vCPU
Software Requirements
- PostgreSQL 9.5+
- Redis 4+
- Ruby 2.6+
- Node.js 14+
- NGINX
- Domain - to link to you Mastodon instance
- SMTP Service - useful for sending outbound emails
Point your Domain to your Mastodon Instance
- Log in to your Domain Server Provider
- Find your domain and go to your DNS Zone Editor.
- Add a DNS record (A-record) to point to your Mastodon instance. For example: a. If you want to use your root domain, i.e. mydomain.com, add an A-record and set the IP-address of your Mastodon instance as the value for this record b. Or if you want to use a subdomain, i.e. social.mydomain.com, add an A-record and still set the value to be the IP-address of your Mastodon instance.
Before proceeding to the next section, make sure to connect to your Mastodon Instance via SSH.
Install NodeJS
- Run:
sudo apt update && sudo apt upgrade -y
- Run:
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
- Run:
sudo apt install nodejs
Because Mastodon uses Yarn package manager, we will need to install that as well.
-
Run:
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
-
Run:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
-
Run:
sudo apt update && sudo apt -y install yarn
Install Ruby
- Run: sudo apt install ruby ruby-dev
Make sure to check your ruby version by running:
ruby -v
Setup the PostgreSQL Database Server
If you have not run an apt update command from the previous section, please do so by running:
sudo apt update
sudo apt install postgresql postgresql-contrib
By default, the database server will start and listen on 127.0.0.1:5432, you can confirm that by running:
sudo ss -lnpt | grep postgres
You should get an output after running the command. For some reason no output shows up, it means your postgresql database server is not running. Run this command to start it:
sudo systemctl start postgresql
Log in to the PostgreSQL console by logging in as postgres, the superuser.
-
Run:
sudo -u postgres -i psql
-
Create the database, run:
CREATE DATABASE mastodon;
-
Create a database user, run: CREATE USER mastodon;
-
Set your database user password, run:
ALTER USER mastodon WITH ENCRYPTED PASSWORD 'your_password_here';
-
Make sure to replace it with your own password Grant user to create a database, run:
ALTER USER mastodon CREATEDB;
-
Assign the user as the owner of the database, run:
ALTER DATABASE mastodon OWNER TO mastodon;
-
Exit the PostgreSQL console by entering:
\q
Configure Mastodon
We need a new system user to run the Mastodon server.
Run this command: sudo adduser mastodon --system --group --disabled-login
- Clone the Mastodon repo.
Run:
git clone https://github.com/tootsuite/mastodon.git
We will store the Mastodon code in /var/www/, so we will need to create that directory.
Run: sudo mkdir -p /var/www
Then let’s move the Mastodon code to that directory. Run: sudo mv mastodon/ /var/www/
Update and change the ownership of that project directory.
sudo chown -R mastodon:mastodon /var/www/mastodon/
Go to your mastodon directory by changing directory:
cd /var/www/mastodon/
Checkout to the latest stable release of Mastodon
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)
Install the gem bundler, sudo gem install bundler
Install required packages:
sudo apt install redis-server optipng pngquant jhead jpegoptim gifsicle nodejs
imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file 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
libidn11-dev libicu-dev libjemalloc-dev
Install Mastodon packages:
sudo -u mastodon bundle config deployment 'true'
sudo -u mastodon bundle config without 'development test'
sudo -u mastodon bundle install -j$(getconf _NPROCESSORS_ONLN)
Setup Mastodon:
sudo -u mastodon RAILS_ENV=production bundle exec rake mastodon:setup
During the process, you will be asked several questions. Answer them as shown below:
- Your instance is identified by its domain name.
- Changing it afterward will break things. Domain name: mydomain.com
Single user mode disables registrations and redirects the landing page to your public profile.
Do you want to enable single user mode? No
Are you using Docker to run Mastodon? no
PostgreSQL host: 127.0.0.1 PostgreSQL port: 5432 Name of PostgreSQL database: mastodon Name of PostgreSQL user: mastodon Password of PostgreSQL user: Database configuration works!
Redis host: 127.0.0.1 Redis port: 6379 Redis password: Redis configuration works!
Do you want to store uploaded files on the cloud? No
Do you want to send e-mails from localhost?
no
E-mail address to send e-mails "from": Mastodon <notifications@mydomain.com>
Send a test e-mail with this configuration right now? Yes
This configuration will be written to .env.production Save configuration? Yes Send test e-mail to: a_valid_email@mydomain.com
Now that configuration is saved, the database schema must be loaded.
If the database already exists, this will erase its contents.
Prepare the database now? Yes
All done! You can now power on the Mastodon server
Do you want to create an admin user straight away? Yes Username: admin E-mail: admin@mydomain.com You can login with the password: 159e473bbfe846eac169eafac880b3f0 You can change your password once you login.
Create Systemd Service for Mastodon
Run the following commands
sudo cp /var/www/mastodon/dist/mastodon-web.service /etc/systemd/system/ sudo
cp /var/www/mastodon/dist/mastodon-sidekiq.service /etc/systemd/system/ sudo
cp /var/www/mastodon/dist/mastodon-streaming.service /etc/systemd/system/
Update the services files:
sudo sed -i 's/home/mastodon/live/var/www/mastodon/g'
/etc/systemd/system/mastodon-.service sudo sed -i
's/home/mastodon/.rbenv/shims/usr/local/bin/g'
/etc/systemd/system/mastodon-.service
sudo systemctl daemon-reload Start the mastodon services, run: sudo systemctl
enable --now mastodon-web mastodon-sidekiq mastodon-streaming Make sure that
they are running, run this command: sudo systemctl status mastodon-web
mastodon-sidekiq mastodon-streaming
Nginx as the Reverse Proxy for Mastodon Install by running: sudo apt install nginx Mastodon has an nginx configuration template, create a copy from that and use it.
sudo cp /var/www/mastodon/dist/nginx.conf /etc/nginx/conf.d/mastodon.conf
Update the config file by replacing it with your domain name, example:
mydomain.com
Run: sed -i 's/example.com/mydomain.com/g' /etc/nginx/conf.d/mastodon.conf
Make sure to replace mydomain.com in the command above with your domain name
Open the config file and replace: root /home/mastodon/live/public; to: root /var/www/mastodon/public; Comment out lines starting with ssl_certificate and ssl_certificate_key
Add the following lines: ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem; ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
Save and close the config file
Test your configuration, run: sudo nginx -t If the previous command is successful, reload the config:
sudo systemctl reload nginx
Visit your Mastodon installation in your browser through your registered domain, i.e.: http://mydomain.com
Adding SSL Cert using Let’s Encrypt
Install certbot, run: sudo apt install certbot python3-certbot-nginx Run the
command to obtain certificate: sudo certbot --nginx --agree-tos --redirect
--hsts --staple-ocsp --email admin@mydomain.com -d mydomain.com
You will get a Congratulations! message on success. Otherwise, it’ll tell you about the problem. Visit your Mastodon install via the https url, i.e.: https://mydomain.com