PeerTube - 自托管视频串流/直播平台

PeerTube – 自托管视频串流/直播平台

之前也搭建过不少直播、视频串流服务:

PeerTube 定位更加靠近 YouTube,有完善的视频上传、分发功能,并且在 V3 版本中新增了直播

本文将展示如何在 Debian 10 下无域名搭建 PeerTube

环境

参考

步骤

因为没有使用域名和 SSL,所以搭建过程较官方教程省去了很多步骤

安装依赖环境

首先安装

apt install curl sudo unzip vim

安装 Node.js 10

curl -sL https://rpm.nodesource.com/setup_10.x | bash -
apt install -y nodejs

安装 Yarn

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
apt update && apt install yarn

其他依赖

apt install nginx ffmpeg postgresql postgresql-contrib openssl g++ make redis-server git python-dev

确认 ffmpeg 和 g++ 的版本

ffmpeg -version # 版本 >= 4.1
g++ -v # 版本 >= 5.x

确认 PostgreSQL 和 Redis 正在运行

systemctl status prostregsql
systemctl status redis

安装 PeerTube

添加 PeerTube 用户

  1. 添加用户
    sudo useradd -m -d /var/www/peertube -s /bin/bash -p peertube peertube
  2. 设置密码
    sudo passwd peertube

配置数据库

  1. 创建数据库
    sudo -u postgres createuser -P peertube 
    sudo -u postgres createdb -O peertube -E UTF8 -T template0 peertube_prod

    新建用户后会要求输入新用户的密码

  2. 启用 PeerTube 需要的组件
    sudo -u postgres psql -c "CREATE EXTENSION pg_trgm;" peertube_prod
    sudo -u postgres psql -c "CREATE EXTENSION unaccent;" peertube_prod

获取 PeerTube

  1. 下载 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"
  2. 新建目录
    cd /var/www/peertube
    sudo -u peertube mkdir config storage versions

获取并解压 PeerTube 客户端

cd /var/www/peertube/versions
sudo -u peertube wget -q "https://github.com/Chocobozzz/PeerTube/releases/download/${VERSION}/peertube-${VERSION}.zip"
sudo -u peertube unzip peertube-${VERSION}.zip && sudo -u peertube rm peertube-${VERSION}.zip

安装 PeerTube

cd /var/www/peertube
sudo -u peertube ln -s versions/peertube-${VERSION} ./peertube-latest
cd ./peertube-latest && sudo -H -u peertube yarn install --production --pure-

PeerTube - 自托管视频串流/直播平台

配置 PeerTube

复制配置文件

cd /var/www/peertube
sudo -u peertube cp peertube-latest/config/default.yaml config/default.yaml
sudo -u peertube cp peertube-latest/config/production.yaml.example config/production.yaml

编辑配置文件

  1. 打开配置文件
    cd /var/www/peertube 
    vim config/production.yaml
  2. 因为不使用域名,所以更改 WebServer 的域名为 IP 地址,并关闭 https,更改端口为 80,如下图PeerTube - 自托管视频串流/直播平台
  3. 向下找到数据库配置区域,更改密码为数据库新用户的密码PeerTube - 自托管视频串流/直播平台
  4. 继续向下找到管理员邮箱,更改为自己的邮箱地址(不需要真实邮箱)PeerTube - 自托管视频串流/直播平台

