如何使用docker部署mastodon

使用docker部署mastodon可以很大程度上简化部署流程,而且对服务器性能要求似乎会更低。

准备工作

具体流程

服务器配置(使用Ubuntu 20.04)

参照mastodon官方文档进行配置

1.编辑/etc/ssh/sshd_config并查找PasswordAuthentication. 确保它未注释并设置为no. 如果您进行了任何更改,请重新启动 sshd:

1
systemctl restart ssh.service 

2.更新系统包

1
apt update && apt upgrade -y 

3.安装 fail2ban

1
apt install fail2ban 

编辑/etc/fail2ban/jail.local加入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
[DEFAULT]
destemail = your@email.here
sendername = Fail2Ban

[sshd]
enabled = true
port = 22

[sshd-ddos]
enabled = true
port = 22

重启fail2ban

1
systemctl restart fail2ban

4.安装防火墙并仅将 SSH、HTTP 和 HTTPS 端口列入白名单

首先,安装 iptables-persistent。在安装过程中,它会询问您是否要保留当前规则,选择拒绝。

1
apt install -y iptables-persistent

编辑/etc/iptables/rules.v4 加入以下内容

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
 *filter

# Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

# Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

# Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Allow SSH connections
# The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT

手动加载配置

1
iptables-restore < /etc/iptables/rules.v4

5.安装常用命令

1
apt install -y curl wget gnupg apt-transport-https lsb-release ca-certificates

6.安装docker和docker-compose

1
2
3
 bash <(curl -L https://get.docker.com/)
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

部署mastodon

为了便于管理,你可以新建一个用户

1
adduser mastodon

进入该用户的文件夹

1
cd /home/mastodon

为mastodon新建一个文件夹并进入

1
mkdir mastodon & cd mastodon

拉取最新版镜像

1
2
docker pull tootsuite/mastodon:latest
wget https://raw.githubusercontent.com/tootsuite/mastodon/master/docker-compose.yml

修改docker-compose.yml

1
vim docker-compose.yml

! 找到webstreamingsidekiq分类,将其中的image: tootsuite/mastodon全部改成image: tootsuite/mastodon:latest

(latest可以改成其他版本号,这取决于你拉取的镜像版本)

将elasticsearch版本改为7.10.2

保存退出

正式开始部署

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

1
touch .env.production

root用户中执行

1
docker-compose run --rm web bundle exec rake mastodon:setup

然后它会询问一系列问题:

  1. Domain name: (你的域名)

  2. Do you want to enable single user mode? No.(是否使用单人模式,选择“不”)

  3. Are you using Docker to run Mastodon? Yes

    • PostgreSQL host: (这取决于你自己的postgresql地址)
    • PostgreSQL port: 5432
    • Name of PostgreSQL database: 这取决于你自己的postgresql数据库名称)
    • Name of PostgreSQL user: (你的数据库账号)
    • Password of PostgreSQL user: (你的数据库密码)
    • Redis host: mastodon_redis_1

    (注意这里的mastodon,如果在上面你新建的文件夹为其他名字请改为你自己所取的名字)

    • Redis port: 6379
    • Redis password: (无密码直接回车)
  4. Do you want to store uploaded files on the cloud?
    (是否使用对象储存,取决于个人需求,若不使用则媒体文件全部储存在本服务器,否则请配置对象储存,若使用对象储存请填写相关参数)

  5. Do you want to send e-mails from localhost? No
    (填写你的smtp发信资料,我使用了mailgun)

!然后会出现.env.production配置,请先复制下来,进程完成后请编辑.env.production,并加入复制的内容

然后它会问你是否你初始化数据库和创建管理员账户,均填yes

为相应文件夹赋权

1
chown 991:991 -R ./public

启动Mastodon

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

nginx配置

安装nginx

1
sudo apt install nginx -y

配置nginx

进入nginx配置文件目录

1
cd /etc/nginx/conf.d

下载官方配置文件

1
wget https://raw.githubusercontent.com/mastodon/mastodon/main/dist/nginx.conf & mv nginx.conf mastodon.conf

编辑配置文件

1
sudo vim mastodon.conf

将所有example.com改成你自己的域名
/home/mastodon/live/public改成/home/mastodon/mastodon/public

先将listen 443 ssl http2;listen [::]:443 ssl http2以及所有ssl开头的项用#注释掉

保存退出

获取ssl证书(使用cerbot)
安装cerbot

1
2
3
4
apt install snapd
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

获取证书

1
sudo certbot certonly --nginx -d

重新编辑nginx配置文件,将之前的注释内容取消注释
ssl_certificatessl_certificate_key也取消注释

使用nginx -t检查配置是否有误

无误后使用ngnix -s reload重载配置

然后你用你的域名应该就可以打开mastodon了(当然,前提是你设置好了dns)

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

Next Post

正式启用nyaku.moe