YJK

独立世界

Independent World
twitter
telegram

Mastodonを構築し、連邦ソーシャルネットワークに参加する

Mastodon は、ActivityPub プロトコルに基づく自由でオープンソースのソーシャルネットワークサーバーで、ユーザーは友達をフォローし、新しい友達を見つけることができます。Mastodon では、ユーザーはリンク、画像、テキスト、動画など、あらゆるコンテンツを投稿できます。すべての Mastodon サーバーは相互に通信でき、連合ネットワークを形成します。ActivityPub プロトコルを実装した非 Mastodon ソフトウェアを使用していても、Mastodon ユーザーとシームレスに交流できます!

Mastodon は分散型のソーシャルネットワークであり、これは各ユーザーが自分のサーバー(または「インスタンス」)を選択し、自分のデータを管理できることを意味しますが、同時に Mastodon ユーザーの一部でもあります。Mastodon は ActivityPub プロトコルに基づいているため、Pleroma、Friendica、Hubzilla など、同じプロトコルを使用する他の分散型ソーシャルネットワークとも通信できます。この相互運用性は、ユーザーが単一の集中型プラットフォームや企業に制限されない、より多様で柔軟なオンラインコミュニティを促進するのに役立ちます。

以下は、VPS 上に Mastodon をセットアップする方法の簡単な紹介です(最低 1C2G、Debian 11 の root ユーザーを推奨):

image

1/ Docker と Compose のインストール#

1 つのコマンド:

curl -L get.docker.com | bash

2/ Nginx と ACME.SH のインストール#

ここでは n.wtf がパッケージ化した Nginx を推奨します:

# 必要なソフトウェアをインストール
apt install -y lsb-release ca-certificates apt-transport-https curl gnupg dpkg
 
# PGPキーをダウンロード
curl -sS https://n.wtf/public.key | gpg --dearmor > /usr/share/keyrings/n.wtf.gpg
 
# リポジトリを追加
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/n.wtf.gpg] https://mirror-cdn.xtom.com/sb/nginx/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/n.wtf.list
 
# システムを更新
apt update
 
# 最新のNginxをインストール
apt install nginx-extras -y

acme.sh をインストールするための 1 つのコマンド:

curl -L get.acme.sh | bash

インストールが完了したら、現在のターミナルを閉じて再度開く必要があるかもしれません。これにより、acme.sh のエイリアスが有効になります。

3/ Mastodon のインストール#

新しいフォルダーを作成

mkdir -p /var/www/mastodon && cd /var/www/mastodon

docker-compose.yml を編集

vim docker-compose.yml

iを押して編集を開始し、Shift+Insで貼り付け、Escを押して:wqを入力して保存して終了します。

version: '3'
services:
  db:
    restart: always
    image: postgres:12.5-alpine
    shm_size: 256mb
    networks:
      - internal_network
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
    volumes:
      - ./postgres:/var/lib/postgresql/data

  redis:
    restart: always
    image: redis:6-alpine
    networks:
      - internal_network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
    volumes:
      - ./redis:/data

#  es:
#    image: kubesphere/elasticsearch-oss:6.7.0-1-arm64
#    environment:
#      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
#      - "cluster.name=es-mastodon"
#      - "discovery.type=single-node"
#      - "bootstrap.memory_lock=true"
#    networks:
#      - internal_network
#    healthcheck:
#      test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
#    volumes:
#      - ./elasticsearch:/usr/share/elasticsearch/data
#    ulimits:
#      memlock:
#        soft: -1
#        hard: -1
#    restart: unless-stopped

  web:
    image: plusminusio/mastodon:latest-arm64
    restart: always
    env_file: .env.production
    command: bash -c "rm -f /mastodon/tmp/pids/server.pid; bundle exec rails s -p 3000"
    networks:
      - external_network
      - internal_network
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:3000/health || exit 1"]
    ports:
      - "127.0.0.1:3000:3000"
    depends_on:
      - db
      - redis
#      - es
    volumes:
      - ./public/system:/mastodon/public/system

  streaming:
    image: plusminusio/mastodon:latest-arm64
    restart: always
    env_file: .env.production
    command: node ./streaming
    networks:
      - external_network
      - internal_network
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:4000/api/v1/streaming/health || exit 1"]
    ports:
      - "127.0.0.1:4000:4000"
    depends_on:
      - db
      - redis

  sidekiq:
    image: plusminusio/mastodon:latest-arm64
    restart: always
    env_file: .env.production
    command: bundle exec sidekiq
    depends_on:
      - db
      - redis
    networks:
      - external_network
      - internal_network
    volumes:
      - ./public/system:/mastodon/public/system