Nginx 反向代理

  1. 复制 Nginx 配置文件
    cp /var/www/peertube/peertube-latest/support/nginx/peertube /etc/nginx/sites-available/peertube

    如果是新安装的 Nginx,先移除默认配置文件

    rm /etc/nginx/sites-enabled/default
    nginx -s reload
  2. 编辑 /etc/nginx/sites-available/,更改所有的 ${WEBSERVER_HOST} 为 IP 地址,删除 http 跳转和 SSL 的相关配置,最后更改 backend 的地址为 127.0.0.1:9000,最终的配置文件如下
    server {
      listen 80;
      listen [::]:80;
      server_name 192.168.16.16;
    
      access_log /var/log/nginx/peertube.access.log; # reduce I/0 with buffer=10m flush=5m
      error_log  /var/log/nginx/peertube.error.log;
    
      location @api {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host            $host;
        proxy_set_header X-Real-IP       $remote_addr;
    
        client_max_body_size  100k; # default is 1M
    
        proxy_connect_timeout 10m;
        proxy_send_timeout    10m;
        proxy_read_timeout    10m;
        send_timeout          10m;
    
        proxy_pass http://backend;
      }
    
      location / {
        try_files /dev/null @api;
      }
    
      location = /api/v1/users/me/avatar/pick {
        limit_except POST HEAD { deny all; }
    
        client_max_body_size                      2M; # default is 1M
        add_header            X-File-Maximum-Size 2M always; # inform backend of the set value in bytes
    
        try_files /dev/null @api;
      }
    
      location = /api/v1/videos/upload {
        limit_except POST HEAD { deny all; }
    
        # This is the maximum upload size, which roughly matches the maximum size of a video file.
        # Note that temporary space is needed equal to the total size of all concurrent uploads.
        # This data gets stored in /var/lib/nginx by default, so you may want to put this directory
        # on a dedicated filesystem.
        client_max_body_size                      8G; # default is 1M
        add_header            X-File-Maximum-Size 8G always; # inform backend of the set value in bytes
    
        try_files /dev/null @api;
      }
    
      ##
      # Websocket
      ##
    
      location @api_websocket {
        proxy_http_version 1.1;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   Host            $host;
        proxy_set_header   X-Real-IP       $remote_addr;
        proxy_set_header   Upgrade         $http_upgrade;
        proxy_set_header   Connection      "upgrade";
    
        proxy_pass http://backend;
      }
    
      location /socket.io {
        try_files /dev/null @api_websocket;
      }
    
      location /tracker/socket {
        # Peers send a message to the tracker every 15 minutes
        # Don't close the websocket before then
        proxy_read_timeout 15m; # default is 60s
    
        try_files /dev/null @api_websocket;
      }
    
      ##
      # Performance optimizations
      # For extra performance please refer to https://github.com/denji/nginx-tuning
      ##
    
      root /var/www/peertube/storage;
    
      # Enable compression for JS/CSS/HTML, for improved client load times.
      # It might be nice to compress JSON/XML as returned by the API, but
      # leaving that out to protect against potential BREACH attack.
      gzip              on;
      gzip_vary         on;
      gzip_types        # text/html is always compressed by HttpGzipModule
                        text/css
                        application/javascript
                        font/truetype
                        font/opentype
                        application/vnd.ms-fontobject
                        image/svg+xml;
      gzip_min_length   1000; # default is 20 bytes
      gzip_buffers      16 8k;
      gzip_comp_level   2; # default is 1
    
      client_body_timeout       30s; # default is 60
      client_header_timeout     10s; # default is 60
      send_timeout              10s; # default is 60
      keepalive_timeout         10s; # default is 75
      resolver_timeout          10s; # default is 30
      reset_timedout_connection on;
    
      tcp_nopush                on; # send headers in one piece
      tcp_nodelay               on; # don't buffer data sent, good for small data bursts in real time
    
      # If you have a small /var/lib partition, it could be interesting to store temp nginx uploads in a different place
      # See https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_temp_path
      #client_body_temp_path /var/www/peertube/storage/nginx/;
    
      # Bypass PeerTube for performance reasons. Optional.
      # Should be consistent with client-overrides assets list in /server/controllers/client.ts
      location ~ ^/client/(assets/images/(icons/icon-36x36\.png|icons/icon-48x48\.png|icons/icon-72x72\.png|icons/icon-96x96\.png|icons/icon-144x144\.png|icons/icon-192x192\.png|icons/icon-512x512\.png|logo\.svg|favicon\.png))$ {
        add_header Cache-Control "public, max-age=31536000, immutable"; # Cache 1 year
    
        root /var/www/peertube;
    
        try_files /storage/client-overrides/$1 /peertube-latest/client/dist/$1 @api;
      }
    
      # Bypass PeerTube for performance reasons. Optional.
      location ~ ^/client/(.*\.(js|css|png|svg|woff2|otf|ttf|woff|eot))$ {
        add_header Cache-Control "public, max-age=31536000, immutable"; # Cache 1 year
    
        alias /var/www/peertube/peertube-latest/client/dist/$1;
      }
    
      # Bypass PeerTube for performance reasons. Optional.
      location ~ ^/static/(thumbnails|avatars)/ {
        if ($request_method = 'OPTIONS') {
          add_header Access-Control-Allow-Origin  '*';
          add_header Access-Control-Allow-Methods 'GET, OPTIONS';
          add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
          add_header Access-Control-Max-Age       1728000; # Preflight request can be cached 20 days
          add_header Content-Type                 'text/plain charset=UTF-8';
          add_header Content-Length               0;
          return 204;
        }
    
        add_header Access-Control-Allow-Origin    '*';
        add_header Access-Control-Allow-Methods   'GET, OPTIONS';
        add_header Access-Control-Allow-Headers   'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        add_header Cache-Control                  "public, max-age=7200"; # Cache response 2 hours
    
        rewrite ^/static/(.*)$ /$1 break;
    
        try_files $uri @api;
      }
    
      # Bypass PeerTube for performance reasons. Optional.
      location ~ ^/static/(webseed|redundancy|streaming-playlists)/ {
        limit_rate_after            5M;
    
        # Clients usually have 4 simultaneous webseed connections, so the real limit is 3MB/s per client
        set $peertube_limit_rate    800k;
    
        # Increase rate limit in HLS mode, because we don't have multiple simultaneous connections
        if ($request_uri ~ -fragmented.mp4$) {
          set $peertube_limit_rate  5M;
        }
    
        # Use this line with nginx >= 1.17.0
        #limit_rate $peertube_limit_rate;
        # Or this line if your nginx < 1.17.0
        set $limit_rate $peertube_limit_rate;
    
        if ($request_method = 'OPTIONS') {
          add_header Access-Control-Allow-Origin  '*';
          add_header Access-Control-Allow-Methods 'GET, OPTIONS';
          add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
          add_header Access-Control-Max-Age       1728000; # Preflight request can be cached 20 days
          add_header Content-Type                 'text/plain charset=UTF-8';
          add_header Content-Length               0;
          return 204;
        }
    
        if ($request_method = 'GET') {
          add_header Access-Control-Allow-Origin  '*';
          add_header Access-Control-Allow-Methods 'GET, OPTIONS';
          add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    
          # Don't spam access log file with byte range requests
          access_log off;
        }
    
        # Enabling the sendfile directive eliminates the step of copying the data into the buffer
        # and enables direct copying data from one file descriptor to another.
        sendfile on;
        sendfile_max_chunk 1M; # prevent one fast connection from entirely occupying the worker process. should be > 800k.
        aio threads;
    
        # Use this in tandem with fuse-mounting i.e. https://docs.joinpeertube.org/#/admin-remote-storage
        # to serve files directly from a public bucket without proxying.
        # Assumes you have buckets named after the storage subdirectories, i.e. 'videos', 'redundancy', etc.
        #set $cdn <your S3-compatiable bucket public url mounted via fuse>;
        #rewrite ^/static/webseed/(.*)$ $cdn/videos/$1 redirect;
        #rewrite ^/static/(.*)$         $cdn/$1        redirect;
        rewrite ^/static/webseed/(.*)$ /videos/$1 break;
        rewrite ^/static/(.*)$         /$1        break;
    
        try_files $uri @api;
      }
    }
  3. 软连接启用配置文件
    ln -s /etc/nginx/sites-available/peertube /etc/nginx/sites-enabled/
  4. 重载 nginx
    nginx -t
    nginx -s reload

