使用docker部署mastodon可以很大程度上简化部署流程,而且对服务器性能要求似乎会更低。
准备工作
- 一台服务器(我使用的是1核1G的服务器,建议不要学我)
- ssh客户端
- postgresql服务器(我使用的是Azure的学生版)
我为了方便迁移(删除实例)选择将数据库分开部署 - mailgun
具体流程
服务器配置(使用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 | [DEFAULT] |
重启fail2ban
1 | systemctl restart fail2ban |
4.安装防火墙并仅将 SSH、HTTP 和 HTTPS 端口列入白名单
首先,安装 iptables-persistent。在安装过程中,它会询问您是否要保留当前规则,选择拒绝。
1 | apt install -y iptables-persistent |
编辑/etc/iptables/rules.v4 加入以下内容
1 | *filter |
手动加载配置
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 | bash <(curl -L https://get.docker.com/) |
部署mastodon
为了便于管理,你可以新建一个用户
1 | adduser mastodon |
进入该用户的文件夹
1 | cd /home/mastodon |
为mastodon新建一个文件夹并进入
1 | mkdir mastodon & cd mastodon |
拉取最新版镜像
1 | docker pull tootsuite/mastodon:latest |
修改docker-compose.yml
1 | vim docker-compose.yml |
! 找到web、streaming、sidekiq分类,将其中的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 |
然后它会询问一系列问题:
-
Domain name: (你的域名)
-
Do you want to enable single user mode? No.(是否使用单人模式,选择“不”)
-
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: (无密码直接回车)
-
Do you want to store uploaded files on the cloud?
(是否使用对象储存,取决于个人需求,若不使用则媒体文件全部储存在本服务器,否则请配置对象储存,若使用对象储存请填写相关参数) -
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 | docker-compose down |
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 | apt install snapd |
获取证书
1 | sudo certbot certonly --nginx -d |
重新编辑nginx配置文件,将之前的注释内容取消注释
将ssl_certificate和ssl_certificate_key也取消注释
使用nginx -t检查配置是否有误
无误后使用ngnix -s reload重载配置
然后你用你的域名应该就可以打开mastodon了(当然,前提是你设置好了dns)
