更新系统
apt -y update
apt -y dist-upgrade
安装docker和docker-compose
不知道怎么安装,点击下面链接,按教程走。
https://blog.1688.al/archives/dockercomposeinstall
安装Mastodon 前准备工作
添加Node.js/Yarn的软件源
apt -y install curl build-essential
curl -sL https://deb.nodesource.com/setup_10.x | bash -
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
创建mastodon要存放路径目录
mkdir -p /root/docker/docker-compose/mastodon/ && cd /root/docker/docker-compose/mastodon/
拉取 Mastodon 仓库docker-compose.yml
wget https://raw.githubusercontent.com/mastodon/mastodon/main/docker-compose.yml
按需求更改docker-compose.yml
nano docker-compose.yml
1.下载的docker-compose.yml 在web, streaming, sidekiq这三项默认是编译,我们不在编译直接使用官方镜像就可以,在这三项的build: .前面加个#注释掉。(要使用编译也是可以)
2.修改这三个你想要的镜像版本,如果 image: ghcr.io/mastodon/mastodon:v4.2.9 ,或者保持最新版本 image: ghcr.io/mastodon/mastodon:latest
3.需要全文搜索功能,则将 es 服务整块取消注释。
注:
db是Postgres数据库
es是Elasticsearch,全文搜索引擎,这是可选地
streaming组件是 Mastodon 的推送服务
sidekiq是 Mastodon 的后台任务处理队列
初始化PostgreSQL
docker run --name postgres14 -v /root/docker/docker-compose/mastodon/postgres14:/var/lib/postgresql/data -e POSTGRES_PASSWORD=设置数据库管理员密码 --rm -d postgres:14-alpine
进入postgres14容器内部
docker exec -it postgres14 psql -U postgres
创建用户和密码(这密码建议和上面数据库管理员密码一致,以免混淆),mastodon是这边设置的用户
CREATE USER mastodon WITH PASSWORD '设置数据库密码' CREATEDB;
退出容器
\q
停止postgres14实例
docker stop postgres14
创建空白.env.production文件:
touch .env.production
开始配置 Mastodon
docker compose run --rm web bundle exec rake mastodon:setup
以下是配置填写项目
Domain name: 填写你要搭建的Mastodon的域名
Do you want to enable single user mode? (y/N) 直接回车
Are you using Docker to run Mastodon? 直接回车
PostgreSQL host: (db) 填写mastodon_db_1
PostgreSQL port: (5432) 5432
Name of PostgreSQL database: (postgres) 填写mastodon
Name of PostgreSQL user: (postgres) 填写mastodon
Password of PostgreSQL user: 你上面设置的数据库密码
Redis host: (redis) mastodon_redis_1
Redis port: 6379
Redis password: 直接回车
Do you want to store uploaded files on the cloud? (y/N) 直接回车
Do you want to send e-mails from localhost? (y/N) 直接回车
SMTP server: wednesday.mxrouting.net (我用的是這家的,填寫你自己的)
SMTP port: 587
SMTP username:
SMTP password:
SMTP authentication: (plain) 回车
SMTP OpenSSL verify mode: (Press ↑/↓ arrow to move and Enter to select)
‣ none
peer
client_once
fail_if_no_peer_cert 回车2次
E-mail address to send e-mails “from”: 給用戶註冊發送的郵件通知信箱
Send a test e-mail with this configuration right now? (Y/n) 郵箱發送測試,自行選擇。
Do you want Mastodon to periodically check for important updates and notify you? (Recommended) (Y/n) 直接回车
This configuration will be written to .env.production
Save configuration? (Y/n) 回车
回车后上面会显示所有配置信息,把配置信息全部复制下来,一会要填入到
.env.production 的对应条列中。
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) 回车
Do you want to create an admin user straight away? (Y/n) 选择NO
注意!!!因为创建管理员目前有bug,所以先不创建,等所有搭建完成后通过tootctl工具建立。
现在生成.env.production所需的信息,复制保存下来。
把上面获取的信息填入.env.production内
nano .env.production
至此所有配置都完成
重新进行docker-compose up -d
docker-compose down
docker-compose up -d
public目录写入/读取权限
sudo chown -R 991:991 ./public
chown -R 70:70 ./postgres14
重新启动mastodon
docker-compose down
docker-compose up -d
Nginx 配置
安装Nginx
apt install nginx -y
下载mastodon nginx.conf文件
wget https://raw.githubusercontent.com/mastodon/mastodon/main/dist/nginx.conf
把nginx.conf 复制到Nginx
cp /root/docker/docker-compose/mastodon/dist/nginx.conf /etc/nginx/sites-available/mastodon
ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon
编辑/etc/nginx/sites-available/mastodon文件
nano /etc/nginx/sites-available/mastodon
1.两个 server_name 后面的域名改成你的域名
server_name 你的域名;
2.两个/home/mastodon/live/public;路径要替换,换成你mastodonde 下public路径
root /root/docker/docker-compose/mastodon/public;
3.将try_files $uri =404;修改为try_files $uri @proxy;
4.取消证书注释,等下生成证书后需要替换证书路径
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
#如果安装时,你的 mastodon web/streaming 端口改了,清把下面端口也替换
location @proxy {
proxy_pass http://127.0.0.1:4000;
}
通过acme.sh申请证书(这边通过Dnsapi + cloudflare申请证书)
安装acme
curl https://get.acme.sh | sh -s email=你的邮箱
去cloudflare后台获取api
export CF_Key="输入你的api"
export CF_Email="输入cloudflare登陆邮箱"
开始申请泛证书(泛证书可以使用在这个域名下所有的二级域名)
acme.sh --issue --dns dns_cf -d 主域名 -d '*.主域名‘
注:如果上面运行出错就用下面命令
~/.acme.sh/acme.sh --issue --dns dns_cf -d 主域名 -d '*.主域名‘
证书申请下来复制下证书的路径,把fullchain.cer和key路径替换到上面的两个ssl路径内。
最后重载Nginx
nginx -s reload
现在应该可以通过你的域名访问到Mastodon
通过 tootctl 生成mastodon 管理员账号
cd /root/docker/docker-compose/mastodon
进入docker mastodon web容器
docker-compose exec web bash
tootctl accounts create 用户名 --email 邮箱 --confirmed --role Owner
然后会输出一个管理员密码,密码只显示一次,记得保存下来。
现在账户创建成功,但是还没有被批准是无法使用。
通过tootctl批准账户
tootctl accounts approve 用户名
还有很多tootctl指令,可以访问网页查看。
https://docs.joinmastodon.org/admin/tootctl/
注:如果忘记密码了,可以重置用户密码,首先进入容器,然后执行命令
tootctl accounts modify "管理员用户名" --email "管理员邮箱" --reset-password
退出容器
exit
至此所有mastodon算搭建完成。
以下是一些优化
开启全文搜索
nano /root/docker/docker-compose/mastodon/docker-compose.yml
编辑docker-compose.yml
1.删除es及下面的所有#注释(删除要按2次,因为注释往后推进了2格)
2.web 内也有一个es #注释也要删除(删除要按2次,因为注释往后推进了2格)
退出docker-compose.yml
编辑.env.production
nano /root/docker/docker-compose/mastodon/.env.production
加入一下信息
ES_ENABLED=true
ES_HOST=es
ES_PORT=9200
重新启动
docker-compose down
docker-compose up -d
待mastodon目录下出现elasticsearch文件夹后,赋权:
chown 1000:1000 -R elasticsearch
再次重新启动
docker-compose down
docker-compose up -d
全文搜索即搭建完成。
评论区