阿里云服务器搭建Nginx+rtmp推流服务器

本文档详细介绍了如何在阿里云CentOS服务器上搭建Nginx并集成RTMP模块,实现音视频流推流服务。首先,通过yum安装依赖,然后下载Nginx和RTMP模块源码进行编译安装。接着,配置Nginx以支持RTMP,并设置开机启动。最后,测试推流和拉流功能,确保服务正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

title: 阿里云服务器搭建Nginx+rtmp推流服务器

categories:[Centos]

tags:[音视频编程]

date: 2021/11/16

一、前期准备

服务器操作系统:CentOS Linux release 8.4.2105

Nginx版本:nginx-1.18.0.tar.gz

RTMP模块:nginx-rtmp-module

推流工具:OBS-Studio/VLC

拉流工具:VLC

二、搭建编译环境

1.安装依赖

新建的服务器先安装一些依赖,或者编译的时候看错误需要什么就安装什么

sudo yum install gcc make pcre pcre-devel openssl openssl-devel
2.下载nginx-1.18.0.tar.gz和nginx-rtmp-module
wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz #解压
git clone https://github.com/arut/nginx-rtmp-module
3.配置和编译安装
#nginx源码文件夹和rtmp模块源码文件夹在同一目录下
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx  --add-module=../nginx-rtmp-module  --with-http_ssl_module
sudo make
sudo make install
4.查看安装结果
 /usr/local/nginx/sbin/nginx -v
 #输出nginx version: nginx/1.18.0即为安装成功

三、配置Nginx

1.设置Nginx开机启动

创建Nginx服务文件

vim /usr/lib/systemd/system/nginx.service

创建Nginx服务文件,输入以下内容

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

启动Nginx服务

sudo systemctl start nginx 
sudo systemctl enable nginx 

2.修改Nginx的配置文件

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

修改前

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


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

    #server
    #{
    #       listen 8080;
    #       location /stat{
    #           rtmp_stat all; #所有状态
    #           rtmp_stat_stylesheet stat.xsl #state的样式表
    #           }
    #       location /stat.xsl{
    #           root /root/workspace/tmp/rtmp/nginx-rtmp-module;#state的样式表路径
    #   }


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


修改后

rtmp_auto_push on;

rtmp {
    server {
        listen 1935;
        application live {
            live on;
            hls on;
            hls_fragment 3s;
            hls_playlist_length 10s;
            hls_path /usr/local/nginx/html/hls;
        }
        application hls {
            live on;
            hls on;
            hls_cleanup off;
            hls_fragment 3s;
            hls_playlist_length 10s;
            hls_path /usr/local/nginx/html/hls;
        }
    }
}
#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;
}


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

    #server
    #{
    #       listen 8080;
    #       location /stat{
    #           rtmp_stat all; #所有状态
    #           rtmp_stat_stylesheet stat.xsl #state的样式表
    #           }
    #       location /stat.xsl{
    #           root /root/workspace/tmp/rtmp/nginx-rtmp-module;#state的样式表路径
    #   }


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

3.重启Nginx,开启Centos端口

开启1935/80/8080端口

sudo firewall-cmd --add-port=1935/tcp --permanent
sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload 

阿里云后台开启对应的端口
在这里插入图片描述

重启ngxin服务

 sudo systemctl restart nginx   

浏览器输入服务器IP出现Welcome to nginx!,nginx配置结束

在这里插入图片描述

四、测试

1.下载OBS-Studio

官网地址:https://obsproject.com/zh-cn/download,下载完一路安装就行

2.下载VLC

官网地址:https://www.videolan.org/,下载完一路安装就行

3.设置OBS-Studio的推流地址

打开OBS,在 文件 -> 设置 -> 推流 填入 rtmp://服务器IP:1935/live/

在这里插入图片描述

设置好了OBS-Studio添加一个屏幕捕获,点击开始推流

在这里插入图片描述

4.VLC拉流

打开VLC,媒体 -> 打开网络串流 -> 网络

输入:rtmp://服务器IP:1935/live/demo

​ http://服务器IP/hls/demo.m3u8

在这里插入图片描述

### 如何在阿里云ECS上安装配置RTMP流媒体服务器 #### 准备工作 为了确保顺利部署,在开始之前需确认已准备好一台运行CentOS 7系统的阿里云ECS实例,并获取root权限以便执行必要的命令操作。 #### 安装依赖库 首先更新现有软件包列表并安装编译所需的各种工具和库文件,这一步骤对于后续正确编译Nginx及其RTMP模块至关重要[^3]: ```bash yum update -y && yum install -y gcc make pcre-devel zlib-devel openssl-devel wget unzip ``` #### 获取Nginx源码及RTMP模块 接着下载最新版本的Nginx压缩包以及官方维护的`nginx-rtmp-module`插件,解压后放置于合适位置等待进一步处理[^2]: ```bash cd /usr/local/src/ wget http://nginx.org/download/nginx-1.20.1.tar.gz tar zxvf nginx-1.20.1.tar.gz git clone https://github.com/arut/nginx-rtmp-module.git ``` #### 编译安装自定义版Nginx 通过指定额外参数来启用对RTMP的支持,完成定制化构建过程;之后设置开机启动项以保障重启后仍能自动加载服务程序: ```bash cd nginx-1.20.1 ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module make && make install echo "[Unit] Description=NginX HTTP and RTMP server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target" > /etc/systemd/system/nginx.service systemctl enable nginx ``` #### 配置Nginx实现基本直播功能 编辑位于`/usr/local/nginx/conf/nginx.conf`中的默认配置文档,添加如下所示的应用层逻辑描述片段用于开启实时多媒体协议(RTMP)通道监听端口1935并向客户端提供拉流传送接口: ```conf rtmp { server { listen 1935; chunk_size 4096; application live { live on; record off; # HLS settings (optional) hls on; hls_path /tmp/hls; hls_fragment 5s; } } } ``` #### 测试验证成果 此时可以尝试使用OBS Studio等第三方应用程序连接至新建立的服务节点实施送动作,与此同时借助VLC Player观看由上述流程产生的在线广播内容。如果一切正常,则说明整个环境已经搭建完毕并且处于可用状态[^4]。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加班猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值