在Ubuntu 20.04上安装Mastodon

Mastodon(又称长毛象或万象)是一个免费开源的去中心化的分布式微博客社交网络。它的用户界面和操作方式跟推特和微博类似,但是整个网络并非由单一机构运作,却是由多个由不同营运者独立运作的服务器以联邦方式交换数据而组成的去中心化社交网络。每个Mastodon的营运站点被称为“实例(Instance)”,用户可到任何开放登记的实体登记,任何一个实体上的用户可以与其他实体上的用户沟通。用户在推特中发布的内容称为“推文”,而在Mastodon中发布的内容则称为“嘟文(Toot)”,用户可以调整隐私设置限制嘟文被其他人或实体读取或查看。

最近注意到有一个使用ActivityPub协议的去中心化个人社交媒体系统,准备搭建一个,搜索了一下资料,也自己实践了一下,虽然中文社区都建议使用docker建立,而且我用docker开起来了一个,但对整体的架构都不太熟悉,还是希望在正式服务上使用从源安装的方案,经过测试后发现官方的文档和目前的配置有一定的出入,因为完全按照官方的命令,并不能让程序完全运行起来,所以在这里记一下。

首先需要准备

  1. 域名
  2. 主机(内存不小于2GB,1GB的话可以考虑Misskey

安装Node.js

Ubuntu:

curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs

Debian:

curl -fsSL https://deb.nodesource.com/setup_12.x | bash -
apt-get install -y nodejs

安装Yarn

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

安装相关软件包

apt update
apt install -y \
  imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core \
  g++ libprotobuf-dev protobuf-compiler pkg-config nodejs gcc autoconf \
  bison build-essential libssl-dev libyaml-dev libreadline6-dev \
  zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev \
  nginx redis-server redis-tools postgresql postgresql-contrib \
  certbot python3-certbot-nginx yarn libidn11-dev libicu-dev libjemalloc-dev

安装 Ruby

因为使用 rbenv 可以更容易的获得正确的版本并在新版本发布后进行更新,我们将使用 rbenv 来管理Ruby版本。rbenv 必须安装在单个Linux用户中,因此,我们首先需要使用以下命令创建一个Mastodon用户:

adduser --disabled-login mastodon

切换到mastodon用户:

su - mastodon

执行以下步骤安装 rbenv 和 rbenv-build:

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
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/plugins/ruby-build

上述操作完成,我们便可以安装正确的 Ruby 版本:

RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.7.4
rbenv global 2.7.4

如果在后续安装的时候提示 rbenv: version '1.2.3' is not installed (set by /home/mastodon/live/.ruby-version) 尝试将rbenv 的版本切换为软件需要的版本

我们同样需要安装 bundler:

gem install bundler --no-document

返回root用户:

exit

配置 PostgreSQL

安装PostgreSQL

由于Ubuntu自带的库里已经有postgersql 12了,所以直接安装即可

apt-get install postgresql-12 

如果需要postgresql 14 可以执行下面的步骤

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql

创建帐户

你需创建一个供Mastodon使用的PostgreSQL帐户。创建一个使用“ident”认证方式的帐户是最容易的配置方法,即PostgreSQL帐户不需要独立的密码并由同名Linux用户使用。

sudo -u postgres psql

在控制台中执行:

CREATE USER mastodon CREATEDB;
\q

配置 Mastodon

现在该下载Mastodon代码了。切换至mastodon用户:

su - mastodon

检出代码

使用git下载最新稳定版Mastodon:

git clone https://github.com/tootsuite/mastodon.git live && cd live
git checkout $(git tag -l | grep -v 'rc[0-9]*

#039; | sort -V | tail -n 1)

安装依赖

现在,安装Ruby和JavaScript依赖:

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

两个bundle config命令仅仅第一次安装依赖时需要。如果你之后进行升级或重安装依赖,只需要bundle install就够了。

生成配置文件

运行交互式安装向导:

RAILS_ENV=production bundle exec rake mastodon:setup

生成配置文件生成器执行下面的三个步骤

  1. 创建一个配置文件
  2. 预编译静态文件
  3. 创建数据库schema

配置的问题范例:

Your instance is identified by its domain name. Changing it afterward will break things.
Domain name: example.com

Single user mode disables registrations and redirects the landing page to your public profile.
Do you want to enable single user mode? No

Are you using Docker to run Mastodon? no

PostgreSQL host: /var/run/postgresql
PostgreSQL port: 5432
Name of PostgreSQL database: mastodon
Name of PostgreSQL user: mastodon
Password of PostgreSQL user: 
Database configuration works! 🎆

Redis host: localhost
Redis port: 6379
Redis password: 
Redis configuration works! 🎆

Do you want to store uploaded files on the cloud? No

Do you want to send e-mails from localhost? yes
E-mail address to send e-mails "from": "Mastodon <[email protected]>"
Send a test e-mail with this configuration right now? no

This configuration will be written to .env.production
Save configuration? Yes

Now that configuration is saved, the database schema must be loaded.

安装完毕后从mastodon账户中退出,切换到root中

exit

配置 nginx

首先你需要先安装nginx

sudo apt install nginx

从Mastodon目录复制配置文件模版到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

编辑 /etc/nginx/sites-available/mastodon,替换 example.com 为你自己的域名,你可以根据自己的需求做出其它的一些调整。
重载 nginx 以使变更生效:

systemctl reload nginx.service

这时候会报错,我们暂时不用管

获取SSL证书

我们将使用 Let’s Encrypt 获取一个免费的SSL证书:
官方给的指令是让nginx自动更新,但官方的默认nginx配置无法根据下面的指令获取证书

certbot --nginx -d example.com

我们需要先手动获取证书

systemctl stop nginx //停止nginx,让certbot能够占用80端口
certbot certonly --standalone -d example.com  //获取证书

获取证书也可以用下面的指令,非交互式一句话

certbot certonly --standalone -n --agree-tos --email [email protected] --preferred-challenges http -d example.com
  • -n 非交互式
  • --email 指定账户
  • --agree-tos 同意服务协议

如果成功的话,证书文件应该在下面的目录里,将这两个目录地址记下来,后面修改nginx配置的时候会用到:

/etc/letsencrypt/live/example.com/fullchain.pem
/etc/letsencrypt/live/example.com/privkey.pem

现在你应该配置certbot自动续约证书

crontab -e

在输入框中写入计划任务

0 0 * * * /usr/bin/certbot renew --quiet

这样配置好了后,certbot会在每天的0点检查证书是否过期,如果过期就自动续约证书。

接着再次修改nginx的配置

vim /etc/nginx/sites-available/mastodon

将上面的证书路径填入到ssl的部分

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

再次启动nginx

systemctl start nginx

现在你应该能够通过浏览器访问你的域名,然后看到一只大象锤击电脑屏幕的错误页面。这是因为我们还没有启动Mastodon进程。

配置 systemd 服务

从Mastodon目录复制systemd服务模版:

su root
cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/

然后编辑文件以确保用户名和路径正确:

  • /etc/systemd/system/mastodon-web.service
  • /etc/systemd/system/mastodon-sidekiq.service
  • /etc/systemd/system/mastodon-streaming.service

最后,启动并启用新的systemd服务:

systemctl daemon-reload
systemctl start mastodon-web mastodon-sidekiq mastodon-streaming

可以检查一下服务是否正常运行

systemctl status mastodon-web.service
systemctl status mastodon-sidekiq.service
systemctl status mastodon-streaming.service

如果一切正常的话,将服务设置为开机自启动

systemctl enable mastodon-web mastodon-sidekiq mastodon-streaming

现在你应该可以通过浏览器看到你的mastodon主页啦!


由于之前配置的Sendmail效果不是太好,所以后面将邮件服务改成由飞书的smtp发送,分享一下配置,也顺便备注一下

SMTP_SERVER=smtp.feishu.cn
SMTP_PORT=465
SMTP_LOGIN=你在飞书绑定的邮箱
SMTP_PASSWORD=飞书给的SMTP密码
SMTP_AUTH_METHOD=login
SMTP_OPENSSL_VERIFY_MODE=none
# Can be: 'none', 'peer', 'client_once', 'fail_if_no_peer_cert', see http://api.rubyonrails.org/classes/ActionMailer/Base.html
SMTP_TLS=ssl
SMTP_ENABLE_STARTTLS_AUTO = true
SMTP_FROM_ADDRESS= ' Mastodon <你在飞书绑定的邮箱> '

参考相关:官方文档 Rin404° 羊毛 于光年外遥望

暂无评论

发送评论 编辑评论


				
上一篇
下一篇