利用nginx搭建http和rtmp协议的流媒体服务器

利用nginx搭建http和rtmp协议的流媒体服务器


一、准备工作

1、安装依赖包:
yum install gcc glibc glibc-devel libtool make openssl-devel-0.9.8e pkgconfig lib-devel mhash.x86_64 pcre-8.30 nasm gettext-devel gettext-devel perl-Digest-SHA1.x86_64

2、安装git工具:
cd /server/software/lmt/
wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
tar zxf git-latest.tar.gz
cd git-2013-10-30
autoconf
./configure
make && make install

-----------------------------------------------
解决运行git --version报错git: error while loading shared libraries: libiconv.so.2
echo /usr/local/lib >> /etc/ld.so.conf
ldconfig
-----------------------------------------------
git --version
输出一下信息:
git version 1.8.1.GIT
cd ..

3、安装ffmpeg及其依赖包:
++++++++Yasm+++++++++++
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar zxf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure
make && make install
cd ..

++++++++x264+++++++++++
git clone git://git.videolan.org/x264(本次获取后打包为x264.tar.gz)
cd x264
./configure --enable-shared
make && make install
cd ..

++++++++LAME+++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar zxf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --enable-nasm
make && make install
cd ..

++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
tar zxf libogg-1.3.0.tar.gz
cd libogg-1.3.0
./configure
make && make install
cd ..

++++++++libvorbis+++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
tar zxf libvorbis-1.3.3.tar.gz
cd libvorbis-1.3.3
./configure
make && make install
cd ..

++++++++libvpx+++++++++++
git clone http://git.chromium.org/webm/libvpx.git (本次获取后打包为libvpx-v1.2.0.tar.gz) 其它获取方式#wget http://webm.googlecode.com/files/libvpx-v1.1.0.tar.bz2
cd libvpx
./configure --enable-shared
make && make install
cd ..


++++++++FAAD2+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
tar zxf faad2-2.7.tar.gz
cd faad2-2.7
./configure
make && make install
cd ..

++++++++FAAC+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
tar zxf faac-1.28.tar.gz
cd faac-1.28
./configure
make && make install
cd ..

++++++++Xvid+++++++++++
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
tar zxf xvidcore-1.3.2.tar.gz
cd xvidcore/build/generic
./configure
make && make install
cd ..
cd ..
cd ..

++++++++++++libaacplus+++++++++++++
#wget http://217.20.164.161/~tipok/aacplus/libaacplus-2.0.2.tar.gz(本次安装后make clean重新打包 ffmpeg-2.1.tar.gz)
tar zxf libaacplus-2.0.2.tar.gz
cd libaacplus-2.0.2
./autogen.sh
./configure
make && make install

#安装后检查是否有库文件:
ls /usr/local/lib
libaacplus.so
libaacplus.so.2

cd ..

++++++++Ffmpeg+++++++++++
git clone git://source.ffmpeg.org/ffmpeg(本次获取后打包为ffmpeg-2.1.tar.gz) 其它获取方式wget http://ffmpeg.org/releases/ffmpeg-2.1.tar.gz
cd ffmpeg
./configure --prefix=/opt/ffmpeg/ --enable-version3 --enable-libvpx --enable-libfaac --enable-libmp3lame --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads --enable-libaacplus
make && make install
cd ..

#以上所有安装后修改/etc/ld.so.conf库配置文件如下:
vim /etc/ld.so.conf
#添加以下内容:
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib

添加以上内容后运行命令:
ldconfig

注意!如果以上相关包有新加或是重新安装,需要重新执行:
ldconfig #使其新加的库文件加载。

ffmpeg -i


安装
Nginx,php,mysql等模块

配置防火墙
#----HTTP---
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 81 -j ACCEPT
#----Rtmp--
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 1935 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.20.0/255.255.255.0 -j ACCEPT

** 创建链接文件
ln -s /targetPath newLinkPath

** 防火墙设置
vi /etc/sysconfig/iptables

 

二、安装Nginx

# 到http://www.wdlinux.cn/lanmp/下载lanmp开源一键安装包(已安装略过).按说明进行安装
cd /server/software
mkdir lanmp
cd lanmp
wget http://dl.wdlinux.cn:5180/lanmp_laster.tar.gz
tar zxvf lanmp_laster.tar.gz
sh install.sh
# 根据相应选项进行相应安装

