深水
深水
发布于 2024-11-17 / 35 阅读
0
0

mastodon 源码部署记录

官方指导:(从源码安装 - Mastodon documentation

基础安装

首先系统使用Ubuntu 24.04

您将以 root 身份运行命令。如果您还不是 root,请切换到 root:sudo -i

先安装 curl、wget、gnupg、apt-transport-https、lsb-release 和 ca-certificates

sudo apt install -y curl wget gnupg apt-transport-https lsb-release ca-certificates

Nodejs

sudo curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
sudo echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

PostgreSQL数据库

sudo wget -O /usr/share/keyrings/postgresql.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc
sudo echo "deb [signed-by=/usr/share/keyrings/postgresql.asc] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/postgresql.list

系统包

sudo apt update
sudo apt install -y \
  imagemagick ffmpeg libvips-tools libpq-dev libxml2-dev libxslt1-dev file git-core \
  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 \
  nginx nodejs redis-server redis-tools postgresql postgresql-contrib \
  certbot python3-certbot-nginx libidn11-dev libicu-dev libjemalloc-dev

Yanr

corepack enable

创建一个mastodon用户

sudo adduser --disabled-password mastodon

设置PostgreSQL

sudo -u postgres psql
CREATE USER mastodon CREATEDB;
\q

切换用户

su - mastodon

拉代码

git clone https://github.com/mastodon/mastodon.git live && cd live
git checkout $(git tag -l | grep '^v[0-9.]*$' | sort -V | tail -n 1)

安装Rub

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec bash
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install

安装依赖

bundle config deployment 'true'
bundle config without 'development test'
bundle install -j$(getconf _NPROCESSORS_ONLN)
yarn install

启动初始化引导(跟着提示输入)

RAILS_ENV=production bin/rails mastodon:setup

完成之后回到root用户

exit

获取ssl证书(example.com替换成自己的域名)它会自己生成在(/etc/letsencrypt/live/example.com/)

certbot certonly --nginx -d example.com

设置Nginx

cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon
ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon
rm /etc/nginx/sites-enabled/default

然后编辑/etc/nginx/sites-available/mastodon把example.com替换成自己的域名并取消ssl相应注释

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

允许其他用户遍历 mastodon 用户的主目录,以便 nginx 的用户可以访问 asset 文件,重启nginx

chmod o+x /home/mastodon
systemctl restart nginx

设置 systemd 服务然后ojbk

cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
$EDITOR /etc/systemd/system/mastodon-*.service
systemctl daemon-reload
systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming

前后端分离部署(进阶)

在完成了上面的基础上可以把前端资源拆分出来部署,解决云服务器资源不足同时“机房”ip问题。

“机房”到云服务器的网络搭建这里不做解释。

准备工作

首先复制一份/home/mastodon/live/public这个文件夹备用

修改/home/mastodon/live/config/puma.rb/home/mastodon/live/streaming/index.js两个文件内的127.0.0.1为0.0.0.0

然后重启服务:systemctl restart --now mastodon-web mastodon-sidekiq mastodon-streaming

做完上面步骤我们转到云服务器

云服务器初始化

您将以 root 身份运行命令。如果您还不是 root,请切换到 root:sudo -i

先安装 curl、wget、gnupg、apt-transport-https、lsb-release 和 ca-certificates

sudo apt install -y curl wget gnupg apt-transport-https lsb-release ca-certificates

Nodejs

sudo curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
sudo echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

系统包(注意这里和之前不一样)

sudo apt update
sudo apt install -y \
  imagemagick ffmpeg libvips-tools libpq-dev libxml2-dev libxslt1-dev file git-core \
  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 \
  nodejs \
  certbot python3-certbot-nginx libidn11-dev libicu-dev libjemalloc-dev

Yanr

corepack enable

创建一个mastodon用户

sudo adduser --disabled-password mastodon

切换用户

su - mastodon

拉代码

git clone https://github.com/mastodon/mastodon.git live && cd live
git checkout $(git tag -l | grep '^v[0-9.]*$' | sort -V | tail -n 1)

完成之后回到root用户

exit

把之前备用的/home/mastodon/live/public替换到云服务的/home/mastodon/live/public

获取ssl证书

(example.com替换成自己的域名)它会自己生成在(/etc/letsencrypt/live/example.com/)

certbot certonly --nginx -d example.com

设置Nginx

cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon
ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon
rm /etc/nginx/sites-enabled/default

然后编辑/etc/nginx/sites-available/mastodon把example.com替换成自己的域名并取消ssl相应注释

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

编辑/etc/nginx/sites-available/mastodon把转发的两个地址改成指向机房服务器的地址端口是3000和4000的两

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

upstream streaming {
    # Instruct nginx to send connections to the server with the least number of connections
    # to ensure load is distributed evenly.
    least_conn;

    server 127.0.0.1:4000 fail_timeout=0;
    # Uncomment these lines for load-balancing multiple instances of streaming for scaling,
    # this assumes your running the streaming server on ports 4000, 4001, and 4002:
    # server 127.0.0.1:4001 fail_timeout=0;
    # server 127.0.0.1:4002 fail_timeout=0;
}

改为

upstream backend {
    server 服务IP:3000 fail_timeout=0;
}

upstream streaming {
    # Instruct nginx to send connections to the server with the least number of connections
    # to ensure load is distributed evenly.
    least_conn;

    server 服务IP:4000 fail_timeout=0;
    # Uncomment these lines for load-balancing multiple instances of streaming for scaling,
    # this assumes your running the streaming server on ports 4000, 4001, and 4002:
    # server 127.0.0.1:4001 fail_timeout=0;
    # server 127.0.0.1:4002 fail_timeout=0;
}

允许其他用户遍历 mastodon 用户的主目录,以便 nginx 的用户可以访问 asset 文件,重启nginx,ojbk!

chmod o+x /home/mastodon
systemctl restart nginx


评论