×
Community Blog How to Install Mastodon Using Docker on Alibaba Cloud ECS

How to Install Mastodon Using Docker on Alibaba Cloud ECS

In this tutorial, we will be configuring Mastodon on an Alibaba Cloud Elastic Compute Service (ECS) with Ubuntu 16.04 installed.

By Arslan Ud Din Shafiq, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

Mastodon is an open source decentralized social network that allows anyone to host their own server node in the network. In this tutorial, I will install and set up Mastodon on Alibaba Cloud Elastic Compute Service (ECS) with Ubuntu 16.04.

Prerequisites

  • You must have Alibaba Cloud Elastic Compute Service (ECS) activated and verified your valid payment method. If you are a new user, you can get $300 – $1200 worth in Alibaba Cloud credits for your new account. If you don't k about how to setup your ECS instance, you can refer to this tutorial or quick-start guide. Your ECS instance must have at least 2GB RAM and 2 Core processor.
  • A domain name registered from Alibaba Cloud. If you have already registered a domain from Alibaba Cloud or any other host, you can update its domain nameserver records.
  • Domain name must be pointed to your Alibaba Cloud ECS's IP address
  • Access to VNC console in your Alibaba Cloud or SSH client installed in your PC
  • Set up your server's hostname and create user with root privileges.

Setting Up Your Server

Before proceeding with installation of any kind of package, use the following command to update your Ubuntu system. To execute this command, remember to login from non-root user with sudo privileges.

# sudo apt update && sudo apt upgrade

Software-properties-common package is required to get the supported files for installation of Docker CE. To install software-properties-common execute the command.

# sudo apt-get install software-properties-common -y 

Apt-transport-https, ca-certificates, and curl are required for installation of Docker CE. To install them, execute these commands.

# sudo apt-get install apt-transport-https -y 
# sudo apt-get install ca-certificates -y 
# sudo apt-get install curl -y 

Install Docker CE and Docker Compose

Add GPG key for Docker by executing command below.

# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Execute the following command to verify the fingerprint of GPG key.

# sudo apt-key fingerprint 0EBFCD88

Add the Docker repository by executing the command below.

# sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update your system by executing command below to load added repository.

# sudo apt update

Execute the following command to install Docker.

# sudo apt install docker-ce

Add your username to docker group by executing command below.

# sudo adduser aareez docker 

Close your current shell session and start a new session. Otherwise, you won't be able to run Docker and you may see permission errors.

Execute the following command to check either docker run correctly or not.

# docker run hello-world

To download and install Docker Compose, execute the following command.

# sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

To set permissions for Docker Compose file, execute the following command.

# sudo chmod +x /usr/local/bin/docker-compose

Install Mastodon:

Execute the following command to clone Mastodon from Git repository.

# git clone https://github.com/tootsuite/mastodon  

Navigate to downloaded directory by running command below.

# cd mastodon

Execute the following command to copy .env.production.sample to .env.production.

# cp .env.production.sample .env.production

To generate secret keys, you will need to build Docker image and run it. To build it, execute the following command.

# docker-compose build

The Docker image has been built successfully. You will need to generate 3 secret keys that are required for configuration. To generate SECRET_KEY_BASE, execute the following command.

# SECRET_KEY_BASE=$(docker-compose run --rm web bundle exec rake secret)

Execute the following command to insert the value of SECRET_KEY_BASE in .env.production file

# sed -i -e "s/SECRET_KEY_BASE=/&${SECRET_KEY_BASE}/" .env.production

To generate OTP_SECRET, execute the following command.

# OTP_SECRET=$(docker-compose run --rm web bundle exec rake secret)

Execute the following command to insert the value of SECRET_KEY_BASE in .env.production file

# sed -i -e "s/OTP_SECRET=/&${OTP_SECRET}/" .env.production

To generate PAPERCLIP_SECRET, execute the following command.

# PAPERCLIP_SECRET=$(docker-compose run --rm web bundle exec rake secret)

Execute the following command to insert the value of PAPERCLIP_SECRET in .env.production file

# sed -i -e "s/PAPERCLIP_SECRET=/&${PAPERCLIP_SECRET}/" .env.production

You will need to update the value of LOCAL_DOMAIN. To do so, you will need to open .env.production file in text editor. Execute the following command to open .env.production file.

# sudo nano ~/mastodon/.env.production

Find LOCAL_DOMAIN variable and change its value from example.com to your domain name or IP address of ECS and save the updated file.

Navigate to /mastodon

# cd mastodon

You will need to build the Docker image again because you have made changes above. To build it, execute the following command.

# docker-compose build

execute the following command to run migrations.

# docker-compose run --rm web rails db:migrate

To make things snappier, you will need to pre-compile assets. To do so, execute the following command.

# docker-compose run --rm web rails assets:precompile

Run the container by executing command below.

# docker-compose up -d

Install and Configure Nginx Server

To make Mastodon working by creating Nginx configuration and install SSL certificate, you will require Nginx server to be installed.

Execute the following command to install nginx server.

# sudo apt-get install nginx

You will need to remove default site Nginx configuration by executing the command below.

# sudo rm /etc/nginx/sites-available/default

Remove symlink for default site by executing command below.

# sudo rm /etc/nginx/sites-enabled/default

Create the Nginx configuration file for Mastodon by executing command below.

# sudo touch /etc/nginx/sites-available/mastodon

Create symlink of Mastodon by executing command below.

# sudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon

Execute the following command to open Mastodon Nginx configuration file in nano text editor.

# sudo nano /etc/nginx/sites-available/mastodon

Copy-paste the following text in opened file and save the changes.

map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

server {
  listen 80;
  listen [::]:80;
  server_name softpedia.xyz;
  root /home/mastodon/live/public;
  # Useful for Encrypt
  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 softpedia.xyz;

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

  ssl_certificate     /etc/letsencrypt/live/softpedia.xyz/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/softpedia.xyz/privkey.pem;

  keepalive_timeout    70;
  sendfile             on;
  client_max_body_size 80m;

  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;

  add_header Strict-Transport-Security "max-age=31536000";

  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;
}

Install SSL Certificate:

We will install SSL certificate using Let's Encrypt with Certbot. To do so, execute the following steps.

Update the package.

# sudo apt-get update

Install software-properties-common.

# sudo apt-get install software-properties-common

Add the certbot repository by using command below.

# sudo add-apt-repository ppa:certbot/certbot

Update the package to load the added certbot repository.

# sudo apt-get update

Stop apache before issuance of SSL certificate.

# sudo systemctl stop apache2

Install python-certbot-apache using the command below.

# sudo apt-get install python-certbot-apache

Execute the following command to get Let's Encrypt SSL issued.

# sudo certbot --apache -d softpedia.xyz

Select the option 2 to redirect the link to https and update virtual host settings for SSL. Restart apache server.

# sudo systemctl start apache2

You can access your website at https://your_domain_name.tld

1

There you go! You have successfully installed Mastodon on an Alibaba Cloud ECS server. You can secure it by changing the database default password and by setting up an administrator account.

0 0 0
Share on

Alibaba Clouder

1,402 posts | 218 followers

You may also like

Comments

Alibaba Clouder

1,402 posts | 218 followers

Related Products