参考:
How to Install Nginx with RTMP Module on CentOS 8 | Atlantic.Net
How to Install Nginx with RTMP Module on CentOS 7
How to Install Nginx with RTMP Module on CentOS 7(另一个网站)
- 一个看起来简单的办法
不需要编译安装,似乎容易,但是没试成功,看起来简单,但是需要看E文一步一步做,其实也不能直接安装成功,感觉也不太好,还是用全编译安装的方式来得实在:
Install NGINX RTMP module in CentOS/RHEL or Amazon Linux
安装nginx
如果已经安装有nginx先要卸载掉
systemctl stop nginx
systemctl disable nginx
yum remove nginx
不能偷懒哦,需要编译安装nginx,带上nginx-rtmp-module和相关组件,以前nginx都是yum install这样来安装,但是玩高深的东西就要用高深的操作来做,是的
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
wget https://www.zlib.net/zlib-1.2.11.tar.gz
wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
# 解压这些下载的文件
tar -xzf nginx-1.18.0.tar.gz
unzip pcre-8.42.zip
tar -xzf zlib-1.2.11.tar.gz
tar -xzf openssl-1.1.0h.tar.gz
获取nginx-rtmp-module
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
编译前,安装一些必要的模块
yum install wget git unzip perl perl-devel perl-ExtUtils-Embed \
libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel \
pcre-devel GeoIP GeoIP-devel gd gd-devel libgeoip-dev -y
带包编译安装
cd nginx-1.18.0
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--build=CentOS \
--builddir=nginx-1.18.0 \
--with-select_module \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-compat \
--with-pcre=../pcre-8.42 \
--with-pcre-jit \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.0h \
--with-openssl-opt=no-nextprotoneg \
--add-module=../nginx-rtmp-module \
--with-debug
编译安装
make
make install
配置nginx成为服务
nano /lib/systemd/system/nginx.service
把下面这段拷贝进文件
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
设置服务启动
systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
这样nginx基本安装完成
在nginx配置文件设置rtmp组件
nano /etc/nginx/nginx.conf
添加如下内容
# RTMP configuration
rtmp {
server {
listen 1935; # Listen on standard RTMP port
chunk_size 4000;
# Define the Application
application show {
live on;
# Turn on HLS
hls on;
hls_path /mnt/hls/;
hls_fragment 3;
hls_playlist_length 60;
# disable consuming the stream from nginx as rtmp
deny play all;
}
}
}
这时如果重启nginx没有问题,证明前面的安装已经正常了
systemctl restart nginx
如果出现错误提示,请重新检查上面的安装步骤,centos的nginx的默认错误信息保存在/var/log/nginx/error.log文件中,可以用命令行进行查看
tail -30 /var/log/nginx/error.log
创建第一个应用
nano /etc/nginx/nginx.conf
在配置rtmp的server段中添加如下内容
# RTMP video on demand for mp4 files
application vod {
play /mnt/mp4s;
}
# RTMP stream using OBS
application stream {
live on;
}
保存文件后,添加相应目录和权限
mkdir /mnt/mp4s
chown -R nginx:nginx /mnt/mp4s
配置好nginx后,需要检查配置之后再重启nginx,养成良好习惯,如果结果是成功的,再重启nginx
nginx -t
结果返回正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启nginx
systemctl restart nginx
基本测试
拷贝文件到/mnt/mp4s目录,使用VLC media player进行测试,操作:
菜单:媒体->打开网络串流,输入
rtmp://服务器IP:1935/vod/例子文件.mp4
注意:要把"例子文件.mp4"拷贝进/mnt/mp4s目录下
如果出现如下界面,说明基本的流服务器搭建成功了
推流测试
服务器端配置/etc/nginx/nginx.conf的rtmp.server段中添加如下配置
application song {
live on;
interleave on;
hls on;
hls_path /tmp/hls;
hls_fragment 15s;
record_unique on;
}
创建目录与授权
mkdir /tmp/hls
chown -R nginx:nginx /tmp/hls
检查配置与重启服务器
nginx -t
systemctl restart nginx
基本服务器搭建成功后,下面进行推流的测试,主要用OBS Studio工具进行推流,并检测实时推流的成果
安装OBS Studio前先要安装必要的系统组件:DirectX End-User Runtime
使用VCL打开串流 rtmp://17.154.216.128:1957/song/12345677 ,如果正常,说明实时推流配置成功
安装流媒体组件
注意,不能直接安装
yum install ffmpeg
会提示
No package ffmpeg available.
CentOS 7 需额外安装扩展源:
mkdir centosExt
cd centosExt
wget https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
wget https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
rpm -ivh *.rpm
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
这样就可以安装了
yum -y install ffmpeg ffmpeg-devel