Nginx教程

Centos中安装带rtmp模块的Nginx

本文主要是介绍Centos中安装带rtmp模块的Nginx,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Centos中安装带rtmp模块的Nginx

很多流媒体功能如直播,点播等功能都能够通过Nginx实现。

Nginx官方有一个hls模块实现了HlS协议,但是这个模块为企业版Nginx功能,github上有大佬自己编写了一个开源的nginx-rtmp-module,实现了Nginx官方的hls协议

在Linux系统中安装Nginx时,即可通过二进制编译添加该模块,后续即可以实现rtmp协议。

通过github上的readme指导安装,以下是安装过程,以及一些安装中碰到的小问题

一、Centos安装

参考网上Centos安装教程,默认安装就行了,因为只是作为推流服务器和流媒体服务器使用,所以安装的是最小版本。

二、环境准备

1.编译工具安装

安装好CentOS以后,需要安装各种编译工具,如pcre,gcc等,用于对二进制源码进行编译安装

具体命令如下

yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc gcc-c++ 

安装一些支持工具

如vim,wget和lsof

yum install -y vim wget lsof git zip unzip

###2. 获取Nginx二进制源码

通过测试,rtmp模块暂时只支持Nginx13-15版本,在官网中下载稳定版的Nginx14

wget http://nginx.org/download/nginx-1.14.2.tar.gz

下载nginx-rtmp-module

wget https://github.com/arut/nginx-rtmp-module/archive/refs/tags/v1.2.1.tar.gz

下载源码以后,解压

tar xf v1.2.1.tar.gz
tar xf nginx-1.14.2.tar.gz

配置编译,将rtmp模块添加到nginx中

如果出现./configure: error: no ../nginx-rtmp-module/config was found

去掉--with-http_ssl_module重新配置一次,或者直接重新配置

配置完成后直接进行编译安装

cd nginx-1.14.2
./configure --add-module=../nginx-rtmp-module-1.2.1/ --with-http_ssl_module
make && make install
#建立软链接
ln -s /usr/local/nginx/sbin/nginx /usr/bin

三、安装后配置

1. 启用rtmp配置

#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;
}

rtmp {
        server {
                listen 1935;
                chunk_size 4096;

                application live {
                        live on;
                }

                application hls {
                        live on;
                        hls on;
                        hls_path /usr/local/nginx/hls;
                        hls_fragment 8s;
                }
        }
}

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;
    #    }
    #}

}

2. 开启Nginx服务

nginx

3. 开启端口

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=1935/tcp --permanent

4.验证服务

通过OBS等测试是否成功

这篇关于Centos中安装带rtmp模块的Nginx的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!