# wdlinux默认安装在/www下,公司目录默认在/server下,保证一致性,添加链接文件
cd /server/
ln -s /www/wdlinux/nginx-1.2.9 nginx
ln -s /www/wdlinux/mysql-5.1.69 mysql
ln -s /www/wdlinux/nginx_php php
ln -s /www/wdlinux/pureftpd-1.0.36 pureftpd

# 用wdlinux自带的站点管理工具新建两个站点,域名分别为enc.testvideo.com和live.testvideo.com
# 参见www.wdlinux.cn相关页面说明

# 192.168.20.17为新建的流媒体服务器的IP,浏览器所在计算机hosts文件及流媒体服务器都添加ip-域名映射
vi /etc/hosts

192.168.20.17 enc.testvideo.com
192.168.20.17 live.testvideo.com


# 将程序添加到其它目录可运行
cd
vi .bash_profile
#将/opt/ffmpeg/bin:/server/nginx/sbin:/server/mysql/bin:/server/php/sbin加到PATH后面保存
PATH= PATH: HOME/bin:/opt/ffmpeg/bin:/server/nginx/sbin:/server/mysql/bin:/server/php/sbin
source .bash_profile


# 验证站点是否能正常访问

 

三、安装Nginx相关模块

mkdir /server/nginx_mod

#++++++++安装nginx h264模块++++++++
cd /server/nginx_mod
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
mv -r nginx_mod_h264_streaming-2.2.7 nginx_mod_h264_streaming

#++++++++安装nginx rtmp模块++++++++
cd /server/nginx_mod
git clone git://github.com/arut/nginx-rtmp-module.git
mv -r nginx-rtmp-module nginx_mod_rtmp

#++++++++安装nginx pcre模块++++++++
cd /server/nginx_mod
wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.30/pcre-8.30.tar.gz
tar -zxvf pcre-8.30.tar.gz
mv -r pcre-8.30 nginx_mod_pcre


#++++++++在已安装的nginx基础上添加h264,rtmp模块++++++++
cd /server/nginx/sbin
./nginx -V #查看已安装nginx的配置参数

# 输出结果:
# nginx version: nginx/1.2.9
# built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54)
# TLS SNI support disabled
# configure arguments: --user=www --group=www --prefix=/www/wdlinux/nginx-1.2.9 --with-http_stub_status_module --with-http_ssl_module

# 进到nginx源码目录 wdlinux的nginx源码目录(或者自定义的原nginx源码目录)
cd /server/software/lanmp/lanmp/nginx-1.2.9


# 配置添加新模块
./configure --user=www --group=www --prefix=/www/wdlinux/nginx-1.2.9 \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-cc-opt=-I/opt/ffmpeg/include \
--with-ld-opt='-L/opt/ffmpeg/lib -Wl,-rpath=/opt/ffmpeg/lib' \
--add-module=/server/nginx_mod/nginx_mod_h264_streaming/ \
--add-module=/server/nginx_mod/nginx_mod_rtmp/ \
--with-pcre=/server/nginx_mod/nginx_mod_pcre


# 重新编译

#以上下下来的nginx_mod_h264_streaming-2.2.7.tar.gz编译时会出错:ngx_http_streaming_module.c:158: 错误:‘ngx_http_request_t’ 没有名为 ‘zero_in_uri’ 的成员
#需要注释掉
vi /server/nginx_mod/nginx_mod_h264_streaming//src/ngx_http_streaming_module.c
#需要注释掉 第158行:
/*
if (r->zero_in_uri)
{
return NGX_DECLINED;
}
*/
make #不要make install,否则覆盖安装

# 已运行nginx,需先停止
killall -9 nginx

# 用新程序覆盖旧程序
cp /www/wdlinux/nginx-1.2.9/sbin/nginx /www/wdlinux/nginx-1.2.9/sbin/nginx.bak
cp /server/software/lanmp/lanmp/nginx-1.2.9/objs/nginx /www/wdlinux/nginx-1.2.9/sbin/nginx

# 测试配置,看nginx是否运行正常
/www/wdlinux/nginx-1.2.9/sbin/nginx -t -c /www/wdlinux/nginx-1.2.9/conf/nginx.conf


