Oct 30, 2024 3 min read

Peertube: A Beginner-Friendly Guide to Self-Hosting

Peertube: A Beginner-Friendly Guide to Self-Hosting
Table of Contents

PeerTube is a decentralized video hosting platform that empowers users to self-host and share videos while maintaining complete control over their data. Designed to bypass the restrictions of centralized platforms, PeerTube uses WebTorrent technology for peer-to-peer video distribution and supports federation through the ActivityPub protocol. This guide walks you through deploying, configuring, and managing a self-hosted PeerTube instance, covering essential topics like installation, reverse proxy setup, logging, backups, updates, and advanced configurations.

Installing Peertube

πŸ“¦ Docker/Docker Compose Setup

Using Docker is one of the easiest and most efficient ways to deploy PeerTube. Below is a docker-compose.yml file tailored for PeerTube:


version: '3.7'

services:

peertube:

image: chocobozzz/peertube:latest

container_name: peertube

ports:

- "9000:9000"

environment:

- NODE_ENV=production

- PEERTUBE_DB_HOST=postgres

- PEERTUBE_DB_USERNAME=peertube

- PEERTUBE_DB_PASSWORD=yourpassword

- PEERTUBE_DB_NAME=peertube

- PEERTUBE_REDIS_HOST=redis

volumes:

- ./data/config:/config

- ./data/storage:/data

- ./data/logs:/logs

depends_on:

- postgres

- redis

restart: unless-stopped

postgres:

image: postgres:13

environment:

POSTGRES_USER: peertube

POSTGRES_PASSWORD: yourpassword

POSTGRES_DB: peertube

volumes:

- ./data/db:/var/lib/postgresql/data

restart: unless-stopped

redis:

image: redis:6

restart: unless-stopped

To deploy:


mkdir -p /opt/peertube && cd /opt/peertube

nano docker-compose.yml  # Copy the above YAML content

docker-compose up -d

This will deploy PeerTube, PostgreSQL, and Redis containers. Replace yourpassword with a secure password for the database.

πŸš€ Manual Installation

For those who prefer a manual setup, follow these steps on a Linux server:

  1. Install dependencies:

sudo apt update && sudo apt install -y curl gnupg nginx postgresql redis nodejs npm yarn

  1. Download and configure PeerTube:

curl -L https://github.com/Chocobozzz/PeerTube/releases/download/vX.X.X/peertube-vX.X.X.tar.xz -o peertube.tar.xz

tar xf peertube.tar.xz

mv peertube /var/www/peertube

cd /var/www/peertube

yarn install --production

cp config/production.yaml.sample config/production.yaml

  1. Setup systemd service:

sudo nano /etc/systemd/system/peertube.service

Add the following content:


[Unit]

Description=PeerTube

After=postgresql.service redis.service

[Service]

Type=simple

User=peertube

WorkingDirectory=/var/www/peertube

ExecStart=/usr/bin/node /var/www/peertube/dist/server

Restart=always

[Install]

WantedBy=multi-user.target

Enable and start the service:


sudo systemctl enable peertube

sudo systemctl start peertube

Configuring Nginx as a Reverse Proxy

🌐 Nginx Configuration

Below is an example Nginx server block for PeerTube:


server {

listen 80;

server_name yourdomain.com;

location / {

proxy_pass http://localhost:9000;

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;

}

}

Create the configuration file:


sudo nano /etc/nginx/sites-available/peertube

sudo ln -s /etc/nginx/sites-available/peertube /etc/nginx/sites-enabled/

πŸ”’ SSL/TLS Setup

Secure your PeerTube instance with Let's Encrypt:


sudo apt install certbot python3-certbot-nginx

sudo certbot --nginx -d yourdomain.com

Automate certificate renewal:


sudo systemctl enable certbot.timer

πŸ› οΈ Testing and Reloading Nginx

Validate the Nginx configuration and reload:


sudo nginx -t

sudo systemctl reload nginx

Logging and Debugging Peertube

πŸ—ƒοΈ Enabling Debug Logs

Enable debug logging in production.yaml:


logs:

level: debug

Restart PeerTube:


sudo systemctl restart peertube

πŸ“„ Viewing Logs

For Docker users:


docker logs -f peertube

For manual installations:


journalctl -u peertube -f

πŸ› οΈ Troubleshooting Common Issues

Check Node.js or database connectivity errors in the logs. Example:


journalctl -u peertube | grep "error"

πŸ“€ Exporting Logs

Send logs to an external system (e.g., ELK Stack):

  1. Update production.yaml:

logs:

transports:

- console

- file

- syslog

  1. Install syslog dependencies:

sudo apt install rsyslog

Backup and Restore

πŸ—‚οΈ File-Based Backups

Backup PeerTube configuration and data:


tar -czvf peertube-backup.tar.gz /var/www/peertube

πŸ”„ Database Backups

Backup PostgreSQL:


pg_dump -U peertube peertube > peertube-db-backup.sql

Restore:


psql -U peertube -d peertube -f peertube-db-backup.sql

πŸ“… Automated Backup Scripts

Create a cron job:


crontab -e

Add the following line:


0 2 * * * tar -czvf /backups/peertube-$(date +\%F).tar.gz /var/www/peertube

Updating and Upgrading Peertube

⬆️ Updating Docker Images

Update PeerTube via Docker:


docker-compose down

docker-compose pull

docker-compose up -d

πŸ› οΈ Manual Updates

Download the latest version, extract, and replace the old files:


curl -L https://github.com/Chocobozzz/PeerTube/releases/download/vX.X.X/peertube-vX.X.X.tar.xz -o peertube.tar.xz

tar xf peertube.tar.xz

rsync -av --delete peertube-vX.X.X/ /var/www/peertube/

Restart PeerTube:


sudo systemctl restart peertube

πŸ” Checking for Updates

Regularly check the GitHub releases page or use:


curl -s https://api.github.com/repos/Chocobozzz/PeerTube/releases/latest | grep "tag_name"

Leveraging Peertube’s Unique Features

πŸ”§ Enabling APIs

Enable the API by configuring production.yaml:


api:

enabled: true

Test the API with curl:


curl -X GET http://yourdomain.com/api/v1/videos

🌟 Advanced Configurations

Customize video encoding settings in production.yaml:


transcoding:

profiles:

- name: 720p

bitrate: 1500k

- name: 1080p

bitrate: 3000k

Save and restart PeerTube for changes to take effect.

Wrapping Up

By following this guide, you've successfully deployed, configured, and optimized PeerTube for self-hosted video sharing. From installation to advanced configurations, these steps ensure a secure, scalable, and feature-rich platform. Start harnessing the full potential of PeerTube to manage your video content with absolute control and freedom!

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Selfhosted Ninja.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.