一.环境准备
- 云服务器/xshell
- 如:阿里云ECS服务器和FinalShell;
二.操作步骤
- 建议:全程所有的操作都保持一致(最省时间)。
1.新建文件夹
- 在root目录下执行以下两个命令
- mkdir rtmp && cd rtmp
2.安装git工具
- yum install git
3.下载nvm是node版本控制器
git clone git://github.com/creationix/nvm.git ~/nvm
- 遇到问题:fatal: 无法连接到 github.com,github.com[0: 20.205.243.166]: errno=连接超时。
- 灵活处理:从github上面下载nvm,然后上传到Linux服务器上。
4.设置nvm自动运行
echo "source ~/nvm/nvm.sh" >> ~/.bashrc
source ~/.bashrc
5.查询node版本
nvm list-remote
6.安装node
nvm install v11.6.0
7.安装pcre依赖
yum -y install pcre*
8.安装openssl依赖
yum -y install openssl*
9.下载文件
- 当前在rtmp目录下,下载两个文件
wget http://nginx.org/download/nginx-1.15.3.tar.gz
wget https://codeload.github.com/arut/nginx-rtmp-module/tar.gz/v1.2.1
10.解压文件并进入Nginx目录
tar xvf v1.2.1
tar xvf nginx-1.15.3.tar.gz
cd nginx-1.15.3
11.生成nginx 安装文件
#--add-module 指向rtmp模块目录
./configure --prefix=./bin --add-module=../nginx-rtmp-module-1.2.1
#此时,查看是否生成了bin目录,若生成,则执行以下命令。执行以下命令可能会报错-nginx安装时/ngx_murmurhash.c:37:11: error: this statement may fall through -Werror=implicit-fallthroug,处理方案进入objs目录下,编辑MakeFile中的内容,去掉以CFLAGS开头一行中的 -Werror,然后再执行以下命令。
make -j4
make install
- 在这个过程中可能因为环境不同而出现不同错误,比如缺少pcre、openssl等,这时候可能就需要安装这些库。 https://blog.csdn.net/z920954494/article/details/52132125
12.cd到bin/conf目录下
-
注意:需要在当前目录下检查是否有bin目录
-
通过Vim编辑nginx.conf文件,先通过d删除文件的原始内容(全部删除);添加以下信息(在粘贴的时候把注释都去掉,避免出错)
#使用root权限
user root;
#工作的进程
worker_processes 1;
#错误日志存放的位置
error_log /root/rtmp/nginx-1.15.3/bin/logs/error.log debug;
#时间
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;#之后推流拉流的端,1935自己随便定
application live {
live on;
}
}
}
http{
server{
listen 8080;//8080自行定义
server_name localhost;
location /stat.xsl
{
root /root/rtmp/nginx-rtmp-module-1.2.1;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /control{
rtmp_control all;
}
location /rtmp-publisher
{ #注意目录
root /root/rtmp/nginx-rtmp-module-1.2.1/test;
}
location / {
#注意目录
root /root/rtmp/nginx-rtmp-module-1.2.1/test/www;
}
}
}
- 注意注意注意:配置文件中的1935,8080需要在服务器上添加进安全组(需要添加的端口还挺多)
13.运行nginx
- 连续执行两次返回上一级文件夹的操作。在/root/rtmp/nginx-1.15.3目录下启动nginx,执行命令:
./bin/sbin/nginx -c conf/nginx.conf
- 在启动之前,可以通过命令检查nginx是否存在配置错误(在/root/rtmp/nginx-1.15.3目录下执行)执行命令:
./bin/sbin/nginx -t
//没有报错会提示
//nginx: the configuration file ./bin/conf/nginx.conf syntax is ok
//nginx: configuration file ./bin/conf/nginx.conf test is successful
- 在启动nginx时,可能因为操作问题会产生以下信息
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
-
处理方案:杀掉被占用的nginx进程
- 查看80占用的进程号命令:
netstat -ntlp|grep 80
- 杀掉具体的进程命令
kill -9 进程号
- 进程杀掉后可能会自动开启新的进程,处理方法:杀掉全部的nginx进程,命令为:
killall -9 nginx
14.测试直播服务器是否搭建成功
- 在pc端上输入:公网IP+端口号
- 或者输入:http://公网IP:8080/stats
- 能看到以上两个页面说明推流服务器环境搭建成功
三.总结
- 搭建直播服务器并不难,但是细节需要注意,不然会很耗费时间。好记性不如烂笔头,越是涉及到过多细节越是需要记录。
评论区