启动并使用 PeerTube

启动 PeerTube

官方提供了 systemd 配置文件来管理 PeerTube

  1. 复制系统服务文件
    sudo cp /var/www/peertube/peertube-latest/support/systemd/peertube.service /etc/systemd/system/
  2. 重载系统服务
    sudo systemctl daemon-reload
  3. 启动 PeerTube
    sudo systemctl enable peertube
    sudo systemctl start peertube

进行到这里,无误的话已经可以使用浏览器访问 IP 地址,开始使用了

登录密码

PeerTube 的管理员登录密码输出到了日志中,执行指令查看密码

cd /var/www/peertube/storage/logs/
cat peertube.log | grep password

PeerTube - 自托管视频串流/直播平台

在登录页面输入配置的管理员邮箱和密码即可登录PeerTube - 自托管视频串流/直播平台

直播功能

使用管理员账户登录后,进入 管理 – Configuration – Live Streaming,开启直播功能PeerTube - 自托管视频串流/直播平台

每个账户在 发布 – Go Live – Live Settings 中获取到直播地址和直播密钥,在串流软件内填入对应区域即可PeerTube - 自托管视频串流/直播平台PeerTube - 自托管视频串流/直播平台

效果

PeerTube - 自托管视频串流/直播平台管理员登录后
PeerTube - 自托管视频串流/直播平台上传视频(这里上传了 mkv 格式的视频,需要转码)PeerTube - 自托管视频串流/直播平台转码完成后即可在线观看,测试 1080p 下服务器的上行带宽会达到 800KB/sPeerTube - 自托管视频串流/直播平台直播效果

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据