Mastodon长毛象搭建教程

引用文章:

Mastodon

安装Docker

  • Docker要求Centos内核高于3.10,我们通过 uname -r 命令查看当前内核版本是否满足条件

  • 执行sudo yum update升级yum包

  • 安装必要软件

    1
    sudo yum install -y yum-utils device-mapper-persistent-date lvm2
  • 设置yum源

    1
    sudo yum-config-manager --add-repo https:``//download.docker.com/linux/centos/docker-ce.repo
  • 查看当前仓库中docker版本

    1
    yum list docker-ce --showduplicates | ``sort` `-r
  • 安装相关版本,安装稳定版本17.12.1

    1
    2
    sudo yum install docker-ce-17.12.1.ce

  • 加入开机启动

    1
    2
    sudo systemctl start docker``sudo systemctl enable docker

  • 查看Docker版本验证是否安装成功,运行docker version只要有 clientservice 两部分则表示 docker 安装启动都成功了)。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    [root@VM-0-2-centos ~]# docker version
    Client: Docker Engine - Community
    Version: 20.10.21
    API version: 1.41
    Go version: go1.18.7
    Git commit: baeda1f
    Built: Tue Oct 25 18:04:24 2022
    OS/Arch: linux/amd64
    Context: default
    Experimental: true

    Server: Docker Engine - Community
    Engine:
    Version: 20.10.9
    API version: 1.41 (minimum version 1.12)
    Go version: go1.16.8
    Git commit: 79ea9d3
    Built: Mon Oct 4 16:06:37 2021
    OS/Arch: linux/amd64
    Experimental: false
    containerd:
    Version: 1.6.10
    GitCommit: 770bd0108c32f3fb5c73ae1264f7e503fe7b2661
    runc:
    Version: 1.1.4
    GitCommit: v1.1.4-0-g5fd4c4d
    docker-init:
    Version: 0.19.0
    GitCommit: de40ad0
    [root@VM-0-2-centos ~]#

安装Docker-Compose

  • 方法很多,这里用的pip安装

    1
    yum -y install python3-pip
    1
    pip3 install --upgrade pip
  • s执行安装命令

    1
    pip3 install docker-compose
  • 查看docker-compose版本

    1
    docker-compose version

下载Mostodon的Dokcer镜像文件

  • 创建安装目录并拉取镜像

    1
    2
    3
    mkdir -p /home/mastodon/mastodon
    cd /home/mastodon/mastodon
    docker pull tootsuite/mastodon:latest #如果需要升级到某稳定版本,请将latest改成v3.5.1等版本号。
  • 下载配置文件

    1
    wget https://raw.githubusercontent.com/tootsuite/mastodon/master/docker-compose.yml
  • 修改配置文件用文本编辑器或者vim都行

    1
    vim docker-compose.yml

    依次找到webstreamingsidekiq分类,在每一类的image: tootsuite/mastodon后添加:latest或者你刚才拉取的版本号,变成image: tootsuite/mastodon:latestimage: tootsuite/mastodon:v3.5.1等等。

    此外,官方的docker-compose.yml文件里写的是7.17.4,但目前elasticsearch的最高版本为7.10.2,需要修改。
    :wq保存退出

初始化PostgreSQL数据库

(在v3.5.0以后,请注意Postgres文件夹所在位置有所修改,从原本的postgres改为了postgres14。本教程已更新。)

刚才docker-compose.yml文件中,数据库(db)部分的地址为./postgres14:/var/lib/postgresql/data,因此你的数据库绝对地址为/home/mastodon/mastodon/postgres14

运行:

1
2
docker run --name postgres14 -v /home/mastodon/mastodon/postgres14:/var/lib/postgresql/data -e 
POSTGRES_PASSWORD=设置数据库管理员密码 --rm -d postgres:14-alpine

执行完后,使用ls检查/home/mastodon/mastodon/postgres14,应该出现postgres相关的多个文件,不是空文件夹。

然后执行:

1
docker exec -it postgres14 psql -U postgres

输入:

1
CREATE USER mastodon WITH PASSWORD '数据库密码(最好和数据库管理员密码不一样)' CREATEDB;

创建mastodon用户

1
\q

退出数据库,停止Docker

