Mastodon 站点迁移
配置系统
配置 ssh-key
登入新服务器后进行如下操作:
1 | mkdir -p ~/.ssh |
将生成的ssh-rsa公钥粘贴入其中,随后通过ssh-key密钥方式登录。
修改配置:
1 | nano /etc/ssh/sshd_config |
找到 PasswordAuthentication 一行,将其前面的 #
删掉 (取消注释),在后面将 yes 改成 no。
然后
1 | systemctl restart sshd |
然后开一个新终端测试公钥登录,确认能进再关闭旧会话。
安装常用命令
1 | sudo apt update |
配置防火墙
首先:
1 | sudo ufw allow OpenSSH |
打开防火墙,随后打开 80 和 443 端口:
1 | sudo ufw allow http |
然后可以通过 sudo ufw status
检查防火墙状态,会看到 80 和 443 端口的显示。
安装 docker
和 docker-compose
参考官网教程。
Set up Docker’s apt repository:
1 | # Add Docker's official GPG key: |
Install the Docker packages:
1 | sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin |
check:
1 | docker --version |
The Docker service starts automatically after installation. To verify that Docker is running, use:
1 | sudo systemctl status docker |
Some systems may have this behavior disabled and will require a manual start:
1 | sudo systemctl start docker |
Verify that the installation is successful by running the hello-world image:
1 | sudo docker run hello-world |
拉取 Mastodon 镜像
拉取镜像:
1 | mkdir -p /home/mastodon/mastodon |
下载适配的配置文件,本站仍为 v3.5.19
,下载对应版本:
1 | wget https://raw.githubusercontent.com/mastodon/mastodon/v3.5.19/docker-compose.yml |
打开配置文件:
1 | nano docker-compose.yml |
把 web、streaming、sidekiq 的 image
改为刚才下载的镜像。
配置 Mastodon
直接复制旧服务器上的配置文件:
1 | rsync -avz -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/home/mastodon/mastodon/.env.production ./.env.production |
由于 docker 版本更新,配置文件中的 DB 和 redis 要修改为
1 | DB_HOST=db |
数据库和 Redis 文件
先在新服务器上生成 ssh key,在 /home/mastodon/mastodon/
下执行:
1 | ssh-keygen -t ed25519 -C "mastodon-migration" -f ~/.ssh/id_rsa_migration |
passphrase 回车留空即可。打开旧服务器上的 authorized_keys
:
1 | nano ~/.ssh/authorized_keys |
在文件末尾粘贴新服务器的公钥。
停止旧服务器上的 Mastodon 服务:
1 | docker-compose down |
在新服务器测试能否顺利从旧服务器复制文件,在新服务器运行:
1 | rsync -avzn -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/home/mastodon/mastodon/postgres14 /home/mastodon/mastodon |
这是 dry-run (-n
),只显示会复制哪些文件,不真正传输。确认 dry-run 没问题后,运行:
1 | rsync -avz -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/home/mastodon/mastodon/postgres14 /home/mastodon/mastodon |
文件会传到 /home/mastodon/mastodon/postgres14
和 /home/mastodon/mastodon/redis
。传输完成后,给文件夹赋权:
1 | chown -R 70:70 ./postgres14 |
媒体文件
在旧服务器 Mastodon 用户下执行清理命令,移除远程媒体文件:
1 | docker-compose run --rm -e RAILS_ENV=production web bin/tootctl media remove |
从新服务器复制 public
文件夹:
1 | mkdir -p ./public/system |
赋权:
1 | chown 991:991 -R ./public |
构建用户首页时间流
在新服务器上:
1 | cd /home/mastodon/mastodon |
nginx 配置
在域名网站把域名的 DNS 设置指向的 IP 地址改为新服务器地址。然后安装 nginx:
1 | sudo apt install nginx -y |
从旧服务器直接把之前站点的配置复制过来:
1 | rsync -avz -e "ssh -i ~/.ssh/id_rsa_migration" root@旧服务器IP:/etc/nginx/sites-available/your.domain /home/mastodon |
之后先编辑
1 | sudo nano /etc/nginx/sites-available/your.domain |
在所有包含 ssl 的行前加注释,之后运行 sudo nginx -t
,没有报错之后,使用 systemctl reload nginx
重启 nginx。
配置 SSL 证书:
1 | apt install snapd |
重启 nginx,打开配置文件,将所有刚才加上的注释取消。检查 Certificate 和 Key 的路径是否和刚才生成的路径一致,如不一致,修改为刚才 certbot 给出的路径。使用 nginx -t
检查没有错误,重启 nginx。
通过 certbot renew --dry-run
检查证书是否能自动更新。
开启全文搜索
参考:如何利用Docker搭建Mastodon实例(一):基础搭建篇 - 开启全文搜索
小结
刚迁移完外站网友头像为空,但会慢慢显示。这是由于没有把旧服务器的 cache 复制过来,所以外站的媒体存储全部都要依靠服务器一点一点和外站服务器交互再拉过来,图片类的信息都很慢。
主要参考: