Creating an NGINX Video Relay Server with Live Player

Based on these links:

https://www.youtube.com/watch?v=VHUPGfgkIgw

https://www.youtube.com/watch?v=eD88NZN6N6Q

https://www.youtube.com/watch?v=Ap_ZxQ0voqE&list=PL10Ao–Mok8MgVMkUqsqnnYRoFtL9P81m&index=5


For this I started with creating a Debian 10 droplet on Digital Ocean…

Once the droplet has been created, SSH and WinSCP into the machine as root…

  1. Remember to update then reboot.
apt-get update
apt-get upgrade
apt-get dist-upgrade
reboot

2. Download the NGINX package (at time of writing the version was 1.18.0), expand and delete the download.

http://nginx.org/en/download.html

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvf nginx-1.18.0.tar.gz
rm nginx-1.18.0.tar.gz

3. Install git and clone the RTMP module:

https://github.com/arut/nginx-rtmp-module

apt-get install git
git clone https://github.com/arut/nginx-rtmp-module

4. Change to the NGINX folder and compile.

You will also need a few other installations to make this work…

cd nginx-1.18.0
apt-get install build-essential
apt-get install libpcre3 libpcre3-dev libssl-dev
apt-get install zlib1g zlib1g-dev

5. Configure:

http://nginx.org/en/docs/configure.html

./configure --with-http_ssl_module \
--add-module=../nginx-rtmp-module \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-pcre \
--pid-path=/run/nginx.pid

6. Make and Install:

make
make install

7. Set up to run as service:

I find easiest to use WinSCP. Go to /etc/systemd/system and create a new file nginx.service. Paste in the following:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
User=root
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

8. Set up the config file:

I find easiest to use WinSCP. Go to your /etc/nginx folder and edit nginx.conf. Paste in the following:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live-<add random string here for stream link from obs> {
            record off;
            live on;

            push rtmp://a.rtmp.youtube.com/live2/<your youtube stream key>;

            # Turn on HLS
            hls on;
            hls_path /usr/local/nginx/html/hls/;
            hls_fragment 3;
            hls_playlist_length 60;
            ## disable consuming the stream from nginx as rtmp
            #deny play all;
        }
    }
}

9. Add Video.js and the hsl folders:

https://github.com/videojs/video.js/releases

Current version at time of writing is v7.11.0

cd /usr/local/nginx/html
mkdir hls
mkdir video.js
cd video.js
apt-get install unzip
wget https://github.com/videojs/video.js/releases/download/v7.11.0/video-js-7.11.0.zip
rm video-js-7.11.0.zip

10. Create a player.html file.

Using WinSCP, create stream.html in /usr/local/nginx/html and add the following:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Live Streaming</title>
    <link href="/video.js/video-js.min.css" rel="stylesheet">
    <script src="/video.js/video.min.js"></script>
</head>
<body>
<video id="player" class="video-js vjs-default-skin" height="360" width="640" controls preload="none">
    <source src="/hls/stream.m3u8" type="application/x-mpegURL" />
</video>
<script>
    var player = videojs('#player');
</script>
</body>
</html>

11. Run the service and enable it:

systemctl daemon-reload
systemctl start nginx
systemctl enable nginx

Browse to your IP and you should see the NGINX welcome page:

You can stop the service with:

systemctl stop nginx

And it should restart automatically when you start the machine.

OpenHAB on a Single Board Computer (SBC)

For this I’m going to use an Asus Tinkerboard – but should be similar for any SBC.

Start by downloading the operating system image.  My preferred is Armbian. and using the mainline Debian distribution (command line – not desktop).

The image will download in a .7z compression so you will need 7-Zip to extract the image.

Next, copy the image to a Micro SD card.  There are many tools to do this and my preferred is Win32 Disk Imager.

First boot will take a while but will show up on your router as “tinkerboard”.  Two tools your are going to need are WinSCP (sftp, scp and ftp client) and Putty (ssh client).