networks:
  external_network:
  internal_network:
    internal: true

データベースを設定

docker run --name postgres12 -v /var/www/mastodon/postgres:/var/lib/postgresql/data -e   POSTGRES_PASSWORD=データベース管理者パスワードを設定 --rm -d postgres:12.5-alpine

postgres フォルダーを確認し、postgres に関連する複数のファイルが表示されるはずです。空のフォルダーではありません。

次に実行

docker exec -it postgres12 psql -U postgres

入力

CREATE USER mastodon WITH PASSWORD 'データベースパスワード(できればデータベース管理者パスワードとは異なるもの)' CREATEDB;

mastodon ユーザーを作成し、docker を停止します。

docker stop postgres12

.env.production を設定

# mastodonフォルダーに戻る
cd /var/www/mastodon

# ファイルを生成
touch .env.production

# 設定ファイル
docker compose run --rm web bundle exec rake mastodon:setup
  1. ドメイン名を入力
  2. シングルユーザーモードを有効にしますか? いいえ
  3. Mastodon を実行するために Docker を使用しますか? はい
  4. postsql ユーザー名、データベース名には mastodon を入力し、パスワードには先ほど設定したデータベースパスワードを入力
  5. redis の部分はすべてそのまま Enter
  6. アップロードしたファイルをクラウドに保存しますか? これはいいえと入力できます。必要に応じて S3 を設定できます。
  7. localhost からメールを送信しますか? いいえ。その後、メールサービスの設定を入力します。 Mailazyの使用を推奨します。具体的な設定は以下を参照してください
  8. この設定は.env.production に書き込まれます。設定を保存しますか? はい
  9. その後、.env.production の設定が表示されます。コピーして、まずコンピュータに保存してください。後で使用します
  10. その後、データベースを作成し、コンパイルするように求められます。すべて「はい」を選択します。最後に管理者アカウントを作成します。

成功した後、.env.productionファイルを編集し、iを押して編集を開始し、Shift+Insで貼り付け、Escを押して:wqを入力して保存して終了します。

vim .env.production

S3 および SMTP 情報は次のようになります。

S3_ENABLED=true
S3_PROTOCOL=https
S3_REGION=fr-par
S3_ENDPOINT=https://s3.fr-par.scw.cloud
S3_HOSTNAME=[hidden].s3.fr-par.scw.cloud
S3_BUCKET=[hidden]
AWS_ACCESS_KEY_ID=[hidden]
AWS_SECRET_ACCESS_KEY=[hidden]
S3_ALIAS_HOST=[hidden]
SMTP_SERVER=smtp.mailazy.com
SMTP_PORT=587
SMTP_LOGIN=[hidden]
SMTP_PASSWORD=[hidden]
SMTP_AUTH_METHOD=plain # この設定はmailazy専用です。他のサービスプロバイダーの場合は適切に変更してください
SMTP_OPENSSL_VERIFY_MODE=none # この設定はmailazy専用です。他のサービスプロバイダーの場合は適切に変更してください
SMTP_FROM_ADDRESS=mastodon@[hidden]

実行

docker compose up -d

該当するフォルダーに権限を付与

chown 991:991 -R ./public
chown -R 70:70 ./postgres

# 停止
docker compose down

# 再起動
docker compose up -d

実行状況を確認

docker compose ps
NAME                   COMMAND                  SERVICE             STATUS              PORTS
mastodon-db-1          "docker-entrypoint.s…"   db                  running (healthy)   
mastodon-redis-1       "docker-entrypoint.s…"   redis               running (healthy)   
mastodon-sidekiq-1     "/usr/bin/tini -- bu…"   sidekiq             running             4000/tcp
mastodon-streaming-1   "/usr/bin/tini -- no…"   streaming           running (healthy)   127.0.0.1:4000->4000/tcp
mastodon-web-1         "/usr/bin/tini -- ba…"   web                 running (healthy)   127.0.0.1:3000->3000/tcp

# 状態がすべてhealthyであれば、正常に実行されています

4/ Nginx と SSL の設定#

設定ファイルを作成

vim /etc/nginx/conf.d/mastodon.conf

