mastodon实例部署记录
此教程只用于ubantu 18,这个记录基于官方文档,如果出现版本问题请对照官方文档!
前提条件
- 一台你有root访问权限的运行 Ubuntu 18.04 的机器
- 一个用于Mastodon站点的域名(或一个子域名),例如:example.com
- 一个电子邮件发送服务提供商,或其他SMTP服务器(我这里用的是华为的企业邮箱)
环境准备
如果因为某些原因需要启用root用户,需要赋予root一个密码
sudo passwd root
如果是自己在家搭建的话要换源(官方源少了东西坑的要死)
vi /etc/apt/sources.list
清空然后插入
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
更新
apt-get update
修改host
echo 140.82.114.4 github.com >> /etc/hosts
echo 151.101.1.227 rubygems.org >> /etc/hosts
安装Node.js
curl -sL https://deb.nodesource.com/setup_16.x | bash -
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 libgdbm5 libgdbm-dev nginx redis-server redis-tools postgresql postgresql-contrib certbot python-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 3.0.3
rbenv global 3.0.3
我们同样需要安装 bundler:
gem install bundler --no-document
返回root用户
配置 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]*$' | 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
运行交互式安装向导(按照实际情况来,可以后面改的不怕,看不懂可以用翻译软件。):
RAILS_ENV=production bundle exec rake mastodon:setup
你已经完成需使用mastodon用户进行的操作,请切换回root用户
配置 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 为你自己的域名,你可以根据自己的需求做出其它的一些调整。
vi /etc/nginx/sites-available/mastodon
我们将使用 Let’s Encrypt 获取一个免费的SSL证书(修改为自己的域名):
certbot --nginx -d example.com
重启nginx
systemctl restart nginx
设置 systemd 服务
从 Mastodon 目录中复制 systemd 服务模板:
cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
如果您在任何时候偏离了默认值,请检查用户名和路径是否正确:
$EDITOR /etc/systemd/system/mastodon-*.service
最后,启动并启用新的 systemd 服务:
systemctl daemon-reload
systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming
然后访问自己的域名就可以了(我的:ssdcc.online - SSDC)

浙公网安备 33010602011771号