centos7+nginx+rtmp+ffmpeg搭建流媒體服務器

1.安裝前須要的工具

#net-tool 查本地IP
#wget 下載安裝包
#unzip 解壓zip包
#gcc gcc-c++ perl 編譯軟件包用
yum install -y net-tools wget unzip gcc gcc-c++ perl

2. 將Centos的yum源更換爲國內的阿里雲源

#備份yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 
#下載阿里源 
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
#清空緩存 
yum makecache

3. 安裝nginx及rtmp

#切換的homme目錄
cd /home

下載並解壓pcre

#下載pcre包
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz
#解壓pcre包
tar -zxvf pcre-8.41.tar.gz

下載並解壓zlib

#下載zlib包
wget http://www.zlib.net/zlib-1.2.11.tar.gz
#解壓zlib包
tar -zxvf zlib-1.2.11.tar.gz

下載並安裝openssl

#下載openssl包 
wget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1i.tar.gz 
#解壓openssl包 
tar -zxvf openssl-1.0.1i.tar.gz 
#切換到openssl裏 
cd openssl-1.0.1i 
#生成配置文件 默認配置 
./config
#編譯程序 
make 
#安裝程序 
make install

下載並解壓nginx-rtmp-model

#下載rtmp包
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
#解壓下載包
unzip -o master.zip
#修改文件夾名
mv master nginx-rtmp-module

安裝nginx

#下載
nginx wget http://nginx.org/download/nginx-1.12.2.tar.gz 
#解壓
nignx tar -zxvf nginx-1.12.2.tar.gz 
#切換到nginx中 
cd nginx-1.12.2 
#生成配置文件,將上述下載的文件配置到configure中 
./configure --prefix=/usr/local/nginx --with-pcre=/home/pcre-8.41 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.0.1i --add-module=/home/nginx-rtmp-module 
#編譯程序 
make 
#安裝程序 
make install 
#查看nginx模塊 
nginx -V

安裝ffmpeg

#安裝epel包
yum install -y epel-release 
#導入簽名 
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 
#導入簽名 
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 update -y 
#安裝ffmpeg 
yum install -y ffmpeg 
#檢查版本 
ffmpeg -version

4. 修改配置運行服務

修改nginx配置

vi /usr/local/nginx/conf/nginx.conf

修改以後的配置php

#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閰嶇疆
rtmp{
server{
listen 1935;
application myapp{
live on;
}
application hls{
live on;
hls on;
hls_path /tmp/hls;
}
}
}
#
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;
#
sendfile on;
##tcp_nopush on;
#
##keepalive_timeout 0;
keepalive_timeout 65;
#
gzip on;
#include /usr/local/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
#
##charset koi8-r;
#
##access_log logs/host.access.log main;
#
location / {
root html;
index index.html index.htm;
}
location /hls {
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
##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 configurati
##error_page 404 /404.html;
#
## redirect server error pages to the static page /50x.html
##

執行nginx

/usr/local/nginx/sbin/nginx

關閉防火牆

systemctl stop firewalld
systemctl disable firewalld

測試功能

ffmpeg -re -i ychx.mp4 -vcodec copy -codec copy -f flv rtmp://192.168.199.145/hls/cctv

測試訪問

視頻切片保存位置:/tmp/hls/
m3u8視頻訪問地址:http://192.168.199.145/hls/cctv.m3u8

 重啓nginxhtml

/usr/local/nginx/sbin/nginx -s reload
相關文章
相關標籤/搜索