ustoopia

Setup a Nginx live-stream server with a HLS video player on

Jul 13th, 2020
1,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.61 KB | None | 0 0
  1. sudo apt update
  2. sudo apt upgrade
  3.  
  4. sudo su
  5. passwd
  6. [enter new password twice]
  7.  
  8. sudo hostnamectl set-hostname YOUR.DOMAIN.COM
  9.  
  10. apt-get install wget unzip software-properties-common dpkg-dev git make gcc automake build-essential zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libxml2-dev libgd-dev libgeoip-dev libgoogle-perftools-dev libperl-dev pkg-config autotools-dev gpac ffmpeg mediainfo mencoder lame libvorbisenc2 libvorbisfile3 libx264-dev libvo-aacenc-dev libmp3lame-dev libopus-dev unzip
  11.  
  12. apt install nginx -y
  13. apt install libnginx-mod-rtmp -y
  14.  
  15. apt install php7.3 php7.3-common php7.3-fpm php7.3-gd php7.3-mysql php7.3-imap php7.3-cli php7.3-cgi php7.3-curl php7.3-intl php7.3-pspell php7.3-recode php7.3-sqlite3 php7.3-tidy php7.3-xmlrpc php7.3-xsl php-memcache php-imagick php-gettext php7.3-zip php7.3-mbstring php-pear mcrypt imagemagick libruby memcached
  16.  
  17. sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.3/fpm/php.ini
  18. sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 1024M/g' /etc/php/7.3/fpm/php.ini
  19. sed -i 's/max_input_time = 60/max_input_time = 300/g' /etc/php/7.3/fpm/php.ini
  20. sed -i 's/max_execution_time = 30/max_execution_time = 60/g' /etc/php/7.3/fpm/php.ini
  21.  
  22. sed -i 's/;date.timezone =/date.timezone = "Europe\/Amsterdam"/g' /etc/php/7.3/fpm/php.ini
  23.  
  24. systemctl restart php7.3-fpm
  25.  
  26. apt install mariadb-server mariadb-client phpmyadmin
  27.  
  28. mysql_secure_installation
  29.  
  30. systemctl restart mysql
  31.  
  32. mysql -u root -p
  33. [enter the password you set earlier]
  34.  
  35. CREATE DATABASE wordpress;
  36. grant all privileges on wordpress.* TO 'wordpress'@'localhost' identified by 'YourPassword';
  37. FLUSH PRIVILEGES;
  38.  
  39. systemctl restart mysql
  40.  
  41. mkdir -p /var/www/yourhostname
  42.  
  43. ln -s /usr/share/phpmyadmin /var/www/yourhostname/phpmyadmin
  44.  
  45. chown -R www-data: /var/www/yourhostname
  46.  
  47. cd /usr/src
  48. git clone https://github.com/arut/nginx-rtmp-module
  49.  
  50. cp /usr/src/nginx-rtmp-module/stat.xsl /var/www/yourhostname/stat.xsl
  51. cp /usr/src/nginx-rtmp-module/stat.xsl /var/www/html/stat.xsl
  52.  
  53. nano /var/www/html/crossdomain.xml
  54.  
  55. <?xml version="1.0"?>
  56. <!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
  57. <cross-domain-policy>
  58. <allow-access-from domain="*"/>
  59. </cross-domain-policy>
  60.  
  61. cp /var/www/html/crossdomain.xml /var/www/yourhostname/crossdomain.xml
  62.  
  63. nano /var/www/yourhostname/phpinfo.php
  64.  
  65. <?php phpinfo(); ?>
  66.  
  67. chown -R www-data:www-data /var/www/yourhostname
  68. chown -R www-data:www-data /var/www/html
  69.  
  70. nano /etc/nginx/nginx.conf
  71.  
  72. --------------------------------
  73. user www-data;
  74. worker_processes 1;
  75. pid /run/nginx.pid;
  76. include /etc/nginx/modules-enabled/*.conf;
  77.  
  78. events {
  79. worker_connections 768;
  80. # multi_accept on;
  81. }
  82.  
  83. http {
  84. sendfile on;
  85. tcp_nopush on;
  86. tcp_nodelay on;
  87. keepalive_timeout 65;
  88. types_hash_max_size 2048;
  89. # server_tokens off;
  90. # server_names_hash_bucket_size 64;
  91. # server_name_in_redirect off;
  92. include /etc/nginx/mime.types;
  93. default_type application/octet-stream;
  94.  
  95. gzip off;
  96. # gzip_vary on;
  97. # gzip_proxied any;
  98. # gzip_comp_level 6;
  99. # gzip_buffers 16 8k;
  100. # gzip_http_version 1.1;
  101. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  102.  
  103. access_log /var/log/nginx/access.log;
  104. error_log /var/log/nginx/error.log;
  105.  
  106. include /etc/nginx/conf.d/*.conf;
  107. include /etc/nginx/sites-enabled/*;
  108. }
  109.  
  110. rtmp {
  111. access_log /var/log/nginx/rtmp_access.log;
  112. server {
  113. listen 1935;
  114. chunk_size 8192;
  115.  
  116. application live {
  117. live on;
  118. meta on;
  119. record off;
  120. interleave off;
  121. wait_key on;
  122. wait_video off;
  123. idle_streams off;
  124. sync 300ms;
  125. session_relay on;
  126. max_connections 1000;
  127. allow publish all;
  128. allow play all;
  129. hls off;
  130. dash off;
  131.  
  132. # on_publish http://yourdomain.com/plugin/Live/on_publish.php;
  133. # on_play http://yourdomain/plugin/Live/on_play.php;
  134. # on_record_done http://yourdomain/plugin/Live/on_record_done.php;
  135.  
  136. push rtmp://localhost/hls;
  137. push rtmp://localhost/dash;
  138.  
  139. }
  140. application hls {
  141. live on;
  142. record off;
  143. meta copy;
  144. allow publish 127.0.0.1;
  145. allow play all;
  146.  
  147. hls on;
  148. hls_nested on;
  149. hls_cleanup on;
  150. hls_sync 100ms;
  151. hls_fragment 2s;
  152. hls_playlist_length 10s;
  153. hls_path /var/livestream/hls;
  154. }
  155. application dash {
  156. live on;
  157. record off;
  158. allow publish 127.0.0.1;
  159. deny publish all;
  160. allow play all;
  161.  
  162. dash on;
  163. dash_nested on;
  164. dash_cleanup on;
  165. dash_fragment 5s;
  166. dash_playlist_length 20s;
  167. dash_path /var/livestream/dash;
  168. }
  169. application vods {
  170. play /var/livestream/recordings;
  171. allow play all;
  172. }
  173. application vods_http {
  174. play https://yourdomain.com/recordings;
  175. allow play all;
  176. }
  177. }
  178. }
  179. --------------------------------
  180.  
  181. mkdir /var/log/nginx
  182. mkdir -p /var/livestream/hls
  183. mkdir -p /var/livestream/dash
  184. mkdir -p /var/livestream/recordings
  185. chown -R www-data:www-data /var/log/nginx
  186. chown -R www-data:www-data /var/livestream
  187.  
  188. nano /etc/nginx/sites-available/yourhostname.conf
  189.  
  190. ---------------------------------
  191. server {
  192. listen 80;
  193. listen [::]:80;
  194.  
  195. server_name yourhostname;
  196.  
  197. root /var/www/yourhostname;
  198. index index.php index.html index-nginx.html index.htm;
  199.  
  200. add_header Strict-Transport-Security "max-age=63072000;";
  201. add_header X-Frame-Options "DENY";
  202.  
  203. location / {
  204. add_header Cache-Control no-cache;
  205. }
  206. add_header Access-Control-Allow-Origin *;
  207. try_files $uri $uri/ =404;
  208. }
  209. location ~ \.php$ {
  210. include snippets/fastcgi-php.conf;
  211. fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
  212. # fastcgi_pass 127.0.0.1:9000;
  213. }
  214. location /stat {
  215. rtmp_stat all;
  216. rtmp_stat_stylesheet stat.xsl;
  217. #auth_basic Restricted Content;
  218. #auth_basic_user_file .htpasswd;
  219. }
  220. location /stat.xsl {
  221. root html;
  222. }
  223. location /control {
  224. rtmp_control all;
  225. #auth_basic stream;
  226. #auth_basic_user_file .htpasswd;
  227. }
  228. location ~ /\.ht {
  229. deny all;
  230. }
  231. location /hls {
  232. types {
  233. application/vnd.apple.mpegurl m3u8;
  234. video/mp2t ts;
  235. }
  236. autoindex on;
  237. alias /var/livestream/hls;
  238.  
  239. expires -1;
  240. add_header Strict-Transport-Security "max-age=63072000";
  241. add_header Cache-Control no-cache;
  242. add_header 'Access-Control-Allow-Origin' '*' always;
  243. add_header 'Access-Control-Expose-Headers' 'Content-Length';
  244. if ($request_method = 'OPTIONS') {
  245. add_header 'Access-Control-Allow-Origin' '*';
  246. add_header 'Access-Control-Max-Age' 1728000;
  247. add_header 'Content-Type' 'text/plain charset=UTF-8';
  248. add_header 'Content-Length' 0;
  249. return 204;
  250. }
  251. }
  252. location /dash {
  253. types{
  254. application/dash+xml mpd;
  255. video/mp4 mp4;
  256. }
  257. autoindex on;
  258. alias /var/livestream/dash;
  259.  
  260. add_header Strict-Transport-Security "max-age=63072000";
  261. add_header Cache-Control no-cache;
  262. expires -1;
  263. add_header 'Access-Control-Allow-Origin' '*' always;
  264. add_header 'Access-Control-Expose-Headers' 'Content-Length';
  265. if ($request_method = 'OPTIONS') {
  266. add_header 'Access-Control-Allow-Origin' '*';
  267. add_header 'Access-Control-Max-Age' 1728000;
  268. add_header 'Content-Type' 'text/plain charset=UTF-8';
  269. add_header 'Content-Length' 0;
  270. return 204;
  271. }
  272. }
  273. }
  274. ---------------------------------
  275.  
  276. ln -s /etc/nginx/sites-available/yourhostname.conf /etc/nginx/sites-enabled/
  277. nginx -t
  278. systemctl restart nginx
  279.  
  280. apt install python-certbot-nginx
  281.  
  282. certbot --nginx -d yourhostname
  283.  
  284. nginx -t
  285. systemctl restart nginx
  286.  
  287. nano /etc/nginx/sites-available/yourhostname.conf
  288.  
  289. listen 443 ssl http2;
  290. listen [::]:443 ssl http2;
  291. ssl_certificate /etc/letsencrypt/live/yourhostname/fullchain.pem;
  292. ssl_certificate_key /etc/letsencrypt/live/yourhostname/privkey.pem;
  293. include /etc/letsencrypt/options-ssl-nginx.conf;
  294. ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
  295.  
  296. ssl_trusted_certificate /etc/letsencrypt/live/yourhostname/chain.pem;
  297.  
  298. nano /etc/letsencrypt/options-ssl-nginx.conf
  299.  
  300. ---------------------------------
  301. ssl_session_cache shared:le_nginx_SSL:1m;
  302. ssl_session_timeout 1d;
  303. ssl_session_tickets off;
  304.  
  305. ssl_protocols TLSv1.2;
  306. ssl_prefer_server_ciphers on;
  307. ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
  308. ssl_ecdh_curve secp384r1;
  309.  
  310. ssl_stapling on;
  311. ssl_stapling_verify on;
  312.  
  313. add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload;";
  314. add_header Referrer-Policy "no-referrer, strict-origin-when-cross-origin";
  315. add_header X-Frame-Options SAMEORIGIN;
  316. add_header X-Content-Type-Options nosniff;
  317. add_header X-XSS-Protection "1; mode=block";
  318. ----------------------------------
  319.  
  320. https://github.com/videojs/video.js/releases
  321. https://github.com/videojs/http-streaming/releases
  322.  
  323. nano /var/www/yourhostname/livestreamhls.html
  324.  
  325. --------------------------------------
  326. <!DOCTYPE html>
  327. <html>
  328. <head>
  329. <meta charset=utf-8 />
  330. <title>LiveStream</title>
  331. <!--
  332. Uses the latest versions of video.js and videojs-http-streaming.
  333.  
  334. To use specific versions, please change the URLs to the form:
  335.  
  336. <link href="https://unpkg.com/[email protected]/dist/video-js.css" rel="stylesheet">
  337. <script src="https://unpkg.com/[email protected]/dist/video.js"></script>
  338. <script src="https://unpkg.com/@videojs/[email protected]/dist/videojs-http-streaming.js"></script>
  339. -->
  340.  
  341. <link href="https://yourhostname/videojs/video-js.css" rel="stylesheet">
  342. </head>
  343. <body>
  344. <center>
  345. <video-js id="live_stream" class="vjs-default-skin" controls preload="auto" width="auto" height="auto">
  346. <source src="https://yourhostname/hls/stream/index.m3u8" type="application/x-mpegURL">
  347. </video-js>
  348.  
  349. <script src='https://yourhostname/videojs/video.js'></script>
  350. <script src="https://yourhostname/videojs/videojs-http-streaming.js"></script>
  351.  
  352. <script>
  353. var player = videojs('live_stream');
  354. </script>
  355. </center>
  356. </body>
  357. </html>
  358. --------------------------------------
  359.  
  360. cd /var/www/yourhostname
  361. wget https://wordpress.org/latest.tar.gz
  362.  
  363. chown -R www-data:www-data /var/www/yourhostname
  364.  
  365. [videojs_hls url="https://yourhostname/hls/stream/index.m3u8" width="1280" inline="true" autoplay="true"]
  366.  
  367.  
  368. <!-- wp:paragraph -->
  369. <p>Here are all the links in random order that helped me set up this guide.</p>
  370. <!-- /wp:paragraph -->
  371.  
  372. <!-- wp:paragraph -->
  373. <p><a href="https://github.com/videojs/video.js/releases/tag/v7.8.3https://github.com/videojs/http-streaming/releases">https://github.com/videojs/video.js/releases/tag/v7.8.3https://github.com/videojs/http-streaming/releases</a></p>
  374. <!-- /wp:paragraph -->
  375.  
  376. <!-- wp:paragraph -->
  377. <p><a href="https://www.npmjs.com/package/videojs-contrib-dash">https://www.npmjs.com/package/videojs-contrib-dash</a></p>
  378. <!-- /wp:paragraph -->
  379.  
  380. <!-- wp:paragraph -->
  381. <p><a href="https://www.npmjs.com/package/videojs-playlist-ui">https://www.npmjs.com/package/videojs-playlist-ui</a></p>
  382. <!-- /wp:paragraph -->
  383.  
  384. <!-- wp:paragraph -->
  385. <p><a href="https://www.npmjs.com/package/videojs-seek-buttons">https://www.npmjs.com/package/videojs-seek-buttons</a></p>
  386. <!-- /wp:paragraph -->
  387.  
  388. <!-- wp:paragraph -->
  389. <p><a href="https://www.npmjs.com/package/videojs-logo">https://www.npmjs.com/package/videojs-logo</a></p>
  390. <!-- /wp:paragraph -->
  391.  
  392. <!-- wp:paragraph -->
  393. <p><a href="https://www.npmjs.com/package/@leochen1216/videojs-chromecast">https://www.npmjs.com/package/@leochen1216/videojs-chromecast</a></p>
  394. <!-- /wp:paragraph -->
  395.  
  396. <!-- wp:paragraph -->
  397. <p><a href="https://www.npmjs.com/package/videojs-playlist">https://www.npmjs.com/package/videojs-playlist</a></p>
  398. <!-- /wp:paragraph -->
  399.  
  400. <!-- wp:paragraph -->
  401. <p><a href="https://videojs.com/plugins">https://videojs.com/plugins</a></p>
  402. <!-- /wp:paragraph -->
Add Comment
Please, Sign In to add comment