
Mastodon is an open-source decentralized social network that fronts full data control, and ease of use as its key features. In this article you’ll install and secure Mastodon on a Ubuntu 22.04 Vultr Cloud Server.
Before you begin, be sure to:
Mastodon requires a PostgreSQL database to store files and application data. You can either install PostgreSQL on your server, or deploy a Vultr managed database for PostgreSQL to avoid any bottlenecks in your production environment. For this article, use a Vultr managed database for PostgreSQL.
Install the PostgreSQL client package.
Login to your PostgreSQL database server. Replace the following example values with your actual Vultr database details.
If installed on the same server, run the following command to login.
Create the Mastodon database.
Create a new database user.
Assign the user a strong password.
Grant the user database creation privileges.
Grant the user ownership privileges to the mastodon database.
Exit the PostgreSQL console.
To send emails from your Mastodon server, you need to install Postfix and setup SMTP relay on the server. If you have an existing mail server, configure it to act as your SMTP relay host. Also, you can use a free email service provider such as Sendinblue, Mailjet or Mailchimp to act as the relay host. For this article, create a Sendinblue account to act as the SMTP relay host then setup Postfix as described below.
Login to your Sendinblue account.
Click your account in the top right corner and select Senders & IP from the list.
Click Domains within the senders, domains and dedicated IPs section.
Click Add a domain, and enter your domain in thedomain name field, then click save to generate DNS records.
If you host domain DNS on Vultr, access the DNS panel, and create the records to authenticate your account.
When added, click Verify & authenticate to approve your domain.
Navigate back to Senders & IP, select Senders, and click Add a sender.
Assign your sender account a name, for example Mastodon on Vultr, then enter the email address to assign the Mastodon sender address in the From email field. For example, noreply@mastodon.example.com, and click save your sender details.
Navigate to Transactional on the main menu bar.
Within the configuration section, copy your Login and encrypted password in the SMTP Settings field.
Click the Configuration example with Postfix tab, select and copy the generated code snippet.
Access your Vultr server SSH session.
Update the server.
Install Postfix and the libsasl2-modules package.
When prompted for the mail configuration type, keep Internet Site selected, and press enter to proceed. Enter your domain name in the System mail name field and press enter to install Postfix.
Using a text editor such as nano, edit the main Postfix configuration file.
Paste your Sendinblue Postfix code at the end of the file. For example:
Find the following line.
Comment it by adding # at the start of the line to avoid configuration errors as below.
Save and close the file.
Create the sasl_passwd file in the Postfix directory as below.
Edit the file.
Add the following contents. Replace example-user:password with the details you copied earlier on step 10.
Save and close the file.
Create a corresponding hash db file using postmap.
Restart Postfix to save settings.
To test that your SMTP settings work, install the mailx package, and send a test email to your active email address as below.
Install mailx
Send the test email. Replace hello@example.com with your active email address.
If your SMTP settings are correct, you should receive an email from noreply@mastodon.yourdomain.com
Install Nginx to work as the main web server application.
Mastodon requires NodeJS version 16, run its repository installer.
Install NodeJS
Install Ruby, Ruby, Redis® Server and all necessary Mastodon dependencies.
Using npm, install the yarn package.
Clone the Mastodon GitHub repository.
Move the created mastodon directory to a more central directory like /var/www/.
Create a new Mastodon user with no login access on the server.
Grant the user ownership privileges to the mastodon directory.
Navigate to the mastodon directory.
Visit the Mastodon GitHub releases page and keep note of the latest version, then, install it as the mastodon user as below.
For example, to install version v4.0.2, run the following command.
Verify that you’re operating in the mastodon directory.
Install the Ruby dependency manager bundler.
Install Mastodon dependency packages.
Start the Mastodon installation wizard.
You should receive the following output:
Reply to each of the prompts and press enter as follows:
*Domain name: Enter your Mastodon domain name. This article uses mastodon.example.com. *Enable single user mode: N to allow multiple user registrations, enter Y for personal use. *Are you using Docker to run Mastodon: N *PostgreSQL host: Enter your Vultr managed database for PostgreSQL host URL, or 127.0.0.1 if you installed PostgreSQL locally on the server. *PostgreSQL port: The Vultr managed database for PostgreSQL port. Keep 5432 for local PostgreSQL. *PostgreSQL database: Enter the Mastodon database you created earlier. *PostgreSQL user: Mastodon database user. *PostgreSQL user password: The Mastodon database user password. *Redis® host: 127.0.0.1 Redis® port: 6379 *Redis® password: Press enter as the local Redis® instance has no password. *Do you want to store uploaded files on the cloud? Enter Yes to fill in your Vultr Object Storage details, No to store all Mastodon files directly on your server storage. *Do you want to send emails from localhost? Enter Yes to use the Postfix configuration. *E-mail address to send e-mails from”: noreply@mastodon.example.com depending on the sender address you set in the Postfix sasl_passwd configuration. *Send a test e-mail with this configuration right now? Enter Yes to send a test message to your email address.
Send test e-mail to: Enter your active email address to receive the test email.This configuration will be written to .env.production Save configuration?: Yes to save all changes to the environment file.The final step is compiling CSS/JS assets. This may take a while and consume a lot of RAM. Compile the assets now?: Yes to start installing Mastodon.Do you want to create an admin user straight away?: Yes to setup the Mastodon administrator.Username: Enter your desired administrator username, admin by default.E-mail: Enter your active email address.When installation is complete, an auto generated password displays for your administrator account, select and copy it to your clipboard.
Mastodon listens on the local host port 3000 and 4000, to secure your production server, verify that these ports are not accessible through the public interface. Instead, allow the HTTP port 80, and HTTPS port 443 on your firewall to access Mastodon through your domain name by setting up the Nginx reverse proxy as described in this section.
Verify that Uncomplicated Firewall (UFW) is running on your Ubuntu server.
If inactive, allow the SSH port 22, and start the firewall.
Show the firewall table, and verify that Mastodon ports 3000,4000 are not allowed through the firewall.
Allow the HTTP port 80.
Allow the HTTPS port 443.
Restart the firewall to load changes.
To tighten your Mastodon production server security, configure the Vultr Firewall to operate on top of your existing server firewall as below.
Access your Vultr account.
On the Products panel, find and click Firewall panel on the navigation menu.
Click Add Firewall Group, assign your group a unique description, for example production server rules to create the firewall group.
On the Manage Firewall Group panel, navigate to the Inbound IPV4 Rules section.
Click the + Add Firewall Rule Action to allow SSH access as your first rule. To tighten server security, edit the rule, and select MyIP as the source.
Click the Protocol drop down, and select HTTP from the common applications list, then, click the + Action button to add the firewall rule.
Select HTTPS from the Protocol drop down, keep Anywhere as the source, and click the + Action button to save rule.
Click Linked Instances on the left navigation list, select your Mastodon server from the list, and click the + Add Linked Instance button to apply changes.
To tighten your server security with more firewall rules, please visit the Vultr Firewall documentation.
To secure Mastodon, you need to serve all application requests over HTTPS instead of plain HTTP. To enable HTTPS, generate free SSL certificates from a trusted authority like Let’s Encrypt as described in this section.
Install the snapd daemon.
Install the Certbot Let’s Encrypt application.
Activate the system-wide certbot command.
Temporarily stop Nginx to avoid conflicts on port 80.
Generate a free SSL Certificate. Replace mastodon.example.com with the domain name pointed to your server.
When successful, the SSL certificate files are present in the /etc/letsencrypt/live/ directory.
Restart Nginx
Test that the certificate auto renews upon expiry.
Navigate to the Mastodon files directory.
Copy the Mastodon Nginx configuration template to the Nginx conf.d directory.
Edit the new Mastodon Nginx configuration file.
Find the following lines in both server { blocks.
Change the server_name directive to your domain name, and root to your Mastodon directory as below.
Find the following two lines within the listen 443 server block.
Uncomment them by removing the # key, then, change example.com to your domain as below.
Save and close the file.
For the setting to be successful, verify that your domain is present in the
/etc/letsencrypt/live/directory, else, request for an SSL certificate without the Nginx plugin to avoid Nginx errors.
Test your Nginx configuration for errors.
If the command returns any error, please verify that you made the correct configuration changes as described above.
Restart Nginx for changes to take effect.
To install and start Mastodon as a system service, edit the system template files using the sed stream editor, and setup the services as below.
Verify that the Mastodon system template files are available.
Change the default user entry in all service template files to your Mastodon user account as below.
Change the working directory in all files from /home/mastodon/live to your Mastodon directory /var/www/mastodon/.
Change the bundle directory in all files from /home/mastodon/.rbenv/shims/bundle to /usr/local/bin/bundle.
Copy all service files to the /etc/systemd/system/ directory.
Reload the systemd daemon to enable the files.
Enable each of the 3 Mastodon services to start at boot time.
Start all Mastodon services.
Verify that all services are up and running.
Verify that Mastodon is listening on port 3000.
Output:
Using a web browser like Google Chrome, visit your Mastodon server domain.
To test that new users can register on your Mastodon server, click Create account.
Fill-in your user details, and enter your non-administrator email to sign up.
Check your email inbox, and click the Verify email address button to approve your new user registration.
Log out the user, then, Log in using the administrator username and password you created during installation.
Start managing your Mastodon server.
During and after installation of Mastodon on your server, you may receive any of the following errors, if you encounter any of them, fix them using the recommendations below.
Mastodon unable to send mails
Verify that you setup Postfix correctly, and you are able to send emails through your relay host. To investigate why emails are not sent from your Mastodon server, visit the following URL while logged in with the administrator account.
Depending on your results, view the Postfix mail log and check for any reported errors.
An error occurred while installing charlock_holmes (0.7.7), and Bundler cannot continue
You may receive this error while installing Mastodon, to clear it, please install all required dependencies using the following command.
Database connection could not be established with this configuration, try again. connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:
You may receive this error if you are running PostgreSQL on your server, to clear it, please enter 127.0.0.1 as your database host.
Mastodon Runtime Error
If the Mastodon returns any runtime errors, please check the systemd journals for errors.
Mastodon not loading
Verify that Nginx is up and running.
Verify that all Mastodon services are active and running.
To keep ahead of any potential server or application failures, it’s important to backup the following Mastodon data:.
.env.production fileIn your home directory, create a new Mastodon backup files directory.
Backup the Mastodon PostgreSQL database using pg_dump.
For local PostgreSQL.
Make a full backup of the Mastodon directory.
Back up the Redis® database.
To easily back up all necessary Mastodon files, and replace any existing files in your target directory, create a new backup.sh file.
Add the following contents to the file.
Save and close the file.
Run the script.
Before upgrading Mastodon, please make a full backup to recover your instance in case your upgrade breaks.
Navigate to the Mastodon directory.
Fetch new Mastodon release tags.
Upgrade to the latest version. For example, to upgrade to v4.0.2, run the following command.
Migrate the database.
Compile Mastodon assets.
When successful, migrate the database again.
Restart the web process.
Restart the streaming API.
Restart background workers.
You have installed Mastodon on a Vultr Cloud server, for more configuration options, please visit the official Mastodon documentation.
0 Comments
Be the first to comment and share your perspective with the community.