1
docker stop postgres14

修改Mastodon配置

  • 配置文件

    在*/home/mastodon/mastodon* 文件夹中创建空白的 .env.production 文件

    1
    touch .env.production

    这个文件是隐藏的

使用root用户运行,下述命令进行配置

1
docker-compose run --rm web bundle exec rake mastodon:setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Domain name: 你的域名

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? Yes

PostgreSQL host: mastodon_db_1
PostgreSQL port: 5432
Name of PostgreSQL database: mastodon
Name of PostgreSQL user: mastodon
Password of PostgreSQL user: (这里写上面你给mastodon设置的your_passwords)
Database configuration works! 🎆

Redis host: mastodon_redis_1
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? No
SMTP server: smtp.zoho.eu
SMTP port: 587
SMTP username: 你的zoho管理员邮箱地址
SMTP password: 你的zoho管理员密码
SMTP authentication: plain
SMTP OpenSSL verify mode: none
E-mail address to send e-mails "from": 你的zoho管理员邮箱地址
Send a test e-mail with this configuration right now? no

This configuration will be written to .env.production
Save configuration? Yes
Below is your configuration, save it to an .env.production file outside Docker:

接下来会出现**.env.production**的配置,请复制保存下来。后面两个选项是建立数据及编译,都选yes

进入**.env.production** 把刚刚的配置复制进去

更新:在4.0.2版本,需要在.env.production中增加一行:

1
REDIS_URL=redis://@mastodon_redis_1:6379

ps:我用的mastodon貌似没增加,也没报错,不知道啥缘故有空检查

  • 为文件夹赋权

    1
    2
    chown 991:991 -R ./public
    chown -R 70:70 ./postgres14
  • 启动Mastodon

    1
    2
    docker-compose down
    docker-compose up -d

开放端口,解析域名

这个 步骤略过,懒得截图贴图

配置Nginx代理设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {
listen 80;
#listen [::]:80;
server_name lcy.moe;
return 301 https://$host$request_uri;
}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name lcy.moe;
## SSL
ssl_certificate /etc/nginx/ssl/lcy.moe.crt;
ssl_certificate_key /etc/nginx/ssl/lcy.moe.key;

## reverse proxy

location / {
proxy_pass http://127.0.0.1:3000;
include conf.d/shared/proxy.conf;
add_header Strict-Transport-Security "max-age=31536000;includeSubDomains;preload" always;
}
location /api/v1/streaming/ {
proxy_pass http://127.0.0.1:4000/;
include conf.d/shared/proxy.conf;
proxy_buffering off;
proxy_redirect off;
tcp_nodelay on;
}

}

其中conf.d/shared/proxy.conf为手动创建的通用的代理配置,放置在/etc/nginx/conf.d/shared/proxy.conf,内容如下:

1
2
3
4
5
6
7
8
9
10
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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 X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;

此处仅供参考

我的Nginx的配置是单独设置的,是将不同服务的server块保存为不同的conf配置文件,避免一个服务修改出错全部服务挂掉。

1
2
[root@VM-0-2-centos conf]# cd /usr/local/nginx/conf
[root@VM-0-2-centos conf]# vim /usr/local/nginx/conf/nginx.conf
1
2
3
4
http {
include mime.types;
default_type application/octet-stream;
include /www/vhost/*.conf;

在 http块中添加 include /www/vhost/*.conf; 即可,然后在对应文件夹创建对应配置文件进行设置,此处贴一下我的MastodonNginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    server {
listen 80;
listen 443 ssl;
server_name mms.yeatsj.com;

#charset koi8-r;

#access_log logs/host.access.log main;
ssl_certificate /www/ssl_cert/mastodon.pem;
ssl_certificate_key /www/ssl_cert/mastodon.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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 X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
add_header Strict-Transport-Security "max-age=31536000;includeSubDomains;preload" always;
}
location /api/v1/streaming/ {
proxy_pass http://127.0.0.1:4000/;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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 X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_buffering off;
proxy_redirect off;
tcp_nodelay on;
}
}

其实中间也遇到了其它一些bug,但是懒得去了,按照这个教程基本上可以完成Mastodon的搭建,欢迎来到我的Mastodon注册玩耍