以下の http 設定情報をコピーして SSL 証明書を申請します。iを押して編集を開始し、Shift+Insで貼り付け、Escを押して:wqを入力して保存して終了します。

server {
  listen 80;
  # IPV6を設定した場合は、以下の井号を削除
  # listen [::]:80;
  server_name mastodon.im.sb;
  root /var/www/mastodon/public;
  location /.well-known/acme-challenge/ { allow all; }
  location / { return 301 https://$host$request_uri; }
}

# mastodon.im.sbを自分のドメイン名に置き換えてください

Nginx をリロード

nginx -t
# 設定ファイルに異常がないことを確認
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

# リロード
nginx -s reload

SSL 証明書を申請

あなたのドメインの A レコードと AAAA レコード(あれば)を VPS IP に指向させ、次にドメイン証明書を申請します。

acme.sh --issue -d mastodon.im.sb -w /var/www/mastodon/public --server letsencrypt

# mastodon.im.sbを自分のドメイン名に置き換えてください

申請が成功した後、次のものが必要です:

# SSL完全証明書
/root/.acme.sh/mastodon.im.sb_ecc/fullchain.cer
# SSL秘密鍵
/root/.acme.sh/mastodon.im.sb_ecc/mastodon.im.sb.key

再度設定ファイルを編集

vim /etc/nginx/conf.d/mastodon.conf

以下の完全な設定情報をコピーし、iを押して編集を開始し、Shift+Insで貼り付け、Escを押して:wqを入力して保存して終了します。

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

upstream backend {
    server 127.0.0.1:3000 fail_timeout=0;
}

upstream streaming {
    server 127.0.0.1:4000 fail_timeout=0;
}

server {
  listen 80;
  # IPV6を設定した場合は、以下の井号を削除
  # listen [::]:80;
  server_name mastodon.im.sb;
  root /var/www/mastodon/public;
  location /.well-known/acme-challenge/ { allow all; }
  location / { return 301 https://$host$request_uri; }
}

server {
  listen 443 ssl http2;
  # IPV6を設定した場合は、以下の井号を削除
  # listen [::]:443 ssl http2;
  server_name mastodon.im.sb;

  ssl_certificate  /root/.acme.sh/mastodon.im.sb_ecc/fullchain.cer;
  ssl_certificate_key /root/.acme.sh/mastodon.im.sb_ecc/mastodon.im.sb.key;

  ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
  ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
  ssl_prefer_server_ciphers off;
  ssl_ecdh_curve X25519:secp384r1;

  ssl_session_cache shared:MASTODON:10m;
  ssl_session_timeout 1d;
  ssl_session_tickets off;

  ssl_stapling on;
  ssl_stapling_verify on;
  resolver 1.1.1.1 8.8.8.8 119.29.29.29 valid=300s;
  resolver_timeout 5s;

  root /var/www/mastodon/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" always;

  keepalive_timeout    70;
  sendfile             on;
  client_max_body_size 80m;

  location / {
    try_files $uri @proxy;
  }

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

  location /sw.js {
    add_header Cache-Control "public, max-age=0";
    add_header Strict-Transport-Security "max-age=31536000" always;
    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 $scheme;
    proxy_set_header Proxy "";
    proxy_pass_header Server;

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

    add_header X-Cached $upstream_cache_status;
    add_header Strict-Transport-Security "max-age=31536000" always;

    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 $scheme;
    proxy_set_header Proxy "";

    proxy_pass http://streaming;
    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 403 404 500 501 502 503 504 /500.html;
}

# mastodon.im.sbを自分のドメイン名に置き換えてください

Nginx をリロード

nginx -t
# 設定ファイルに異常がないことを確認
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

# リロード
nginx -s reload

これで、あなたのドメインにアクセスすることで Mastodon にアクセスできます。

5/ 管理者の設定#

あなたの Mastodon にアクセスしてアカウントを登録し、ターミナルに戻って実行:

# mastodonフォルダーに戻る
cd /var/www/mastodon
docker compose run --rm web bin/tootctl accounts modify [YOURACCOUNT] --role Owner

または、ターミナルで直接管理者アカウントを作成:

docker compose run --rm web bin/tootctl accounts create  [YOURACCOUNT] --email [[email protected]] --confirmed --role Owner

ターミナルにランダムに生成されたパスワードが表示されます。


👉 一部の内容はこちらの記事を参考にしました。

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。