利用nginx搭建http和rtmp協議的流媒體服務器


1、準備工做javascript

一、安裝依賴包:
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_64php

二、安裝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 installhtml

-----------------------------------------------
解決運行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 ..java

三、安裝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 ..mysql

++++++++x264+++++++++++
git clone git://git.videolan.org/x264(本次獲取後打包爲x264.tar.gz)
cd x264
./configure --enable-shared
make && make install
cd ..linux

++++++++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 ..nginx

++++++++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 ..git

++++++++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 ..github

++++++++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 ..web


++++++++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

 

2、安裝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


# 驗證站點是否能正常訪問

 

3、安裝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$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.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>

5、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 {}

相關文章
相關標籤/搜索