Linux centOS下搭建RTMP服務器的具體步驟

如下的所需的安裝包,可直接在linux系統終端下載,也可從其餘地方下載以後拷到對應目錄下解壓使用,遇到鏈接不到國外網站時可改變壓縮包地址php

一、安裝依賴包: html

 #yum install glibc.i686
 #yum –y update
 #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   gcc-c++linux

二、安裝git工具://新安裝的軟件都放在新建的softsource文件夾下
  
 #mkdir softsource
 #cd softsource
 #wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
 #tar xzvf git-latest.tar.gz 
 #cd git-2013-08-28
 #autoconf
 #./configure
 #make && make install
 # git --version
 git version 1.8.1.GIT
 #cd ..        目前直接回去rtmp目錄, 沒有在上一層安裝FFmpeg
 
三、安裝ffmpeg及其依賴包:
 ++++++++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 ..
 ++++++++x264+++++++++++
 #git clone git://git.videolan.org/x264
 #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)
 #wget http://kakola.googlecode.com/files/lame-3.98.4.tar.gz
 (安裝lame-3.98.4.tar.gz, 以避免在第三步的時候出現ERROR:libmp3lame >= 3.98.3 not found錯誤, 須要提早安裝lame.)
 #tar xzvf lame-3.98.4.tar.gz
 #cd lame-3.98.4
 #./configure --enable-nasm
 #make
 #make install
 #cd ..
 ++++++++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 ..
 ++++++++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 ..
 ++++++++libvpx+++++++++++   nginx

 #wget http://webm.googlecode.com/files/libvpx-v1.1.0.tar.bz2 
 #tar -vxjf libvpx-v1.1.0.tar.bz2
 #cd libvpx-v1.1.0
 #./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 zxvf faad2-2.7.tar.gz
 #cd faad2-2.7
 #./configure
 #make
 #make install
 #cd ..
 ++++++++FAAC+++++++++++   #wget http://sourceforge.net/projects/faac/files/faac-src/faac-1.28/faac-1.28.tar.gz/download (http://172.16.1.100/faac-1.28.tar.gz)
 #tar zxvf faac-1.28.tar.gz
 #cd faac-1.28
 #cd common/mp4v2/
 #vi mpeg4ip.h
 126G—定位到126行後刪除這一行,保持退出
 回到faac-1.28目錄下:cd ..
 Cd ..
 #./configure
 #make
 #make install
 #cd ..
 ++++++++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 ..
 ++++++++ffmpeg+++++++++++   c++

 #git clone git://source.ffmpeg.org/ffmpeg
 #cd ffmpeg
 #./configure --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 ..
 ++++++++ zlib+++++++++++ 
 Wget http://www.zlib.net/zlib-1.2.8.tar.gz
 #tar xzvf zlib-1.2.8.tar.gz
 #cd zlib-1.2.8
 #./configure -prefix-/usr/local/zlib
 #make && make installgit

************************************************web

 修改/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
 
 
 OpenSSL
 # wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
 # tar -zxvf openssl-1.0.1c.tar.gz
 # cd openssl-1.0.1c.tar.gz
 # ./config
 # make
 # make install
 
 會出現語法錯誤 POD document had syntax errors at /usr/bin/pod2man line 69. Make
    解決辦法:rm /usr/bin/pod2man  刪除此文件(多是由於不兼容的問題)ubuntu

 
4. 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 打開進行編輯:vim

#!/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 \

echo "configure end!"

【保存並退出】
 # chmod +x nginx_configure.sh  (此步驟執行後可能會出錯 缺乏什麼包就添加什麼包)
 # ./nginx_configure.sh
 # make
 # make installcentos

備註:
 在執行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-devel
 (ubuntu下的命令 centos下用yum -y install openssl openssl-devel)
 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
  
 解決方法:在nginx_mod_h264_streaming-2.2.7中修改
 那麼將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
  
 解決方法:在nginx的安裝目錄
 # vim objs/Makefile (修改objs/Makefile文件, 去掉其中的"-Werror"), 而後就可以正常編譯了.
 而後在make就能過了。
 
 
 安裝過程當中又下載了nginx_mod_h264_streaming-2.2.7   pcre-8.12  兩個文件放入softsource文件夾中
 
 
******************************************
五、配置Nginx相關模塊
 一、安裝好了以後終端進入 nginx安裝的目錄 : /usr/local/nginx/html/
 而後新建目錄:nginx-rtmp-module   (新建的命令爲 mkdir 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/
 拷貝的時候 會報錯由於 test文件是個文件夾因此 須要遞歸拷貝 在cp後邊加上-r就能夠了
 2.
 拷貝以後就須要修改 /usr/local/nginx/nginx.conf 
 nginx.conf  配置文件已經配好!(也可直接把rtmp那一塊的配置直接拷貝到原來的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;
        }
    }

}
    


 若是出現端口 wait_CLOSE
 就在 /etc/sysctl.conf中添加
 net.ipv4.tcp_tw_reuse=1
 net.ipv4.tcp_tw_recycle=1
 而後讓配置生效
 /sbin/sysctl -p
 (以上的命令都是在根目錄執行的)
 
六、根據nginx的配置文件的中的配置設置端口號  (此步驟 暫不清楚   不過能夠直接將防火牆所有關閉,service firewalld stop)
 #vi /etc/sysconfig/iptables   (此文件能夠本身建立而後在網上找相關的代碼拷進去)
 開放8081端口  (進入nginx的nginx.conf中設置端口爲8081)
 #/sbin/service iptables restart 
 重啓防火牆,設置生效
 修改防火牆配置文件,所在目錄/etc/sysconfig/iptables,在裏面增長鬚要開通的端口號,以下:
 1
 2 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT 
 -A INPUT -m state --state NEW -m tcp -p tcp --dport 8088 -j ACCEPT
 #增長的代碼必須放在如下代碼之上,不然不會起做用.
 1
 2 -A INPUT -j REJECT --reject-with icmp-host-prohibited 
 -A FORWARD -j REJECT --reject-with icmp-host-prohibite
 
七、 啓動nginx後測試

 (若是沒有nginx文件夾中沒有sbin文件那麼能夠直接進入nginx文件夾
 使用 ./nginx 啓動nginx 
      ./nginx -t 測試是否配置成功
   ./nginx -s reload 重啓nginx)


 啓動命令:
/usr/local/nginx/nginx  -c /usr/local/nginx/nginx.conf  (能保證rtmp正常運行) 


 啓動命令其實就用
 進入#usr/local/nginx/sbin/下   ./nginx 就能夠
 
 啓動nginx的幫助文檔:
 #/usr/local/nginx/sbin/nginx -h
 #/usr/local/nginx/sbin/nginx –s reload
 查看nginx的配置是否正確:
 #usr/local/nginx/sbin/nginx –t/
 若是配置是正確的會顯示以下內容:
 Nginx.conf syntax is ok.
 Nginx.conf test is successful.
 
 配置成功後使用ffmpeg進行推流
 
 (轉發鳳凰衛視的直播地址)ffmpeg -re -i rtmp:/live.hkstv.hk.lxdns.com/live/hks -c:a aac -ac 1 -strict -2 -b:a 64k -c:v libx264 -f flv rtmp://192.168.1.75/live/stream

  (標準地址格式)ffmpeg -re -i /root/softsource/960x720_30fps.mp4 -f flv rtmp://192.168.1.59/live/stream 

相關文章
相關標籤/搜索