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

2016-10-26 zhulinan 服务器

操作系统:阿里云CentOS 6.5 64位


-------- 系统更新 --------
# yum update


-------- 安装依赖包 --------
# yum install openssl-devel
# yum install mhash.x86_64
# yum install nasm
# yum install perl-Digest-SHA1.x86_64
# yum install curl-devel//使git支持http协议


-------- 安装git工具 --------
# mkdir /soft
# cd /soft
# wget http://codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.xz
# xz -d git-latest.tar.xz
# tar xvf git-latest.tar
# cd git-2016-10-25
# autoconf
# ./configure
# make && make install
# git --version//git version 2.10.0.GIT
# cd /soft


-------- 安装ffmpeg及其依赖包 --------
1). 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 /soft

2). x264
# git clone git://git.videolan.org/x264
# cd x264
# ./configure --enable-shared
# make && make install
# cd /soft

3). lame
# wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
# tar xzvf lame-3.99.5.tar.gz
# cd lame-3.99.5
# ./configure --enable-nasm
# make && make install
# cd /soft

4). libogg
# wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
# tar xzvf libogg-1.3.0.tar.gz
# cd libogg-1.3.0
# ./configure
# make && make install
# cd /soft

5). libvorbis
# vi /etc/ld.so.conf

尾部加一行:/usr/local/lib,然后保存、退出。

# ldconfig -v
# wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
# tar xzvf libvorbis-1.3.3.tar.gz
# cd libvorbis-1.3.3
# ./configure
# make && make install
# cd /soft

6). libvpx
# git clone https://github.com/webmproject/libvpx
# cd libvpx
# ./configure  --enable-shared
# make && make install
# cd /soft

7). faad2
# wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
# tar zxvf faad2-2.7.tar.gz
# cd faad2-2.7
# ./configure
# make && make install
# cd /soft

8). faac
# wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
# tar zxvf faac-1.28.tar.gz
# cd faac-1.28
# ./configure
# make && make install

报错。

# vi common/mp4v2/mpeg4ip.h

123行开始,修改前:
#ifdef __cplusplus
 extern "C" {
 #endif
char *strcasestr(const char *haystack, const char *needle);
 #ifdef __cplusplus
 }
#endif

修改后:
#ifdef __cplusplus
 extern "C++" {
 #endif
const char *strcasestr(const char *haystack, const char *needle);
 #ifdef __cplusplus
 }
#endif

 # make && make install
# cd /soft

9). xvid
# wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
# tar zxvf xvidcore-1.3.2.tar.gz
# cd xvidcore/build/generic
# ./configure
# make && make install
# cd /soft

10). libaacplus
# wget http://ffmpeg.gusari.org/uploads/libaacplus-2.0.2.tar.gz
# tar zxvf 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 /soft

11). ffmpeg
# git clone git://source.ffmpeg.org/ffmpeg
# 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

出错:
Unknown option "--enable-libfaac".

# ./configure --prefix=/opt/ffmpeg/ --enable-version3 --enable-libvpx --enable-libmp3lame --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads --enable-libaacplus

出错:
Unknown option "--enable-libaacplus".

# ./configure --prefix=/opt/ffmpeg/ --enable-version3 --enable-libvpx --enable-libmp3lame --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads

提示:
WARNING: using libx264 without pkg-config

未理会。

# vi /etc/ld.so.conf

添加以下内容:
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib

# ldconfig
# make && make install//时间比较长
# cd /soft


-------- 安装Nginx --------
# mkdir lanmp
# cd lanmp
# wget http://dl.wdlinux.cn:5180/lanmp_laster.tar.gz
# tar zxvf lanmp_laster.tar.gz
# sh install.sh//安装时间较长,中间2次选择,分别选2、1,此安装包含prce
# rpm -qa pcre//显示pcre版本:pcre-7.8-7.el6.x86_64


-------- 安装Nginx相关模块 --------
# mkdir /soft/nginx_mod
# cd /soft/nginx_mod

1). 下载nginx h264模块
# 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 -f nginx_mod_h264_streaming-2.2.7 nginx_mod_h264_streaming

2). 下载nginx rtmp模块
# git clone git://github.com/arut/nginx-rtmp-module.git
# mv -f nginx-rtmp-module nginx_mod_rtmp

3). 下载nginx pcre模块
# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.30/pcre-8.30.tar.gz
# tar -zxvf pcre-8.30.tar.gz
# mv -f pcre-8.30 nginx_mod_pcre

4) . 配置添加新模块
# cd /soft/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=/soft/nginx_mod/nginx_mod_h264_streaming/ \
--add-module=/soft/nginx_mod/nginx_mod_rtmp/ \
--with-pcre=/soft/nginx_mod/nginx_mod_pcre

# vi /soft/nginx_mod/nginx_mod_h264_streaming/src/ngx_http_streaming_module.c

需要注释掉 第158行:

/*if (r->zero_in_uri)
{
  return NGX_DECLINED;
}*/

# make

到此网站已可以访问。已运行nginx,需先停。

# killall -9 nginx

用新程序覆盖旧程序:
# cp /www/wdlinux/nginx-1.2.9/sbin/nginx /www/wdlinux/nginx-1.2.9/sbin/nginx.bak
# cp /soft/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: the configuration file /www/wdlinux/nginx-1.2.9/conf/nginx.conf syntax is ok
nginx: configuration file /www/wdlinux/nginx-1.2.9/conf/nginx.conf test is successful

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

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

b. 修改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;//原60未改
    ...
}

c. 添加rtmp模块参数
rtmp {
    server {
        listen 1935;
        application vod {
            #Directory of flv files
            play /www/web/vod;
        }
    }
}

保存nginx.conf文件。

6). 建点播文件夹
# cd /www/web
# mkdir vod


-------- 配置防火墙 --------

若iptables未配置过,则可以通过初始化iptables,先生成配置文件。前面安装Nginx时,已配置过iptables,下面初始化iptables过程可以略过。

初始化iptables
# iptables -F
# service iptables save
# service iptables restart

修改配置文件:
# vi /etc/sysconfig/iptables

将:
-A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 20000:20500 -m state --state NEW -j ACCEPT

改为:
-A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 1935 -j ACCEPT

重新启动iptables服务:
# service iptables restart


-------- 其它 --------
1). 通过wdcp服务器/虚拟主机管理系统(http://yourip:8080/ )修改登录密码和mysql密码;

2). 通过wdcp服务器/虚拟主机管理系统上传视频播放相关文件。
系统管理->文件管理->default

3). 通过wdcp服务器/虚拟主机管理系统上传视频文件。
系统管理->文件管理->vod

4). 安装完后运行yum update提示错误
# yum update

显示:
Error: Cannot retrieve repository metadata (repomd.xml) for repository: rpmforge. Please verify its path and try again

如何解决参考:http://blog.csdn.net/david_xtd/article/details/8645306

网站备案号:京ICP备11043289号-1 北京市公安局网络备案 海1101084571
版权所有 北京育灵童科技发展有限公司 Copyright © 2002-2024 www.elight.cn, All Rights Reserved