PeerTube

Updated 10 September 2019

PeerTube

PeerTube is a distributed video hosting and broadcasting platform. [PeerTube] (https://joinpeertube.org) is an independent, lightweight, scalable alternative to YouTube, Dailymotion and Vimeo. It uses visitor browsers to create a P2P-based distributed network. PeerTube supports the ActivityPub protocol, which allows you to merge isolated servers with video content into a federated network. People visiting it can subscribe to channels and be notified about new videos. This open source application is distributed under the AGPLv3 license.

Preparing a LXC container

We recommend that you install PeerTub in a separate container and configure it according to the manual.

Installing and configuring PostgreSQL

Install and configure PostgreSQL according to the manual. Replace dbtest with your peertube_prod database, and the test user with peertube. Complete the setup process:

psql -U postgres

psql (11.2)
Type "help" for help.
postgres=# \c peertube_prod
postgres=# CREATE EXTENSION pg_trgm;
postgres=# CREATE EXTENSION unaccent;
postgres=# \q

Installing PeerTube

Install the packages required by PeerTube:

emerge -a app-arch/unzip dev-db/redis media-video/ffmpeg dev-python/nodeenv

Create a peertube user account and protect it with a password:

mkdir -p /var/calculate/www

useradd -m -d /var/calculate/www/peertube -s /bin/bash -p peertube peertube

passwd peertube

Install Node.js in the user directory:

su - peertube

nodeenv --node=8.16.3 .node-10

source .node-10/bin/activate

npm install -g yarn

echo 'source ~/.node-10/bin/activate' >> ~/.bash_profile

Create the necessary paths, download and extract the latest version of PeerTube:

mkdir config storage versions

VERSION=$(curl -s https://api.github.com/repos/chocobozzz/peertube/releases/latest | grep tag_name | cut -d '"' -f 4) && echo "Latest Peertube version is $VERSION"

wget "https://github.com/Chocobozzz/PeerTube/releases/download/${VERSION}/peertube-${VERSION}.zip" -P versions

unzip versions/peertube-${VERSION}.zip -d versions

rm versions/peertube-${VERSION}.zip

Install PeerTube:

ln -s versions/peertube-${VERSION} ./peertube-latest

cd ./peertube-latest

yarn install --production --pure-lockfile

cp /var/calculate/www/peertube/versions/peertube-latest/config/default.yaml /var/calculate/www/peertube/config/default.yaml

cp config/production.yaml.example ../config/production.yaml

PeerTube configuration

Setup PeerTube, replacing peertube.example.org with your site:

/var/calculate/www/peertube/config/production.yaml
# Correspond to your reverse proxy server_name/listen configuration
webserver:
  https: true
  hostname: 'peertube.example.org'
  port: 443
# Your database name will be "peertube"+database.suffix
database:
  hostname: 'localhost'
  port: 5432
  suffix: '_prod'
  username: 'peertube'
  password: 'secret'
  pool:
    max: 5
# SMTP server to send emails
smtp:
  hostname: mail.example.org
  port: 465 # If you use StartTLS: 587
  username: null
  password: null
  tls: true # If you use StartTLS: false
  disable_starttls: false
  ca_file: null # Used for self signed certificates
  from_address: 'admin@mail.example.org'
# From the project root directory
storage:
  tmp: '/var/calculate/www/peertube/storage/tmp/' # Used to download data (imports etc), store uploaded files before processing...
  avatars: '/var/calculate/www/peertube/storage/avatars/'
  videos: '/var/calculate/www/peertube/storage/videos/'
  redundancy: '/var/calculate/www/peertube/storage/videos/'
  logs: '/var/calculate/www/peertube/storage/logs/'
  previews: '/var/calculate/www/peertube/storage/previews/'
  thumbnails: '/var/calculate/www/peertube/storage/thumbnails/'
  torrents: '/var/calculate/www/peertube/storage/torrents/'
  captions: '/var/calculate/www/peertube/storage/captions/'
  cache: '/var/calculate/www/peertube/storage/cache/'
admin:
  # Used to generate the root user at first startup
  # And to receive emails from the contact form
  email: 'support@example.org'

Getting Let's Encrypt certificate

Get a certificate for Nginx on peertube.example.org according to the manual.

Installing and configuring Nginx

Install and configure your Nginx server as a reverse proxy according to the manual.

Copy the default Nginx configuration file for PeerTube:

cp /var/calculate/www/peertube/peertube-latest/support/nginx/peertube /etc/nginx/sites-enabled/peertube.conf

Specify the paths and the server name, peertube.example.org:

/etc/nginx/sites-enabled/peertube.conf
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name peertube.example.org;

    ssl_certificate /etc/letsencrypt/live/peertube.example.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/peertube.example.org/privkey.pem;
    ...
    # Bypass PeerTube for performance reasons. Could be removed
    location ~ ^/client/(.*\.(js|css|woff2|otf|ttf|woff|eot))$ {
        add_header Cache-Control "public, max-age=31536000, immutable";
        alias /var/calculate/www/peertube/peertube-latest/client/dist/$1;
    }
    ...
    # Cache 2 hours
    add_header Cache-Control "public, max-age=7200";
    root /var/calculate/www/peertube/storage;
    rewrite ^/static/(thumbnails|avatars)/(.*)$ /$1/$2 break;
    try_files $uri
    ...
        # Don't spam access log file with byte range requests
        access_log off;
    }
    root /var/calculate/www/peertube/storage;
    rewrite ^/static/webseed/(.*)$ /videos/$1 break;
    rewrite ^/static/redundancy/(.*)$ /redundancy/$1 break;
}

