theme: channing-cyan
前言
长毛象(Mastodon)是一个去中心化的微博客平台,所以大致使用方式,类似微博或者twitter。区别是,它并不是一个网站,而是成百上千的小站点聚合而成,俗称“联邦宇宙”。这些网站都遵循同一个协议,使用Mastodon系统,每一个网站都叫做一个实例。
本篇文章将介绍如何搭建属于自己的Mastodon实例,在此之前,你需要
- 国外服务器
- 邮箱并且支持SMTP
- 已解析的域名
一、创建 SWAP 分区
如果内存过小,会导致没有样式的问题,我的服务器是2G,需要分配个2G的空间
检查是否已启用 SWAP 空间
$ sudo swapon --show #未启用SWAP分区的话,执行此命令将不会有任何输出
创建 SWAP 文件
在添加 SWAP 文件时,建议创建的文件大小为实际物理内存的 2-3 倍。
$ sudo fallocate -l 2G /swapfile #创建大小依据实际物理内存大小自行调整
#或使用dd命令创建
$ sudo dd if=/dev/zero of=/swapfile bs=512M count=4
创建完成后,给与 SWAP 文件 600 权限
$ sudo chmod 600 /swapfile
标注 SWAP 区域
$ sudo mkswap /swapfile
激活 SWAP 分区
$ sudo swapon /swapfile
查看 SWAP 分区是否工作
$ sudo swapon --show
$ sudo free -h
将创建的 SWAP 分区设置为永久分区,将 SWAP 路径写入到/etc/fstab
文件中
/swapfile swap swap defaults 0 0
二、安装常用命令
apt update && apt install wget rsync python git curl vim git ufw -y
三、安装docker、docker-compose
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镜像
- 拉取镜像
mkdir -p /home/mastodon/mastodon
cd /home/mastodon/mastodon
systemctl start docker
docker pull tootsuite/mastodon:v3.5.1 #如果需要升级到某稳定版本,请将latest改成v3.5.1等版本号。
wget https://raw.githubusercontent.com/tootsuite/mastodon/master/docker-compose.yml
- 修改
docker-compose.yml
配置文件
nano docker-compose.yml
找到
web
、streaming
、sidekiq
,在每一类的image:tootsuite/mastodon后添加:lastest或者版本号踩坑:最新版本会出现添加管理员失败的情况,3.5.1没有样式(原因是因为运行内存不足,需要添加swap)
五、初始化PostgreSQL
docker run --name postgres14 -v /home/mastodon/mastodon/postgres14:/var/lib/postgresql/data -e POSTGRES_PASSWORD=123456 --rm -d postgres:14-alpine
这里将管理员密码设置为
123456
docker exec -it postgres14 psql -U postgres
#输入SQL语句
CREATE USER mastodon WITH PASSWORD '123456' CREATEDB;
#创建mastodon用户;这里的是数据库密码,同样设置为123456
\q #退出数据库
#最后停止docker:
docker stop postgres14
六、配置Mastodon
touch .env.production # 创建.env.production文件
docker-compose run --rm web bundle exec rake mastodon:setup
随后会出现下面的问答
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设置的数据库密码)
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配置,复制下来,先存到电脑里,等会儿要用。
这里还会出现管理员密码,也要记下来1dc76aa40be294e48e82291fc43256b9
然后将上面复制的配置粘贴到下面
nano .env.production
- 启动Mastodon
docker-compose down
docker-compose up -d
- 给文件赋权
chown 991:991 -R ./public
chown -R 70:70 ./postgres14
docker-compose down
docker-compose up -d
七、配置Nginx
- 安装宝塔
wget -O install.sh ``http://download.bt.cn/install/install-ubuntu_6.0.sh`` && sudo bash install.sh
- 在宝塔内安装nginx
- 赋值内容https://github.com/mastodon/mastodon/blob/main/dist/nginx.conf
- 将/home/mastodon/live/public路径改为/home/mastodon/mastodon/public
- 将SSL证书的注释去掉(如下图),并且把证书目录改为你自己的路径
八、效果展示
最后
第一次搭建可能会有点困难,需要有耐心,我在搭建过程中已经重装很多次系统了,如果小伙伴在搭建的有疑问,我会尽力帮助~
暂无评论内容