# 配置 nginx.conf
cd /www/wdlinux/nginx-1.2.9/
vi nginx.conf

# 修改每个进程打开的最大文件数(linux系统设置需支持,修改/etc/security/limits.conf文件)
events {
use epoll;
worker_connections 65535;
}

# 修改http中参数设置
http {
include mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
limit_conn_zone $binary_remote_addr zone=one:256k;
limit_conn_log_level notice;

keepalive_timeout 6000;

# .....
}

# 添加rtmp模块参数
rtmp {
server {
listen 1935;
application vod {
#Directory of flv files
play /server/serverweb/live/public_html/live;
}
}
}

# 保存nginx.conf文件

# 新建及配置虚拟站点文件
cd /www/wdlinux/nginx-1.2.9/vhost
#cp default.conf live.testvideo.com.conf (之前有建站点live.testvideo.com,则不需复制配置)
vi live.testvideo.com.conf

# 配置如下

server {
listen 80;
server_name live.testvideo.com;
root /server/serverweb/live/public_html;
index index.php;
error_page 400 /errpage/400.html;
error_page 403 /errpage/403.html;
error_page 404 /errpage/404.html;
error_page 405 /errpage/405.html;
location ~ \.php fastcgipass127.0.0.1:9000;fastcgiindexindex.php;includefcgi.conf;location \.mp4 {
mp4;
}

access_log /server/serverlog/live.testvideo.com_access.log wwwlogs;
error_log /server/serverlog/live.testvideo.com_error.log;
}


# 测试配置,看nginx是否运行正常
/www/wdlinux/nginx-1.2.9/sbin/nginx -t -c /www/wdlinux/nginx-1.2.9/conf/nginx.conf


#++++++++ 播放测试站点 ++++++++
# 上传一个.mp4文件放到live目录
cd /server/serverweb/live/public_html/live


# 下载开源播放器jwplayer
cd /server/software/
mkdir jwplayer
cd jwplayer
wget https://account.jwplayer.com/static/download/jwplayer-6.7.zip # 如果不能下载,则需要到官网http://www.jwplayer.com注册然后下载
unzip jwplayer-6.7.zip

# 复制播放器代码到站点文件夹,根据官方文档新建html文件播放页(省略)。
cp -r jwplayer /server/serverweb/live/public_html/

cd /server/serverweb/live/public_html
vi player.html

#复制以下内容到player.html中保存
cd /server/serverweb/live/public_html
vi player.html

#复制以下内容到player.html中保存
<html>
<head>
<title>播放测试</title>
<script type="text/javascript" src="/jwplayer/jwplayer.js"></script>
<script type="text/javascript">jwplayer.key="MGAzpXRYxGnaVXhH8jSdKXzDe7ucAAvZGWggIA==";</script>
</head>
<body>
<div id="testPlayer">Loading the player...</div>
<script type="text/javascript">
jwplayer("testPlayer").setup({
file: "/live/test.mp4",
image: "/live/test.png",
title: 'test',
width: '100%',
aspectratio: '16:9',
fallback: 'false',
primary: 'flash'
});
</script>
</body>
</html>

五、http及rtmp流媒体测试

# 浏览器中测试能否播放http流媒体
http://live.testvideo.com/player.html

# 测试Rtmp点播vod
rtmp://live.testvideo.com:1935/vod/test.mp4

# 测试Rtmp直播live
rtmp://live.testvideo.com:1935/live/h_room_60025

# 测试Rtmp转码

#* 从本地文件转
ffmpeg -i /server/serverweb/live/public_html/live/test.mp4 -c:a libfaac -ar 44100 -ab 48k -c:v libx264 -f flv rtmp://live.testvideo.cn:1935/live/h1_room_60025

#* 从视频流转
ffmpeg -i rtmp://live.testvideo.cn:1935/live/h_room_60025 -c:a libfaac -ar 44100 -ab 48k -c:v libx264 -f flv rtmp://live.testvideo.cn:1935/live/h2_room_60025


# 查找转码进程
ps -ef|grep 'room_60025'*

# 杀掉转码进程
ps -ef|grep 'room_60025'* | grep -v grep|awk '{print $2}' | xargs -i kill -9 {}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值