Starting PeerTube

Create an OpenRC script for PeerTube:

/etc/init.d/peertube
#!/sbin/openrc-run
# Copyright 2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

name="Peertube daemon"
description=""
pidfile=/run/peertube.pid
command_user=peertube
output_log=/var/log/peertube.log
error_log=/var/log/peertube.log
directory=/var/calculate/www/peertube/peertube-latest
start_stop_daemon_args="-e NODE_ENV=production -e NODE_CONFIG_DIR=/var/calculate/www/peertube/config -e PATH=/var/calculate/www/peertube/.node-10/lib/node_modules/.bin:/var/calculate/www/peertube/.node-10/bin:\"$PATH\"
command="/var/calculate/www/peertube/.node-10/bin/npm"
command_args="start"
command_background=true

depend() {
        need nginx postgresql redis
}

start_pre() {
    checkpath -f -o peertube -m 0600 /var/log/peertube.log
}

Define the privileges:

chmod 0755 /etc/init.d/peertube

Launch the PeerTube daemon:

/etc/init.d/peertube start

Add PeerTube to autostart:

rc-update add peertube

Open peertube.example.org in your Web browser:

peertube.example.org

Defining the administration password

The administrator password is created automatically. You can find it in the log. To reset the password, run:

cd /var/www/peertube/peertube-latest && NODE_CONFIG_DIR=/var/calculate/www/peertube/config NODE_ENV=production npm run reset-password -- -u root

Updating PeerTube

Download and unpack the new version of PeerTube:

su - peertube

VERSION=$(curl -s https://api.github.com/repos/chocobozzz/peertube/releases/latest | grep tag_name | cut -d '"' -f 4) && echo "Latest Peertube version is $VERSION"

wget "https://github.com/Chocobozzz/PeerTube/releases/download/${VERSION}/peertube-${VERSION}.zip" -P versions

unzip versions/peertube-${VERSION}.zip -d versions

rm versions/peertube-${VERSION}.zip

Install PeerTube:

cd versions/peertube-${VERSION}

yarn install --production --pure-lockfile

Copy the new default configuration file:

cp config/default.yaml /var/calculate/www/peertube/config/default.yaml

Update your production.yml configuration:

mv /var/calculate/www/peertube/config/production.yaml /var/calculate/www/peertube/config/production-old.yaml

cp config/production.yaml.example /var/calculate/www/peertube/config/production.yaml

Check the differences and move your settings to production.yaml:

diff /var/calculate/www/peertube/config/production{-old,}.yaml

Update the symlink:

cd

unlink peertube-latest

ln -s versions/peertube-${VERSION} ./peertube-latest

exit

Now restart PeerTube:

/etc/init.d/peertube restart