Mastodon ,一个个人的微博类 社交网络 站点,可以作为发布消息的平台,也许没几个人关注你的动态,但另有妙用……
我把它作为记录日记的地方,每天发表一下流水账,可以将来回顾。除此之外,一些图文消息、视频我也会通过 mastodon 发表,然后分享网址给朋友,这样能让聊天队列更干净(微信烂的缘故)。
以后若有合适的微信机器人项目,可以将公开的嘟文自动转发到微信聊天里,这样就更方便了。
适用系统:Debian 11/12,Ubuntu 按流程稍改命令也可。其他发行版不确定。
走通预计时间:40 分钟,CPU 性能差的话会更久。
GitHub: mastodon/mastodon: Your self-hosted, globally interconnected microblogging community (github.com)
官网: Mastodon - Decentralized social media (joinmastodon.org)
如果你按照本文部署,那一定不能错过相应的维护文章: Mastodon 备份、更新和迁移的全面指南 - 技焉洲 (vfly2.com)
服务端
部署自己的实例站点,需要以下硬性条件:
- 一个域名,AhFei 使用的域名是 social.vfly2.com 。
- 机器内存不能低于 1 GB
- 需要能发送电子邮件的 SMTP Service(AhFei 用的是 Gmail 的,好开启,也能发往国内邮箱)
另外需要注意的是,无法迁移域名,无法迁移嘟文。如果你像我一样,曾经用要抛弃的域名练手,然后发了上百篇嘟文,如今要换一个域名,那很抓狂的是,只能从头建一个实例。
具体说,如果将来你要换域名,无法简单的更改配置文件里的域名实现,实际上,这是完全无法实现的,因为 ActivityPub 协议的原因。(域名是辨别用户身份的依据,更改域名,就意味着,那个用户已经不存在了)
安装 Mastodon
官网教程: Installing from source - Mastodon documentation (joinmastodon.org)
中文版本的教程可能不会及时更新,推荐看英文的。
前置条件
以 root 运行: sudo -i
先安装一些工具
apt update && \
apt install -y curl wget gnupg apt-transport-https lsb-release ca-certificates
导入 Node.js GPG 密钥,添加相应的 deb 软件包源,方便 apt 安装。先选择 Node 安装的版本
NODE_MAJOR=20
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg # 导入 Nodesource GPG key
# Create deb repository
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
为 PostgreSQL 添加 deb 软件包源
wget -O /usr/share/keyrings/postgresql.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc
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
安装软件
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 libgdbm-dev \
nginx redis-server redis-tools postgresql postgresql-contrib \
certbot python3-certbot-nginx libidn11-dev libicu-dev libjemalloc-dev
安装 Yarn,一个 Node.js 包管理器。
corepack enable
yarn set version classic
安装 Ruby
使用 rbenv 来管理 Ruby 版本。 它必须为单个 Linux 用户安装,因此,先创建 mastodon 用户:
useradd -m -s /bin/bash mastodon
设置强密码
passwd 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 版本:(有需要,可以修改后面的版本号 3.2.2 )
RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.2.2
设置默认的 Ruby 版本
rbenv global 3.2.2
还需要安装 Ruby 依赖管理器 bundler:
gem install bundler --no-document
按 CTRL + D 返回 root 用户
设置 PostgreSQL
创建 Mastodon 的 PostgreSQL 用户。
最简单的方法是使用“ident”身份验证,这种方法,PostgreSQL 用户没有单独的密码,可以由相同用户名的 Linux 用户使用。
sudo -u postgres psql
在提示符下执行:
CREATE USER mastodon CREATEDB;
退出
\q
安装 Mastodon
切换到mastodon用户:
su - mastodon
使用 git 下载 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)
注意,此时目录为
/home/mastodon/live
,以后涉及 mastodon 的操作,比如更新,都需要在这个目录下运行命令。
现在安装 Ruby 和 JavaScript 依赖项:
# 仅在第一次安装依赖项时才需要这两个 bundle config 命令
bundle config deployment 'true'
bundle config without 'development test'
# 更新或重新安装依赖项,只需 bundle install 就足够了
bundle install -j$(getconf _NPROCESSORS_ONLN)
yarn install --pure-lockfile
生成 Mastodon 配置
运行交互式设置向导,这会:创建配置文件,运行预编译,创建数据库架构
RAILS_ENV=production bundle exec rake mastodon:setup
配置文件保存为 .env.production
。
按 CTRL + D 返回 root 用户
设置 Nginx
先获取证书,记得修改命令中的域名。以 root 用户运行。
创建放证书的目录
mkdir -p /etc/letsencrypt/live/social.vfly2.com/
申请证书(certbot 在前置准备里装上了,Nginx 也是)
certbot --nginx -d social.vfly2.com
需要输入邮箱,以及填两个 Yes。
申请的证书会自动更新,位置在:
- Certificate is saved at:
/etc/letsencrypt/live/social.vfly2.com/fullchain.pem
- Key is saved at:
/etc/letsencrypt/live/social.vfly2.com/privkey.pem
不启用 default 配置文件,因为上面证书申请后,会自动加入到 default 中
rm /etc/nginx/sites-enabled/default
复制 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
编辑
vim /etc/nginx/sites-available/mastodon
- 替换
example.com
为你的域名, - 取消注释
ssl_certificate
和ssl_certificate_key
,并修改后面的路径,记得路径后要有分号;
重启生效
systemctl reload nginx
Systemd 守护进程
复制 mastodon 示例 Systemd 配置文件
cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
启动 mastodon
systemctl daemon-reload
systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming
可以看到,mastodon 由三部分组成,mastodon-web、mastodon-sidekiq、mastodon-streaming
到此全部安装完毕,访问域名使用 Mastodon 吧!
原文链接: https://technique.vfly2.com/2023/11/mastodon-native-installation-process/
版权声明:本博客所有文章除特別声明外,均为 AhFei 原创,采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 技焉洲 (technique.vfly2.com) 。
保持更新 ٩(•̤̀ᵕ•̤́๑)ᵒᵏᵎᵎᵎᵎ 清晰恒益的实用技能,欢迎使用 RSS 订阅,或在支持 ActivityPub 的平台关注 @vfly2tech@technique.vfly2.com
接收新文章的推送,如果能留言互动就更好了。