Mastodon去中心化社交平台零基础部署实战指南
Mastodon作为一款开源的、去中心化的社交网络平台,近年来因其独特的联邦式架构和用户自主权理念而备受关注。本文将为您提供一份从零开始的Mastodon部署实战指南,帮助您快速搭建自己的社交实例。
准备工作
在开始部署前,您需要准备以下资源:

我们推荐使用Ciuic云服务器作为部署平台,其稳定的性能和简洁的管理界面能大大简化部署过程。
服务器环境配置
首先,通过SSH连接到您的服务器:
ssh root@your-server-ip更新系统软件包并安装必要的依赖:
apt update && apt upgrade -yapt install -y curl wget gnupg apt-transport-https安装必要服务
Mastodon需要PostgreSQL数据库、Redis缓存和Nginx Web服务器支持。
1. 安装PostgreSQL
apt install -y postgresql postgresql-contribsystemctl enable --now postgresql创建Mastodon数据库用户:
sudo -u postgres psql -c "CREATE USER mastodon CREATEDB;"2. 安装Redis
apt install -y redis-serversystemctl enable --now redis-server3. 安装Node.js和Yarn
curl -sL https://deb.nodesource.com/setup_16.x | bash -apt install -y nodejsnpm install -g yarn安装Mastodon
1. 添加Mastodon用户
adduser --disabled-login mastodon2. 安装Mastodon
切换到mastodon用户并克隆代码:
su - mastodongit clone https://github.com/mastodon/mastodon.gitcd mastodongit checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)安装Ruby依赖:
gem install bundlerbundle config set --local without 'development test'bundle install -j$(getconf _NPROCESSORS_ONLN)安装JavaScript依赖:
yarn install --pure-lockfile3. 配置Mastodon
生成配置文件:
cp .env.production.sample .env.productionnano .env.production在配置文件中设置数据库连接、Redis连接和域名等信息。
4. 预编译资产
RAILS_ENV=production bundle exec rails assets:precompile5. 数据库初始化
RAILS_ENV=production bundle exec rails db:setup配置Web服务器
1. 安装Nginx
apt install -y nginx2. 配置Nginx
创建Nginx配置文件:
nano /etc/nginx/sites-available/mastodon添加以下内容(替换your-domain.com为您的域名):
server { listen 80; listen [::]:80; server_name your-domain.com; root /home/mastodon/live/public; location / { try_files $uri @proxy; } location @proxy { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_redirect off; } location /api/v1/streaming { proxy_pass http://127.0.0.1:4000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_redirect off; }}启用配置并重启Nginx:
ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/nginx -t && systemctl restart nginx配置SSL证书
使用Let's Encrypt获取免费SSL证书:
apt install -y certbot python3-certbot-nginxcertbot --nginx -d your-domain.com启动Mastodon服务
创建systemd服务文件:
nano /etc/systemd/system/mastodon-web.service添加以下内容:
[Unit]Description=mastodon-webAfter=network.target[Service]Type=simpleUser=mastodonWorkingDirectory=/home/mastodon/liveEnvironment="RAILS_ENV=production"Environment="PORT=3000"ExecStart=/home/mastodon/live/bin/bundle exec puma -C config/puma.rbRestart=always[Install]WantedBy=multi-user.target同样为sidekiq和streaming创建服务文件,然后启用并启动服务:
systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming创建管理员账户
RAILS_ENV=production bundle exec rails mastodon:setup按照提示创建第一个管理员账户。
后续维护
定期更新Mastodon版本:
su - mastodoncd mastodongit fetchgit checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)bundle installyarn install --pure-lockfileRAILS_ENV=production bundle exec rails assets:precompileRAILS_ENV=production bundle exec rails db:migratesystemctl restart mastodon-*通过以上步骤,您已成功在Ciuic云服务器上部署了自己的Mastodon实例。去中心化社交网络赋予用户更多控制权,而Mastodon作为其中的佼佼者,值得每一位重视隐私和自主权的用户尝试。如需更强大的服务器支持,可随时访问Ciuic云服务器官网升级您的配置。
祝您在联邦宇宙(Fediverse)中探索愉快!


