对于没有接触过nginx的我,在看了别人搭建的直播服务后心痒痒了,也就照着搭建了一个直播服务,我是在阿里云服务器上搭建的,首先来说一下阿里云服务器,我买的是一个ECS的云服务器,系统是CentOS7
然后用Xshell连接上我的服务器,发现连接不上,原来是阿里云服务器的安全策略在起作用,默认端口是不开放的,所以我们需要开放端口,打开控制台,找到自己的云服务器,打开实例,然后打开安全组去配置规则
在这里开放需要用到的端口,我开放了21,22,80,81号端口,然后我们去安装nginx和rtmp模块
1,安装rtmp及nginx
获取rtmp安装包,百度网盘。
获取nginx安装包,百度网盘。
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.8.1.tar.gz 公共 视频 文档 音乐
initial-setup-ks.cfg nginx-rtmp-module-master.zip 模板 图片 下载 桌面
2,解压压缩包
[root@localhost ~]# unzip nginx-rtmp-module-master.zip
[root@localhost ~]# tar -xvf nginx-1.8.1.tar.gz
3,编译安装nginx
[root@localhost ~]# cd nginx-1.8.1/
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
当然都知道编译安装的前提是要有编译安装的工具,所以先来安装一下编译安装的工具
[root@localhost nginx-1.8.1]# yum groupinstall "development tools" -y
[root@localhost nginx-1.8.1]# yum install openssl-devel openssl -y
然后再来configure
[root@localhost nginx-1.8.1]# ./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
./configure: error: no ../nginx-rtmp-module/config was found
出现错误,找不到文件,改rtmp目录名即可
[root@localhost nginx-1.8.1]# cd
[root@localhost ~]# mv nginx-rtmp-module-master nginx-rtmp-module
继续
[root@localhost nginx-1.8.1]# ./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
这样就成功了。
编译安装
[root@localhost nginx-1.8.1]# make&&make install
至此,nginx安装完毕,接下来便是编辑配置文件
4,编辑nginx配置文件
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 81;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
rtmp {
server {
listen 1935;
application hls {
live on;
hls on;
hls_path /usr/local/nginx/html/hls;
hls_fragment 2s;
}
}
}
这便是我的配置文件,我没有监听80端口而监听的81端口
[root@localhost nginx-1.8.1]# mkdir /usr/local/nginx/html/hls/
5,启动nginx
[root@localhost nginx]# cd /usr/local/nginx/
[root@localhost nginx]# ./sbin/nginx
6,推流
下载OBS
在设置之中推流
流名称可以任意填写,我填的是test
6,下载vlc,串流
在串流网络中输入URL
rtmp://XXX/hls/test XXX为主机IP
串流之后即可看到推流内容
这样,直播服务搭建完毕