LoginSignup
1
1

More than 1 year has passed since last update.

某AIにお手軽にMastodonを構築するためのスクリプトを作らせた 第一章

Last updated at Posted at 2023-05-01

最近AIくんがめざましくあったま良いようなので、Mastodonのインストールを簡略化したいと目論んだ。
そこで以下のような質問をぶん投げた。

Question:ubuntuサーバーにMastodonを簡単にインストールするためのスクリプトを作成してほしい。

Answer:以下は、UbuntuサーバーにMastodonを簡単にインストールするためのスクリプトです。このスクリプトでは、必要な依存関係のインストール、Mastodonのインストール、設定、および起動を行います。

#!/bin/bash

# Update and upgrade the system
sudo apt update
sudo apt upgrade -y

# Install necessary dependencies
sudo apt install -y curl git imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file g++ gcc autoconf automake bison build-essential libssl-dev libyaml-dev libreadline-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev redis-server postgresql postgresql-contrib certbot python-certbot-nginx yarn libidn11-dev libicu-dev libjemalloc-dev

# Install rbenv and ruby-build
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

# Install Ruby and set it as the default version
rbenv install 2.7.3
rbenv global 2.7.3

# Install bundler
gem install bundler

# Clone Mastodon repository
git clone https://github.com/tootsuite/mastodon.git
cd mastodon

# Install gems and npm packages
bundle install --deployment --without development test
yarn install --pure-lockfile

# Set up Mastodon configuration
cp .env.production.sample .env.production

# Edit the .env.production file with your favorite text editor to configure your instance

# Set up the database
sudo -u postgres psql -c "CREATE USER mastodon CREATEDB;"
RAILS_ENV=production bundle exec rails db:setup

# Precompile assets
RAILS_ENV=production bundle exec rails assets:precompile

# Install and configure nginx
sudo apt install -y nginx
sudo rm /etc/nginx/sites-enabled/default

# Create the nginx configuration file for Mastodon
sudo bash -c 'cat > /etc/nginx/sites-available/mastodon << EOL
map \$http_upgrade \$connection_upgrade {
  default upgrade;
  ""      close;
}

server {
  listen 80;
  server_name example.com;
  root /home/mastodon/mastodon/public;
  client_max_body_size 80m;

  location / {
    try_files \$uri @proxy;
  }

  location @proxy {
    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;
    proxy_set_header Proxy "";
  }

  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;
    proxy_set_header Proxy "";
    proxy_http_version 1.1;
    proxy_set_header Upgrade \$http_upgrade;
    proxy_set_header Connection \$connection_upgrade;
    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 "";
  }
}
EOL'

# Replace 'example.com' with your domain name in the nginx configuration
sudo sed -i 's/example.com/your_domain.com/g' /etc/nginx/sites-available/mastodon

# Enable the site and restart nginx
sudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/
sudo systemctl restart nginx

# Set up Let's Encrypt SSL certificate
sudo certbot --nginx -d your_domain.com

# Start Mastodon services
RAILS_ENV=production bundle exec rails db:migrate
RAILS_ENV=production bundle exec rails assets:precompile

このスクリプトを使用する前に、your_domain.comを実際のドメイン名に置き換えてください。また、.env.productionファイルを編集して、Mastodonインスタンスに適切な設定を適用してください。特に、以下の設定が重要です。

LOCAL_DOMAIN: あなたのドメイン名
SINGLE_USER_MODE: false(マルチユーザーインスタンスを設定する場合)
SECRET_KEY_BASE: 安全なランダムな文字列(bundle exec rails secretを実行して生成できます)
OTP_SECRET: 安全なランダムな文字列(bundle exec rails secretを実行して生成できます)
VAPID_PRIVATE_KEYおよびVAPID_PUBLIC_KEY: これらは、bundle exec rake mastodon:webpush:generate_vapidを実行して生成できます。
このスクリプトを使用してMastodonをインストールし、すべての設定が完了したら、以下のコマンドを実行してMastodonサービスを起動してください。

RAILS_ENV=production bundle exec rails db:migrate
RAILS_ENV=production bundle exec rails assets:precompile

そして、以下のコマンドでMastodonのサービスを開始します。

RAILS_ENV=production bundle exec puma -C config/puma.rb
RAILS_ENV=production bundle exec sidekiq -C config/sidekiq.yml

Mastodonが正常にインストールされ、起動することを確認したら、Webブラウザであなたのドメインにアクセスして、Mastodonインスタンスが実行されていることを確認してください。

注意: このスクリプトは、UbuntuサーバーにMastodonをインストールするための簡単な手順を提供しますが、セキュリティやパフォーマンスの観点から、適切な設定や最適化が必要になる可能性があります。本番環境での使用には十分注意してください。

うーん、どうやら幾つかのパラメータを置き換えなければならないようです。
次は対話形式にしてみよう・・・

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1