由flymc.cc驱动的Mastodon社区(已经下线并改成了misskey https://pari.cafe/)已经上线啦~

很久之前尝试搭建过Mastodon实例,由于官方中英文档都多多少少有点问题,一直没能成功。

这两天又摸索了一下,参照着更新的官方文档(屑文档仍然有一些问题orz)又查了一堆资料,终于是搭好了XD

我的Mastodon实例配置:E5 16核心,16GiB内存 , 100G SSD,GTX970(不,闲的没事加的,客厅云服务器

实例推荐的基本配置:1-2核心+2GiB内存,30GiB以上SSD(HDD也ok其实)

配置当然越高越好,较低的配置不建议托管大量用户以及订阅过多中继站

本文档适用于Mastodon当前版本v4.0.2(https://github.com/mastodon/mastodon/releases/tag/v4.0.2)

随着后续新版本的发布,本文档会按需更新。

准备实例

本文档中所有操作适用于Ubuntu Server 22.04 LTS,推荐在全新安装无任何环境的系统下进行。

其它系统请按需修改。

Mastodon为Ruby程序,需要准备的环境有:Ruby(3.0.2/3.0.4), Node.js 16或者Node.js14(不推荐)(注意,不是越新越好,Node.js 18及以上版本可能会造成资源文件编译失败)

准备好实例后,执行

apt update && apt upgrade -y
apt install curl wget gnupg lsb-release ca-certificates git software-properties-common make apt-transport-https redis-server optipng pngquant jhead jpegoptim gifsicle imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file 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 libidn11-dev libicu-dev libjemalloc-dev -y

准备环境

安装Node.js:

执行

curl -sL https://deb.nodesource.com/setup_16.x | bash -

Repository添加成功后,执行

apt install nodejs -y

Yarn:

下载并添加Yarn GPG Key与Respository

执行

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | tee /etc/apt/sources.list.d/yarn.list

安装Yarn

apt update -y
apt -y install yarn

PostgreSQL:

执行

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

安装PostgreSQL 15

apt update -y
apt install -y postgresql postgresql-contrib

安装完成后,创建一个新的数据库

执行

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

数据库配置完成。

如果想要生成合适的数据库配置文件,请前往https://pgtune.leopard.in.ua/生成(数据库版本为15),并添加生成的配置文件到/etc/postgresql/15/main/postgresql.conf末尾,保存后执行一次systemctl restart postgresql

安装Ruby

执行

apt install -y ruby ruby-dev

安装完成后,输入 ruby -v 以检查安装情况,应返回:

ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux-gnu]

安装和配置Mastodon

执行

adduser mastodon --system --group --disabled-login
git clone https://github.com/tootsuite/mastodon.git


mkdir -p /var/www/
mv mastodon/ /var/www/
chown -R mastodon:mastodon /var/www/mastodon/

cd /var/www/mastodon/
sudo -u mastodon git checkout v4.0.2

执行最后一步时,得到下面输出:

Note: switching to 'v4.0.2'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c 

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 03b0f3ac8 Bump version to 4.0.2 (#20725)

安装Mastodon依赖:

gem install bundler
sudo -u mastodon bundle config deployment 'true'
sudo -u mastodon bundle config without 'development test'
sudo -u mastodon bundle install -j$(getconf _NPROCESSORS_ONLN)

开始配置和安装Mastodon:

sudo -u mastodon RAILS_ENV=production bundle exec rake mastodon:setup

接下来根据指示,依次输入信息:

Domain name 域名,只能一个,为Mastodon的访问域名
single user mode单用户模式(建议no)
Do you want to enable single user mode? No 是否使用Docker (根据具体情况,建议no)

PostgreSQL数据库配置:
PostgreSQL host: /var/run/postgresql
PostgreSQL port: 5432
Name of PostgreSQL database: mastodon
Name of PostgreSQL user: mastodon
Password of PostgreSQL user:
(密码为空)

Redis配置:
Redis host: localhost
Redis port: 6379
Redis password:
(密码同样为空)

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": (根据自己情况设置)
Send a test e-mail with this configuration right now? no

完成配置:
This configuration will be written to .env.production Save configuration? (Y/n) Y

准备数据库
Now that configuration is saved, the database schema must be loaded. If the database already exists, this will erase its contents. Prepare the database now? (Y/n) Y

预编译CSS/JS
The final step is compiling CSS/JS assets. This may take a while and consume a lot of RAM. Compile the assets now? (Y/n) Y


All done! You can now power on the Mastodon server ????

创建管理员账户
Do you want to create an admin user straight away? Yes
Username: admin
E-mail: [email protected]
You can login with the password: 9835fbd5e569ad149610862178580da7
You can change your password once you login.

将Mastodon安装为服务

执行:

cp /var/www/mastodon/dist/mastodon*.service /etc/systemd/system/
sed -i 's/home\/mastodon\/live/var\/www\/mastodon/g' /etc/systemd/system/mastodon-*.service
sed -i 's/home\/mastodon\/.rbenv\/shims/usr\/local\/bin/g' /etc/systemd/system/mastodon-*.service
systemctl daemon-reload
systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming

Mastodon配置完成,默认监听端口3000

nginx配置可自行反代,这里提供一个配置模板(宝塔面板配置nginx站点会导致不能与大部分站点互通,建议复制以下配置):

自行修改

server {
    listen 80;
    server_name pari.cafe;
    root /www/wwwroot/pari.cafe;

    location / {
        return 301 https://$server_name$request_uri;
    }

    location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }

    location ~ \.well-known{
        allow all;
    }

    if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {
        return 403;
    }

    access_log  /www/wwwlogs/pari.cafe.log;
    error_log  /www/wwwlogs/pari.cafe.error.log;
}

server {
	listen 443 ssl http2;
    server_name pari.cafe;

    ssl_certificate    /www/server/panel/vhost/cert/pari.cafe/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/pari.cafe/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497 https://$host$request_uri;
	#SSL-END


    #PROXY-START/

    location / {
        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 REMOTE-HOST $remote_addr;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_http_version 1.1;
        # proxy_hide_header Upgrade;

        add_header X-Cache $upstream_cache_status;


    #PROXY-END/

    location ~ /purge(/.*) {
        proxy_cache_purge cache_one $host$1$is_args$args;
        #access_log  /www/wwwlogs/pari.cafe_purge_cache.log;
    }


    access_log  /www/wwwlogs/pari.cafe.log;
    error_log  /www/wwwlogs/pari.cafe.error.log;
}
最后更新于 2023-11-16