Start putty and log into the IP of the Tinkerboard.  The first time you log in use ‘root’ with the default password ‘1234’ – you will be prompted to change the password and create a new user.  The SBC is now all set to proceed – at this point I like to reboot (enter reboot at the command prompt).

Log into the device again with Putty as ‘root’.  The first thing I like to do is disable logging (many will say this is a bad idea but my belief is that if you don’t the SD card will fill up and your system will stop working).  To do this execute the following two comands:

[sourcecode language=”text”]service rsyslog stop
systemctl disable rsyslog[/sourcecode]

(This can be re-enabled using ‘systemctl enable rsyslog’)

Next update the system using the following two commands (this will take a while):

[sourcecode language=”text”]apt-get update
apt-get upgrade[/sourcecode]

After it finishes reboot and login again with ‘root’.

Next is to set the timezone and hostname (I’m going to use ‘TinkerHAB’).  To do this use ‘armbian-config’ at the command line.  (If it’s not found try running ‘apt-get install armbian-config’ and try again).  When done log out and back in.

Next install Zulu (java engine) using the following commands:  (instructions based on here)

[sourcecode language=”text”]apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv-keys 0x219BD9C9
echo ‘deb http://repos.azulsystems.com/debian stable main’ > /etc/apt/sources.list.d/zulu.list
apt-get update -qq
apt-get install zulu-embedded-8[/sourcecode]

When done confirm it has been installed by entering ‘java -version’.

Next install OpenHAB using the following commands: (instructions based on here)

[sourcecode language=”text”]wget -qO – ‘https://bintray.com/user/downloadSubjectPublicKey?username=openhab’ | sudo apt-key add –
apt-get install apt-transport-https
echo ‘deb https://dl.bintray.com/openhab/apt-repo2 stable main’ | sudo tee /etc/apt/sources.list.d/openhab2.list
apt-get update
apt-get install openhab2
apt-get install openhab2-addons
systemctl daemon-reload
systemctl enable openhab2.service
systemctl start openhab2.service[/sourcecode]

If all worked you should be able to log in to OpenHAB in your browser:

  • http://192.168.x.x:8080

Congrats!

Here is a good time to shut down and make a backup of the SD Card with Win32DiskImager.

Now on to MQTT (Mosquitto).  Enter the following command: (instructions based on here)

[sourcecode language=”text”]wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
apt-key add mosquitto-repo.gpg.key
echo ‘deb http://repo.mosquitto.org/debian stretch main’ > /etc/apt/sources.list.d/mosquitto.list
apt-get update
apt-get install mosquitto mosquitto-clients[/sourcecode]

Next we will add a username and password to Mosquitto. (instructions based on here)

Log in again and change to your home folder:

[sourcecode language=”text”]cd ~[/sourcecode]

Create a text file with a login name and password:

[sourcecode language=”text”]echo ‘username:password’ > passwd[/sourcecode]

Encrypt the file:

[sourcecode language=”text”]mosquitto_passwd -U passwd[/sourcecode]

Move the file to the mosquitto folder:

[sourcecode language=”text”]mv passwd /etc/mosquitto/[/sourcecode]

Using nano (or whatever editor you like) edit ‘mosquitto.conf’ file in ‘/etc/mosquitto’

[sourcecode language=”text”]nano /etc/mosquitto.conf[/sourcecode]

Add the following text just after the comments:

[sourcecode language=”text”]password_file /etc/mosquitto/passwd
allow_anonymous false[/sourcecode]

That’s pretty much it for the setup – anything more will be configuration.  This would be another good point to shutdown and backup the SD Card.

 

Installing node.js and cncjs on an Orange Pi Zero H2+

Based on instructions found here!
And here!

Armbian is based on Debian/Ubuntu. For cncjs it is recommended to use Nodejs 6.x so start by adding the Nodejs 6.x repository:
[sourcecode language=”text”]curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -[/sourcecode]

Install Node.js using:
[sourcecode language=”text”]sudo apt-get install nodejs[/sourcecode]

If installed correctly then “node -v” should display v6.11.3

