介绍下如何在本地CentOS 7中使用nginx-rtmp-module来搭建基本的RTMP和HLS直播服务器。
基于如下情况进行搭建:

  • 系统版本: CentOS 7.6 Minimal
  • 虚拟机IP地址: 192.168.168.199
  • Nginx版本:1.14.2
  • Nginx Rtmp模块: nginx-rtmp-module
  • 如下安装步骤,如果没有特别说明,都是基于root用户进行安装。

安装nginx

首先更新CentOS系统

1
yum update

安装编译nginx所需的开发包

1
2
3
4
yum install gcc
yum install pcre pcre-devel pcre-static pcre-tools
yum install openssl openssl-static openssl-devel
yum install wget unzip

下载nginx和nginx-rtmp-module

1
2
3
4
wget http://nginx.org/download/nginx-1.14.2.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
tar -zxvf nginx-1.14.2.tar.gz
unzip master.zip

编译和安装nginx

1
2
3
4
cd nginx-1.14.2
./configure --with-http_ssl_module --with-http_secure_link_module --add-module=../nginx-rtmp-module-master
make
sudo make install

make install后,nginx默认会安装在/usr/local/nginx/目录下。

安装ffmpeg

安装ffmpeg可以选择静态包,自编译或者rpmfusion.org的yum源。具体参照http://ffmpeg.org/download.html#build-linux
此处选择rpmfusion.org的yum源方式安装

1
2
yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
yum install ffmpeg

安装后,可以查看ffmpeg的版本,此处版本是3.4.8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# ffmpeg
ffmpeg version 3.4.8 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-39)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --docdir=/usr/share/doc/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --extra-cflags=' ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-fontconfig --enable-gcrypt --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libcdio --enable-libdrm --enable-indev=jack --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libmp3lame --enable-nvenc --enable-openal --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libopus --disable-encoder=libopus --enable-libpulse --enable-librsvg --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libvidstab --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzvbi --enable-avfilter --enable-avresample --enable-libmodplug --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-libmfx --enable-runtime-cpudetect
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
Use -h to get full help or, even better, run 'man ffmpeg'
[root@localhost ~]#

配置Nginx

修改/usr/local/nginx/conf/nginx.conf配置文件中pid的配置为/var/run/nginx.pid

1
pid /var/run/nginx.pid;

创建nginx日志目录

1
mkdir -p /var/log/nginx/

创建SystemD自启动文件/lib/systemd/system/nginx.service为如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target

nginx启动相关命令

1
2
3
systemctl enable nginx.service # 设置开机自启动
systemctl start nginx.service # 设置启动nginx
systemctl stop nginx.service # 停止nginx

添加rtmp配置

添加配置

/usr/local/nginx/conf/nginx.conf中添加rtmp相关的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
rtmp_auto_push on;
rtmp {
server {
listen 1935;
notify_method get;
access_log /var/log/nginx/access.log;
chunk_size 4096;
application live {
# 开启直播模式
live on;
# 允许从任何源push流
allow publish all;
# 允许从任何地方来播放流
allow play all;
# 20秒内没有push,就断开链接。
drop_idle_publisher 20s;
}
}
}

检查nginx配置后reload nginx,使之生效。

1
2
/usr/local/nginx/sbin/nginx -t
systemctl reload nginx

添加后的配置参见: nginx_with_basic_rtmp.conf

试验

配置完后,可以使用ffmpeg和ffplay进行试验。
首先,在本机上使用ffmpeg读取mp4文件,推流到虚拟机上部署的直播服务。

1
ffmpeg -re -i xiaozhupeiqi.mp4 -c copy -f flv 'rtmp://192.168.168.199/live/xiaozhupeiqi'

再在本机上使用ffplay播放刚推送的地址,

1
ffplay 'rtmp://192.168.168.199/live/xiaozhupeiqi'

就可以看到rtmp流了。Hello, Peppa.
nginx_rtmp_ffplay_peppa.jpg

添加hls配置

配置hls需要有两个步骤

  1. 配置rtmp流产生hls文件
  2. 设置nginx来访问hls文件

配置rtmp流产生hls文件

创建存放hls文件的目录, 确保nobody用户可以读写该目录。

1
2
mkdir -p /usr/local/nginx/hls/
chown nobody:root /usr/local/nginx/hls/

修改配置,增加hls相关配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
rtmp_auto_push on;
rtmp {
server {
listen 1935;
notify_method get;
access_log /var/log/nginx/access.log;
chunk_size 4096;
application live {
# 开启直播模式
live on;
# 允许从任何源push流
allow publish all;
# 允许从任何地方来播放流
allow play all;
# 20秒内没有push,就断开链接。
drop_idle_publisher 20s;
# 开启HLS
hls on; # Enable HTTP Live Streaming
# Pointing this to an SSD is better as this involves lots of IO
# 设置hls存放目录
hls_path /usr/local/nginx/hls/;
}
}
}

添加了如下两个配置:

  • hls on : 开启HLS
  • hls_path /usr/local/nginx/hls/ : 设置hls目录

此时使用ffmpeg进行推流后,在/usr/local/nginx/hls/目录下,就会有HLS要用到的m3u8和ts文件。

1
2
3
4
5
6
7
# ll
total 3364
-rw-r--r-- 1 nobody nobody 834532 Jul 29 04:19 xiaozhupeiqi-0.ts
-rw-r--r-- 1 nobody nobody 571144 Jul 29 04:19 xiaozhupeiqi-1.ts
-rw-r--r-- 1 nobody nobody 1055244 Jul 29 04:19 xiaozhupeiqi-2.ts
-rw-r--r-- 1 nobody nobody 162 Jul 29 04:19 xiaozhupeiqi.m3u8
#

设置nginx来访问hls文件

配置nginx通过http访问hls文件, 在server中添加如下location。

1
2
3
4
5
6
7
8
9
10
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/local/nginx/;
add_header Cache-Control no-cache; # Prevent caching of HLS fragments
add_header Access-Control-Allow-Origin *; # Allow web player to access our playlist
}

检查nginx配置后reload nginx,使之生效。

1
2
/usr/local/nginx/sbin/nginx -t
systemctl reload nginx

访问试验

然后使用支持HLS的浏览器访问http://192.168.168.199/hls/xiaozhupeiqi.m3u8,即可以HLS方式观看。
nginx_rtmp_hls_peppa.jpg

添加后的配置参见: nginx_with_basic_rtmp_and_hls.conf

Reference

相关阅读

留言