Nginx搭建视频点播和视频直播服务器

Nginx搭建视频点播和视频直播服务器

一·、环境:
Centos 7,(推荐,Ubuntu不是很好用,经常会有一些莫名其妙的报错)
Nginx1.10.1
二、系统环境搭建
首先,我是不建议自己一个个去安装这些软件的,耗时耗力,而且,容易出错,所以,最好使用yum install ***命令安装,出错的概率小。
在这里插入图片描述
资源链接:链接:https://pan.baidu.com/s/1WmJYpQ_b089Oj783FZjX0g
提取码:bk0v
1、首先说下默认安装流程
(1)、yum install gcc gcc-c++
(2)、yum install openssl openssl-devel
(3)、yum insall pcre pcre-devel
(4)、yum install zlib zlib-devel
(5)、重点,下载nginx-rtmp-module-master
命令:git clone https://github.com/arut/nginx-rtmp-module.git
解压:
(6)、编译安装nginx
进入到解压之后的目录下,./configure --prefix=/usr/local/nginx --add-module=/home/admin/ftp/software/nginx-rtmp-module-master
注意:有时候编译的时候可能会报错,比如openssl找不到,而你输入openssll是有显示的,这是为什么,是因为你没有安装openssl-devel。
编译,注意:/home/admin/ftp/software/是你自己定义的rtmp包解压之后的目录。
2、非默认的,下载压缩包,解压后,编译(./configure --prefix=/usr/local/yourname)->make–>make install。出错率比较高,不交易这样做。
三、启动nginx
测试的时候,发现不论是service nginx start还是systemctl start nginx都不起作用,这就需要我们自己将nginx这个添加进服务里面去。在/etc/init.d/下创建一个nginx文件。(创建方法,vi nginx——>:wq退出保存)。

特别注意:#!/bin/sh一定要放在第一行,复制粘贴的时候不要忘记。

#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
 
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid
 
# Source function library.
 
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
 
. /etc/sysconfig/network
 
# Check that networking is up.
 
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
 
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
lockfile=/var/lock/subsys/nginx
 
start() {
 
    [ -x $nginx ] || exit 5
 
    [ -f $NGINX_CONF_FILE ] || exit 6
 
    echo -n $"Starting $prog: "
 
    daemon $nginx -c $NGINX_CONF_FILE
 
    retval=$?
 
    echo
 
    [ $retval -eq 0 ] && touch $lockfile
 
    return $retval
 
}
 
 
stop() {
 
    echo -n $"Stopping $prog: "
 
    killproc $prog -QUIT
 
    retval=$?
 
    echo
 
    [ $retval -eq 0 ] && rm -f $lockfile
 
    return $retval
 
}
 
 
 
restart() {
 
    configtest || return $?
 
    stop
 
    start
 
}
 
 
reload() {
 
    configtest || return $?
 
    echo -n $"Reloading $prog: "
 
    killproc $nginx -HUP
 
    RETVAL=$?
 
    echo
 
}
 
force_reload() {
 
    restart
 
}
 
 
configtest() {
 
  $nginx -t -c $NGINX_CONF_FILE
 
}
 
 
 
rh_status() {
 
    status $prog
 
}
 
 
rh_status_q() {
 
    rh_status >/dev/null 2>&1
 
}
 
case "$1" in
 
    start)
 
        rh_status_q && exit 0
        $1
        ;;
 
    stop)
 
 
        rh_status_q || exit 0
        $1
        ;;
 
    restart|configtest)
        $1
        ;;
 
    reload)
        rh_status_q || exit 7
        $1
        ;;
 
 
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
 
 
    condrestart|try-restart)
 
        rh_status_q || exit 0
            ;;
 
    *)
 
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

nginx="/usr/local/nginx/sbin/nginx"这里的文件目录为你自己的目录,得确认你sbin目录下有nginx这个文件。
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"这个也同上,注意自己的目录。
lockfile=/var/lock/subsys/nginx这一行不是很重要,我也不是很理解,我的subsys下并没有nginx这个文件,但是整个运行时没有报错的。
四、systemctl start nginx
五、修改nginx.conf文件。

rtmp {
        server {
            listen 1935;
            #server_name localhost;
            application liveApp {
                live on;
             }

             application vod {
                play /home/admin/ftp/video;//你的视频存放的位置
             }

             application vod_http {
                 play http://119.23.234.4/vod;
             }

             application hls {
                 live on;
                 hls on;
                 hls_path /home/admin/ftp/software/nginx/objs/addon/hls;#注意为你的实际目录,可以通过find / -name hls查找。
             }
        }
}
location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
           root /usr/local/nginx/nginx-rtmp-module/;#注意,确保你的nginx下有这个文件,没有的话,可以把你解压后的文件复制粘贴过来。这里填你的原来的目录报错。
        }

        location /hsl {
          types {
            application /vnd.apple.mpegurl m3u8;
            video/mp2t ts;
          }
          alias  /usr/local/nginx/nginx-rtmp-module/hls;//复制粘贴过来之后的hls目录。
          add_header Cache-Control no-cache;
        }

        #location /dash {
            #root /tmp;
            #add_header Cache-Control no-cache;
        #}

合理安装之后,/usr/local/nginx下应当是有rtmp模块文件的,没有也没关系,只要你编译nginx的时候,他没有报错,(没有报not found错误)。把nginx-rtmp-module复制粘贴过去即可。
六、演示:
在这里插入图片描述
在这里插入图片描述