The only way I managed to get cncjs installed was to run:
[sourcecode language=”text”]sudo npm install –unsafe-perm -g cncjs[/sourcecode]

To start at boot I added the following to /etc/rc.local before the exit 0:
[sourcecode language=”text”]sudo /usr/lib/node_modules/cncjs/bin/cnc[/sourcecode]

Yay!

BTSync with Orange Pi

This is a work in progress…

BTSync (now known as Resilio Sync Home) is a fantastic tool for syncing files between devices.  Here’s my diary on installing it onto a little Orange Pi One running Armbian.

First download the Armbian image and copy it to an SD card using Win32 Disk Imager.  Install as recommended setting a root password and sudo user.  Once set up you can use Putty to access the device.

I’ve found it simplest to use windows to prep the USB drive.  Plug it in and use DISKPART – select the partition and use CLEAN.

Once cleaned format the drive NTFS.

Remove the drive and plug it into the Orange Pi.  Ensure it’s working using lsblk:

With the disk formatted we can mount it. First create a folder in /mnt – I’ll use windrv (since we want to read it ultimately in Windows):

root@orangepione:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 3.7T 0 disk
├─sda1 8:1 0 128M 0 part
└─sda2 8:2 0 3.7T 0 part
mmcblk0 179:0 0 14.5G 0 disk
└─mmcblk0p1 179:1 0 14.3G 0 part /

mkdir /mnt/windrv

Then mount it using mount:

mount -t ntfs-3g /dev/sda1 /mnt/windrv

If this was successful we can add it to /etc/rc.local (I was having problems with /etc/fstab – seems the drive may not be ready when fstab loads)

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0” on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

mount -t ntfs-3g /dev/sda2 /mnt/windrv

exit 0

Next we can install Samba for sharing folders:

apt-get install samba

This will take a little while. Once installed we need a user for BTSync, create one and give it a password:

root@orangepione:/dev# adduser btsync
Adding user `btsync’ …
Adding new group `btsync’ (1001) …
Adding new user `btsync’ (1001) with group `btsync’ …
Creating home directory `/home/btsync’ …
Copying files from `/etc/skel’ …
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for btsync
Enter the new value, or press ENTER for the default
Full Name []: btsync user
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y

Create a folder to share in /mnt/windrv – I’ll use Video:

mkdir /mnt/windrv/Video

With the drive mounted and Samba installed we can now add a share to Samba. Edit /etc/samba/smb.conf and add to the bottom of the file:

[Video]
comment = Video Share
path = /mnt/windrv/Video
available = yes
browsable = yes
guest ok = yes
read only = yes
write list = btsync
create mask = 0755

This will create a share with guest read only and write access to btsync. Reload the Samba service:

service smbd restart

And then test in Windows in a file explorer to \\192.168.xxx.xxx\Video.

