前言
目录
下面我们介绍使用nginx rtmp模块构建媒体服务器
安装环境
udo apt-get update
sudo apt-get install build-essential libpcre3-dev zlib1g-dev libssl-dev
下载nginx
下载地址 https://nginx.org/en/download.html
tar -zxvf nginx-1.27.0.tar.gz
cd nginx-1.27.0/
编译安装
./configure \
--with-threads \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-stream \
--with-stream_ssl_module \
--add-module=/root/nginx-rtmp-module
make
sudo make install
# 创建了一个符号链接,将 /usr/local/nginx/sbin/nginx 指向 /usr/local/bin/nginx。这样,你可以在终端中直接使用 nginx 命令来启动或管理 Nginx,而不必输入完整的路径
sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
设置nginx.conf
http{
server{
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /stat {
stub_status on;
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx/html;
}
location /control {
rtmp_control all;
}
}
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
hls off;
}
}
}
移动stat.xsl文件,方便配置
sudo cp /root/nginx-rtmp-module/stat.xsl /usr/local/nginx/html/
重新启动nginx
sudo nginx -s stop
sudo nginx
备用命令
# 查看端口占用情况
sudo lsof -i :80
sudo lsof -i :1935
设置开机启动
编写nginx.service
sudo nano /etc/systemd/system/nginx.service
加入以下内容
[Unit]
Description=nginx rtmp server
After=network.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
Restart=on-failure
User=root
[Install]
WantedBy=multi-user.target
重新加载使其生效
sudo systemctl daemon-reload
设置开机启动
sudo systemctl enable nginx
验证开机启动
sudo reboot
最终效果
使用VLC或者OBS播放NginxRTMP上面的流数据