[转/魔改]自建RTMP私服并推流直播的方式

原文(Ubuntu版)地址:https://io.backrunner.top/2018/05/13/自建RTMP私服直播的方式.html

本文为Centos7版

0x00 建服

首先需要一个Linux服务器,这里因为OpenVZ的Ubuntu系统自带一个删不掉的Apache所以只能用Centos7。
建服需要执行下列命令:

yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel git

安装好之后,在Nginx官网下载源码并解压,

cd /home
wget http://nginx.org/download/nginx-1.13.12.tar.gz
tar -xzvf nginx-1.13.12.tar.gz

下载nginx-rtmp-module

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

配置,编译

cd nginx-1.13.12
./configure --add-module=/home/nginx-rtmp-module
make
make install

在/usr/local/nginx中找到nginx.conf

cd /usr/local/nginx
vim conf/nginx.conf

配置文件如下:
nginx.conf

worker_processes 1;

events{
    use epoll;
}

rtmp {
    server { 
        listen 1935;
        chunk_size 4000;

        application live{
            live on;
			record off;
        }
		
		application hls{
			live on;
			hls on;
			hls_path /tmp/hls;
		}
    }
}

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen 80;
        server_name [YOUR_SERVERNAME];

		location /stat {
			rtmp_stat all;
			rtmp_stat_stylesheet stat.xsl;
		}

		location /stat.xsl {
			root /home/nginx-rtmp-module/;
		}

		location / {
			root html;
			index index.html index.htm;
		}
		
		location /hls{
			types{
				application/vnd.apple.mpegurl m3u8;
				video/mp2t ts;
			}
			root /tmp;
			add_header Cache-Control no-cache;
		}
		
		error_page 500 502 503 504 /50x.html;
		location = /50x.html {
			root html;
		}
	}
}

注意替换配置文件中带有中括号的内容
启用nginx,访问[SERVERNAME]/stat,可看到统计页面
启用nginx的命令如下(目录定位在/usr/local/nginx):

./sbin/nginx

reload命令如下:

./sbin/nginx -s reload

0x01 推流

请参考原文:https://io.backrunner.top/2018/05/13/自建RTMP私服直播的方式.html