From 524847d027cbdce3288737b58e3ad9ab60687e97 Mon Sep 17 00:00:00 2001 From: Kaede Fujisaki Date: Fri, 26 Feb 2021 21:56:11 +0900 Subject: [PATCH] now we can run peertube! --- .gitignore | 2 +- .gitmodules | 2 +- docker-compose.yml | 63 ++++-- images/nginx/Dockerfile | 8 + images/nginx/entrypoint.nginx.sh | 17 ++ images/nginx/peertube.conf | 234 +++++++++++++++++++++++ {peertube => images/peertube}/Dockerfile | 0 {peertube => images/peertube}/src | 0 8 files changed, 310 insertions(+), 16 deletions(-) create mode 100644 images/nginx/Dockerfile create mode 100644 images/nginx/entrypoint.nginx.sh create mode 100644 images/nginx/peertube.conf rename {peertube => images/peertube}/Dockerfile (100%) rename {peertube => images/peertube}/src (100%) diff --git a/.gitignore b/.gitignore index b2d51ae..a5e523b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/docker-volume/ \ No newline at end of file +/volume/ \ No newline at end of file diff --git a/.gitmodules b/.gitmodules index f40baa0..14baf61 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "docker-image/peertube/app"] - path = peertube/src + path = images/peertube/src url = https://github.com/chocobozzz/PeerTube diff --git a/docker-compose.yml b/docker-compose.yml index 59a79cc..6e8f776 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,26 +2,53 @@ version: '3.7' services: + # You can comment this webserver section if you want to use another webserver/proxy + web: + container_name: motibus_web + hostname: motibus_web + #image: chocobozzz/peertube-webserver:latest + # If you don't want to use the official image and build one from sources: + build: + context: ./images/nginx + dockerfile: Dockerfile + env_file: + - .env + expose: + - '80' + networks: + - default + - planet-link + volumes: + - type: bind + # Switch sources if you downloaded the whole repository + #source: ../../nginx/peertube + source: ./images/nginx/peertube.conf + target: /etc/nginx/conf.d/peertube.template + - assets:/var/www/peertube/peertube-latest/client/dist:ro + - ./volume/peertube/data:/var/www/peertube/storage + depends_on: + - peertube + restart: "always" peertube: - container_name: peertube - hostname: peertube - image: peertube + container_name: motibus_peertube + hostname: motibus_peertube + image: motibus_peertube build: - context: ./peertube + context: ./images/peertube networks: - - default - - planet-link + default: + ipv4_address: 172.54.0.42 env_file: - .env - expose: - - '1935' - '9000' + ports: + - '1935:1935' volumes: - - ./docker-volume/assets:/app/client/dist - - ./docker-volume/data:/data - - ./docker-volume/config:/config + - assets:/app/client/dist + - ./volume/peertube/data:/data + - ./volume/peertube/config:/config depends_on: - postgres - redis @@ -33,13 +60,13 @@ services: env_file: - .env volumes: - - ./docker-volume/db:/var/lib/postgresql/data + - ./volume/db:/var/lib/postgresql/data restart: "always" redis: image: redis:6-alpine volumes: - - ./docker-volume/redis:/data + - ./volume/redis:/data restart: "always" postfix: @@ -47,11 +74,19 @@ services: env_file: - .env volumes: - - ./docker-volume/opendkim/keys:/etc/opendkim/keys + - ./volume/opendkim/keys:/etc/opendkim/keys restart: "always" +networks: networks: default: + ipam: + driver: default + config: + - subnet: 172.54.0.0/16 planet-link: external: name: planet-link + +volumes: + assets: \ No newline at end of file diff --git a/images/nginx/Dockerfile b/images/nginx/Dockerfile new file mode 100644 index 0000000..875963d --- /dev/null +++ b/images/nginx/Dockerfile @@ -0,0 +1,8 @@ +FROM nginx:alpine + +COPY ./entrypoint.nginx.sh . +RUN chmod +x entrypoint.nginx.sh + +EXPOSE 80 443 +ENTRYPOINT [] +CMD ["/bin/sh", "entrypoint.nginx.sh"] diff --git a/images/nginx/entrypoint.nginx.sh b/images/nginx/entrypoint.nginx.sh new file mode 100644 index 0000000..ad26aa0 --- /dev/null +++ b/images/nginx/entrypoint.nginx.sh @@ -0,0 +1,17 @@ +#!/bin/sh +set -e + +# Process the nginx template +SOURCE_FILE="/etc/nginx/conf.d/peertube.template" +TARGET_FILE="/etc/nginx/conf.d/default.conf" +export WEBSERVER_HOST="$PEERTUBE_WEBSERVER_HOSTNAME" +export PEERTUBE_HOST="peertube:9000" + +envsubst '${WEBSERVER_HOST} ${PEERTUBE_HOST}' < $SOURCE_FILE > $TARGET_FILE + +while :; do + sleep 12h & wait $!; + nginx -s reload; +done & + +nginx -g 'daemon off;' diff --git a/images/nginx/peertube.conf b/images/nginx/peertube.conf new file mode 100644 index 0000000..2cc3a42 --- /dev/null +++ b/images/nginx/peertube.conf @@ -0,0 +1,234 @@ +# Minimum Nginx version required: 1.13.0 (released Apr 25, 2017) +# Please check your Nginx installation features the following modules via 'nginx -V': +# STANDARD HTTP MODULES: Core, Proxy, Rewrite, Access, Gzip, Headers, HTTP/2, Log, Real IP, SSL, Thread Pool, Upstream, AIO Multithreading. +# THIRD PARTY MODULES: None. + +upstream backend { + server ${PEERTUBE_HOST}; +} + +server { + listen 80; + listen [::]:80; + server_name ${WEBSERVER_HOST}; + + access_log /var/log/nginx/peertube.access.log; # reduce I/0 with buffer=10m flush=5m + error_log /var/log/nginx/peertube.error.log; + + ## + # Security hardening (as of Nov 15, 2020) + # based on Mozilla Guideline v5.6 + ## + + ssl_protocols TLSv1.2 TLSv1.3; + ssl_prefer_server_ciphers on; + ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256; # add ECDHE-RSA-AES256-SHA if you want compatibility with Android 4 + ssl_session_timeout 1d; # defaults to 5m + ssl_session_cache shared:SSL:10m; # estimated to 40k sessions + ssl_session_tickets off; + ssl_stapling on; + ssl_stapling_verify on; + # HSTS (https://hstspreload.org), requires to be copied in 'location' sections that have add_header directives + #add_header Strict-Transport-Security "max-age=63072000; includeSubDomains"; + + ## + # Application + ## + + 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/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 12G; # default is 1M + add_header X-File-Maximum-Size 8G always; # inform backend of the set value in bytes before mime-encoding (x * 1.4 >= client_max_body_size) + + try_files /dev/null @api; + } + + location ~ ^/api/v1/(videos|video-playlists|video-channels|users/me) { + client_max_body_size 3M; # default is 1M + add_header X-File-Maximum-Size 2M always; # inform backend of the set value in bytes before mime-encoding (x * 1.4 >= client_max_body_size) + + 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; + proxy_ignore_client_abort 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 ; + #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; + } +} diff --git a/peertube/Dockerfile b/images/peertube/Dockerfile similarity index 100% rename from peertube/Dockerfile rename to images/peertube/Dockerfile diff --git a/peertube/src b/images/peertube/src similarity index 100% rename from peertube/src rename to images/peertube/src