title: 阿里云服务器搭建 Nginx+rtmp 推流服务器
categories:[Centos]
tags:[音视频编程]
date: 2021/11/16
<div align = ‘right’> 作者:hackett</div>
<div align = ‘right’> 微信公众号:加班猿 </div>
一、后期筹备
服务器操作系统: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/…,下载完一路装置就行
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
如果你感觉文章还不错,能够给个 ”三连“
我是 加班猿,咱们下期见