最近在 ubuntu12.04+wdlinux(centos)上搭建了一个rtmp服务器,感受还挺麻烦的,因此记录下。php
大部分都是参考网络上的资料。html
前提:node
在linux下某个目录中新建一个nginx目录。linux
而后进入该目录去下载搭建环境所须要的一些资源包。nginx
此处在 /root/ 目录下新建一个nginx目录即:git
/root/softsource/github
注意:依赖包和工具包须要下载,请在良好的网络环境下安装,不然在网速很差的状况下容易下漏掉,形成后面安装失败web
====================================ubuntu
一、安装依赖包:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64
二、安装相关工具包
1). git
# mkdir soft-source
# cd soft-source
# wget http://codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.xz
# xz -d git-latest.tar.xz
# tar xzvf git-latest.tar
# cd git-2014-06-27
# autoconf
# ./configure
# make && make install
# git --version
git version 2.0.0.GIT
# cd ..
2). zlib
# wget http://zlib.net/zlib-1.2.8.tar.gz
# tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
# ./configure
# make
# make install
# cd ..
3). pcre
# wget http://exim.mirror.fr/pcre/pcre-8.32.tar.gz
# tar zxvf pcre-8.12.tar.gz
# cd pcre-8.12
# ./configure
# make && make install
# cd ..
4). yadmi
yadmi的做用是为flv文件添加关键帧,才能实现拖动播放
# wget http://sourceforge.net/projects/yamdi/files/yamdi/1.4/yamdi-1.4.tar.gz/download
# tar xzvf download
# cd yamdi-1.4
# make && make install
# cd ..
测试方法:
# yamdi -i input.flv -o out.flv
给input.flv文件 添加关键帧,输出为out.flv文件
5). OpenSSL
# wget http://www.openssl.org/source/openssl-1.0.1i.tar.gz
# tar -zxvf openssl-1.0.1c.tar.gz
# ./config
# make
# make install
三、安装ffmpeg及其依赖包:
1). Yasm
# wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
# tar xzvf yasm-1.2.0.tar.gz
# cd yasm-1.2.0
# ./configure
# make
# make install
# cd ..
2). x264
# git clone git://git.videolan.org/x264
# cd x264
# ./configure --enable-shared
# make
# make install
# cd ..
若是这个下载不到,请搜索下载"last_x264.tar"vim
注意:可能出现安装错误 修改 x264目录下的version.sh
找到 version="" 改成 version="2245"我这里是这个版本号 具体根据你下载解压后的文件最后那几个数字就是版本号
有必要介绍下linux几种压缩文件的解压方法:
.tar.gz tar -xzvf 该类型文件(下面就以file代替) -xzvf 能够不加前面的-
.tar.bz2 tar -xjvf file 也能够不加 -
.tar tar -xf file
.tar.xz tar -xjvf
.zip unzip
以上是本人经常使用的几个,了解更多的请找度娘
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 ..
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 ..
5). libvorbis
# 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 ..
注意这里 :应该是先安装libogg后在安装libvorbis 若是中间发生带有 "must ogg installled"的语句,此时不要慌张
由于你安装到这里来了,说明你前面的libogg是安装成功了,可是为何说找不到ogg呢,我这里只说解决知道,缘由我也不知道
解决:在 /etc/ld.so.conf/目录下建立名为 local-libraries.conf的文件
vi /etc/ld.so.conf/local-libraries.conf
文件内容:/usr/local/lib
wq!---保存退出
在执行 ldconfig -v
这里建议安装下vim
6). libvpx
# git clone http://git.chromium.org/webm/libvpx.git
# cd libvpx
# ./configure --enable-shared
# make
# make install
# cd ..
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 ..
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
# cd ..
注:编译时可能遇到一下错误:
mpeg4ip.h:126: error: new declaration ‘char* strcasestr(const char*, const char*)’
解决方法:
从123行开始修改此文件mpeg4ip.h,到129行结束。
修改前:
#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
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 ..
10). 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
# make && make install
# cd ..
修改/etc/ld.so.conf以下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
# ldconfig
特别强调:在centos下可能出现 “waring using libx264 without pkgconfig”警告,出现后继续安装 安装结束也是能够的
在ubuntu下如果安装时提示缺乏什么包的话 使用 sudo apt-get 工具包名 安装对应缺乏的包就能解决问题
2、安装Nginx相关模块
1. 模块安装
# 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
# git clone git://github.com/arut/nginx-rtmp-module.git
2. 建立用户和组
# groupadd www
# useradd -g www www
3. nginx安装
# wget http://nginx.org/download/nginx-1.6.0.tar.gz
# tar zxvf nginx-1.6.0.tar.gz
# cd nginx-1.6.0
4.编译安装nginx
在nginx-1.6.0目录下新建文件:nginx_configure.sh
而后使用gedit 打开进行编辑:
#!/bin/sh echo "configure start ..." ./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --error-log-path=/usr/local/nginx/logs/error.log \ --add-module=../nginx_mod_h264_streaming-2.2.7 \ --with-pcre=../pcre-8.12 \ --with-zlib=../zlib-1.2.8 \ --with-http_dav_module \ --with-http_flv_module \ --with-http_stub_status_module \ --without-http_scgi_module \ --without-http_uwsgi_module \ --without-http_gzip_module \ --without-http_ssi_module \ --without-http_proxy_module \ --without-http_memcached_module \ --without-http_empty_gif_module \ --without-mail_pop3_module \ --without-mail_imap_module \ --without-mail_smtp_module \ --with-http_ssl_module \ --with-openssl=../openssl-1.0.1c \ --add-module=../nginx-rtmp-module \ --with-cc-opt=-I/opt/ffmpeg/include \ --with-ld-opt=`-L/opt/ffmpeg/lib -Wl, -rpath=/opt/ffmpeg/lib` echo "configure end!"
【保存并退出】
# chmod +x nginx_configure.sh
# ./nginx_configure.sh
# make
# make install
备注:
在执行make的时候可能出现错误1:
adding module in ../nginx_mod_h264_streaming-2.2.7
+ ngx_http_h264_streaming_module was configured
adding module in ../nginx-rtmp-module
+ ngx_rtmp_module was configured
checking for OpenSSL library ... not found
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
configure end!
root@ubuntu:/home/song/nginx-t/nginx-1.6.0# make
make: *** No rule to make target `build', needed by `default'. Stop.
解决方法:
a)能够安装ssl
apt-get install openssl
apt-get install libssl-dev
b)也能够添加
--with-openssl=<path>
在执行make的时候可能出现错误2:
/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c: In function ‘ngx_streaming_handler’:
/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158: error: ‘ngx_http_request_t’ has no member named ‘zero_in_uri’
make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1
make[1]: Leaving directory `/root/nginx-0.8.54'
make: *** [build] Error 2
解决方法:
那么将src/ngx_http_streaming_module.c文件中如下代码删除或者是注释掉就能够了:
/* TODO: Win32 */
if (r->zero_in_uri)
{
return NGX_DECLINED;
}
在执行make的时候可能出现错误3:
../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c: 在函数‘esds_read’中:
../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:377:16: 错误: 变量‘stream_priority’被设定但未被使用 [-Werror=unused-but-set-variable]
../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:376:12: 错误: 变量‘stream_id’被设定但未被使用 [-Werror=unused-but-set-variable]
../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c: 在函数‘stsd_parse_vide’中:
../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:529:22: 错误: 变量‘level_indication’被设定但未被使用 [-Werror=unused-but-set-variable]
../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:528:22: 错误: 变量‘profile_compatibility’被设定但未被使用 [-Werror=unused-but-set-variable]
../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:527:22: 错误: 变量‘profile_indication’被设定但未被使用 [-Werror=unused-but-set-variable]
../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:526:22: 错误: 变量‘configuration_version’被设定但未被使用 [-Werror=unused-but-set-variable]
cc1: all warnings being treated as errors
make[1]: *** [objs/addon/src/mp4_reader.o] 错误 1
解决方法:
# vim objs/Makefile (修改objs/Makefile文件, 去掉其中的"-Werror"), 而后就可以正常编译了.
而后在make就能过了。
3、配置Nginx相关模块
1.
安装好了以后终端进入 nginx安装的目录 : /usr/local/nginx/html/
而后新建目录:nginx-rtmp-module
而后拷贝 以前用于存放下载 nginx 所需资源包的目录 :/root/nginx/nginx-rtmp-module/
拷贝 目录 test 到/usr/local/nginx/html/ 目录下:
# cp /root/nginx-rtmp-module/test /usr/local/nginx/html/nginx-rtmp-module/
# cp /root/nginx-rtmp-module/stat.xsl /usr/local/nginx/html/nginx-rtmp-module/
2.
拷贝以后就须要修改 /usr/local/nginx/nginx.conf 文件:
下面是配置好的nginx.conf 文件:
#user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; pid logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } #切换自动推送(多 worker 直播流)模式。默认为 off rtmp_auto_push on; #当 worker 被干掉时设置自动推送链接超时时间。默认为 100 毫秒 rtmp_auto_push_reconnect 1s; #设置用于流推送的 UNIX 域套接字目录。默认为 /tmp #rtmp_socket_dir /var/sock; rtmp { server { listen 1935; #点播配置 application vod { play /opt/media/nginxrtmp/flv; } #直播流配置 application live { live on; #为 rtmp 引擎设置最大链接数。默认为 off max_connections 1024; # default recorder record all; record_path /var/rec; recorder audio { record audio; record_suffix -%d-%b-%y-%T.flv; } recorder chunked { record all; record_interval 15s; record_path /var/rec/chunked; } #on_publish http://localhost:8080/publish; #on_play http://localhost:8080/play; #on_record_done http://localhost:8080/record_done; #rtmp日志设置 #access_log logs/rtmp_access.log new; #access_log logs/rtmp_access.log; #access_log off; } #HLS协议支持 #application hls { #live on; #hls on; #hls_path /tmp/app; #hls_fragment 5s; #} application hls{ live on; hls on; hls_path /usr/local/nginx/html/app; hls_fragment 1s; } } } 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 main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} #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; #} location /rtmp-publisher { root /root/nginx/nginx-rtmp-module/test; } #location /hls { #server hls fragments #types{ #application/vnd.apple.mpegurl m3u8; #video/mp2t ts; #} #alias /usr/local/nginx/html; #expires -1; #} location /hls { #server hls fragments types{ application/vnd.apple.mpegurl m3u8; video/mp2t ts; } #alias /usr/local/nginx/html/app; alias /usr/local/nginx/html; expires -2; } location / { #root /root/nginx/nginx-rtmp-module/test/rtmp-publisher; types{ application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /usr/local/nginx/html/rtmp-publisher; expires -1; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} #支持flv server { listen 8081; server_name localhost; #root /opt/pub/media/nginx; #http协议时候,flv视频位置 #root /root/nginx/nginx-rtmp-module/test/www; root /usr/local/nginx/html/www; location ~ .*.(flv|swf|mp4|wma|wmv)$ { valid_referers none blocked *.xxxx.com http://localhost; if ($invalid_referer) { return 403; } } location ~ \.flv$ { flv; #limit_conn one 20;#限制客户端并发链接数 limit_rate 200k;#限制每客户端最大带宽 } location ~ \.mp4$ { flv; #limit_conn one 20; limit_rate 200k; } #access_log logs/nginxflv_access.log main; } server { listen 8082; server_name localhost; index index.html; location / { #root /opt/pub/media/nginx-rtmp; root /root/nginx/nginx-rtmp-module/test/rtmp-publisher; } #access_log logs/nginxrtmpflv_access.log main; } server { listen 8080; server_name localhost; index player.html; location /stat { rtmp_stat all; rtmp_stat_stylesheet /root/nginx/nginx-rtmp-module/stat.xsl; } location /stat.xsl { root /root/nginx/nginx-rtmp-module; } location /rtmp-publisher { root /usr/local/nginx/html/test/rtmp-publisher; } location / { root /root/nginx/nginx-rtmp-module/test/rtmp-publisher; } } }
4、启动Nginx相关模块
1.
配置好以后就能够开启nginx服务器了。
能够在终端执行:
#service nginx start
注:可能会出现一下错误:
nginx: unrecognized service
解决方法:
下载nginx的启动脚本:
# wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
将脚本添加到init.d目录和生成可执行:
# sudo mv init-deb.sh /etc/init.d/nginx
# sudo chmod +x /etc/init.d/nginx
加的nginx到系统启动:
# sudo /usr/sbin/update-rc.d -f nginx defaults
如今咱们可使用nginx的控制:
sudo service nginx stop
sudo service nginx start
sudo service nginx restart
sudo service nginx reload
2.
在开启以前记得要打开 1935端口。
CentOS 6.4开放端口方法
一、LINUX下经过命令开启容许对外访问的网络端口:
/sbin/iptables -I INPUT -p tcp --dport 1935 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/rc.d/init.d/iptables restart
二、查看端口是否开放
/etc/init.d/iptables status
ubuntu下:sudo iptables -I INPUT -p tcp --dport 1935 -j ACCEPT
3.
可是开启的时候命令行要这样去启动:
# /usr/local/ngnix/nginx –c /usr/local/nginx/nginx.conf
这样nginx 的rtmp协议流媒体服务器才能用。
4.下面使用ffmpeg 往rtmp服务器推送一个视频:
ffmpeg -re -i /home/song/Desktop/apk/test.flv -f flv rtmp://172.16.80.18/live/steam
这样在 vlc或者 flash player 上就能够打开 rtmp地址:
rtmp://192.168.1.11/live/steam
进行观看了。
还要说明的是 若是你不是在服务器上推流 而是在其它的终端推流 好比 一台pc使用obs软件作直播 而后推送到你的流媒体服务器上 而后服务器作分发直播视频 那么你也能够暂时不安装ffmpeg
可是ffmpeg安装后 能够起到 音视频转码 中转(pc推流过来 再将此流经过ffmpeg推流到其它地方)这些也是颇有用的
注:若是使用的是虚拟机安装的话,记得网络适配器 要选择 “桥接模式” 使用虚拟机里的ip 地址和 主机地址在一个网端,这样外部好比 手机 或者局域网内的其余机器才能访问
该虚拟机上搭建的nginx服务器了。
补充说明 wdlinux更新 nginx
一、①【前提须要安装依赖包和工具包,因此仍是把文章开始提到的包都安装上,否则这里会出错的】wget http://www.jicker.cn/down/2015/2/nginx_up.sh sh nginx_up.sh nginx版本号(例如 1.10.0)-----好像这个也要下载对应版本的包才能成功 我是这样作的
或者② 先下载须要的添加的模块
git clone git:
//github
.com
/yaoweibin/ngx_http_substitutions_filter_module
.git
|
再下载对应的nginx版本
wget -c http:
//www
.nginx.org
/download/nginx-1
.10.0.
tar
.gz
|
tar
zxvf nginx-
tar
.gz
|
二、看一下,咱们的nginx的已经有的一些参数
/www/wdlinux/nginx-1
.0.15
/sbin/nginx
-V
|
nginx version: nginx/-1
.0.15
.....
-1
.0.15
--with-http_stub_status_module --with-http_ssl_module
cd
nginx-1.10.0
|
.
/configure
--user=www --group=www --prefix=
/www/wdlinux/nginx-1
.10.0 --with-http_stub_status_module --with-http_ssl_module --add-module=
/root/ngx_http_substitutions_filter_module-【这里就能够添加rtmp模块这些; 和上面同样】
|
make
|
#####此处不须要install
service nginxd stop
|
mv
/www/wdlinux/nginx-1
.10.0
/sbin/nginx
/www/wdlinux/nginx-1
.10.0
/sbin/nginx
.old
|
cp
objs
/nginx
/www/wdlinux/nginx-1
.10.0
/sbin/nginx----【要在目录下看看有没有objs这个目录 没有的话 恭喜你失败了】
|
/www/wdlinux/nginx-1
.10.0
/sbin/nginx
-t
|
service nginxd start
|
####能够看见参数中带有新添加的模块
/www/wdlinux/nginx-1
.10.0
/sbin/nginx
-V