Now we need to install Resilio Sync. First add the repository to the list:
(info here: https://help.resilio.com/hc/en-us/articles/206178924-Installing-Sync-package-on-Linux)

vi /etc/apt/sources.list

deb http://linux-packages.resilio.com/resilio-sync/deb resilio-sync non-free
~

Slightly different for arm64:

deb [arch=armhf] http://linux-packages.resilio.com/resilio-sync/deb resilio-sync non-free

Then add the Resilio key so we can install packages – change to the tmp folder first as the key is not necessary once done:

cd /tmp
wget https://linux-packages.resilio.com/resilio-sync/key.asc
apt-key add key.asc

Install Resilio Sync:

apt update
apt install resilio-sync

Slightly different install for Arm64:

dpkg –add-architecture armhf
apt update
apt install resilio-sync libc6:armhf

Start Resilio Sync and set it so it autostarts:

systemctl start resilio-sync
systemctl enable resilio-sync

All should now be working. Check by browsing to http:\\OPSync:8888

Below is some extra stuff for an Orange Pi Zero and enabling the Access Point:

Info found here:
https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/

Below is even more on installing Plex:

Info found here:
https://www.htpcguides.com/install-plex-media-server-on-orange-pi-devices/

Found this for Pine64:
http://jez.me/article/plex-server-on-a-pine64-how-to

sudo su –
apt-get install apt-transport-https
wget -O – https://dev2day.de/pms/dev2day-pms.gpg.key | apt-key add –
echo “deb https://dev2day.de/pms/ jessie main” >/etc/apt/sources.list.d/pms.list
dpkg –add-architecture armhf
apt-get update
apt-get install binutils:armhf
apt-get install –no-install-recommends plexmediaserver-installer

Some stuff about OpenVPN client:
https://askubuntu.com/questions/229800/how-to-auto-start-openvpn-client-on-ubuntu-cli

Copy the *.ovpn and *.txt to /etc/openvpn – the text file is named in auth-user-pass *.txt as:

your_server_user_name
your_server_passowrd
Save and Close
sudo nano OpenVPNConfigFile.ovpn

Rename the ‘.ovpn’ to ‘.conf’

sudo nano /etc/default/openvpn

Uncomment AUTOSTART=”all”
sudo service openvpn start

Oh, and add ‘keepalive 10 120’ or similar to the configuration file.

Final note: From time to time you may get a certificate error to fix the public key will need to be updated…

wget -qO – https://linux-packages.resilio.com/resilio-sync/key.asc | sudo apt-key add –

Creating an OctoPrint Atom Server

This is a work in progress – not complete yet…

  1. Optional: Go through the Windows install on the NUC and setup (and updated) the pre-installed Windows 10 Home – once fully updated I created a full disk backup ISO using the Windows tools.  I’ll probably never use this but I think of it as a safety net.
  2. Download the linux distribution.  For this I’m using Ubuntu 16.04 LTS (64 bit) – this was current at the time of writing.  Download the iso to your local disk (ubuntu-16.04.2-server-amd64.iso).
  3. Create a bootable install USB stick.  I’ve tried a number of tools but Rufus 2.15 had the best success.
  4. Boot up the NUC with the USB stick – you may need to head into the bios and select the boot drive.
  5. Install Ubuntu.  There’s many tutorials on how to do this – only ‘additional’ package in installed was SSH.
  6. Reboot and login with your own account.
  7. Enable root and root SSH.  First you need to give the root account a password:
    [sourcecode language=”text”]sudo passwd root[/sourcecode]
    and then edit the ssh config with ‘vi’:
    [sourcecode language=”text”]sudo vi /etc/ssh/sshd[/sourcecode]
    find the ‘PermitRootLogin’ line, press ‘Insert’ key to edit, and change it to:
    [sourcecode language=”text”]PermitRootLogin yes[/sourcecode]
    press ‘Esc’ key to exit edit then ‘:w’ to write and ‘:q’ to exityou can restart the service but I find it just a quick to reboot:[sourcecode language=”text”]sudo reboot[/sourcecode]
  8. Move the NUC to where you want it to go and attach power and ethernet.  At the time of writing I was unable to get WiFi working but this may change in the future.
  9. Download and install WinSCP and Putty.
  10. Optional: I like to set a reservation for the NUC in your DHCP (router) – this way you’ll always have the same IP for the NUC.
  11. Start WinSCP and login as root, also start Putty and login as root.


  12. From time to time you’ll need to update Ubuntu – we’ll do it now:
    [sourcecode language=”text”]apt-get update[/sourcecode]
    and
    [sourcecode language=”text”]apt-get upgrade[/sourcecode]
    Note that you don’t need sudo since you’re logged in as root.

The ‘Computer Room’

Here’s the home man cave.   Lars is running an i5, beside him is a spare first gen i7, and I’m sporting an i7-6700.

My desk is the hobby shop with shelves of electronic parts, a bench power supply, soldering station and a number of meters – hoping for an oscilloscope in the near future.

Currently have three 3D printers all web enabled on OctoPrint (which was set up on an Atom X5-Z8350 NUC)

Have a few projects underway.  There’s a toster oven re-flow soldering oven, a single board BT Sync device and a new Hypercube printer (making improvements from the first).