Nginx 服务器配置与搭建
环境准备
-
下载Nginx wget http://nginx.org/download/nginx-nginx-1.17.8.tar.gz
-
解压Nginx tar -zxvf nginx-nginx-1.17.8.tar.gz
-
下载Nginx RTMP模块 wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
-
解压Nginx RTMP模块 tar -zxvf v1.2.1.tar.gz
编译及安装
通过命令[root@VM_0_2_centos nginx-1.17.8]# ./configure --help
查看编译参数配置。 ./
表示当前目录
1. nginx 引入rtmp模块
nginx 默认不支持rtmp模块,需要引入第三方的nginx-rtmp-module-1.2.1
执行命令:[root@VM_0_2_centos nginx-1.17.8]# ./configure --prefix=./bin --add-module=../nginx-rtmp-module-1.2.1
-
./configure
:执行脚本 -
--prefix
:路径 -
./bin
:当前目录,也就是跟./configure
同级目录下新建bin
目录 -
--add-module=
:添加模块 -
../
:相对路径,相对于当前目录的上一级目录下的nginx-rtmp-module-1.2.1
-
执行结果:
nginx 中 gzip 模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要 openssl 库。所以如果服务器未安装这三个依赖库的话会报错,需要先安装这三个依赖库
- 下载
wget https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
- 解压
tar -zxvf pcre-8.40.tar.gz
- 配置
./configure
- 编译
make
- 安装
make install
- 检测安装
rpm -qa pcre
- 下载
2. 编译:make install
3. 编译完生成bin目录
```
[root@VM_0_2_centos nginx-1.17.8]# ls
auto bin CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man nginx_configure_help.txt objs pcre-8.40 pcre-8.40.tar.gz README src
[root@VM_0_2_centos nginx-1.17.8]# cd bin/
[root@VM_0_2_centos bin]# ls
conf html logs sbin
[root@VM_0_2_centos bin]#
```
conf:配置相关
html:欢迎页面、错误页面
logs:日志存放区
sbin:可执行文件存放区
修改配置
Nginx默认不支持rtmp,需要修改配置文件。
如何修改,可以参考:nginx-rtmp-module-1.2.1/test/nginx.conf
进入bin/conf目录,找到 nginx.conf 文件。
首先备份nginx.conf
,执行重命名命令:mv nginx.conf nginx.conf.bak
。
然后新建一个nginx.conf
,编辑:
worker_processes 1;
# 错误日志 存放路径
error_log logs/error.log debug;
events {
# 最大连接数
worker_connections 1024;
}
rtmp {
server {
#注意端口占用,1935为默认端口 ,可修改
listen 1935;
# myapp 可以理解为主播的房间号,可修改
application myapp {
# 开启直播功能
live on;
#丢弃闲置5s的连接
drop_idle_publisher 5s;
}
}
}
http {
server {
#注意端口占用
listen 8080;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
#注意目录
root /root/junlyuan/nginx-rtmp-module-1.2.1;
}
location /control {
rtmp_control all;
}
location /rtmp-publisher {
#注意目录
root /root/junlyuan/nginx-rtmp-module-1.2.1/test;
}
location / {
#注意目录
root /root/junlyuan/nginx-rtmp-module-1.2.1/test/www;
}
}
}
启动服务
进入sbin目录尝试执行nginx:
[root@VM_0_2_centos conf]# cd ../sbin/
[root@VM_0_2_centos sbin]# ls
nginx
[root@VM_0_2_centos sbin]# ./nginx
nginx: [alert] could not open error log file: open() "./bin/logs/error.log" failed (2: No such file or directory)
2020/02/14 16:21:42 [emerg] 5057#0: open() "./bin/conf/nginx.conf" failed (2: No such file or directory)
仔细看错误说明,"./bin/logs/error.log" 文件找不到?也就是当前目录下找不到 bin/logs/error.log。我们执行的当前目录是sbin,里面只有可执行文件nginx,当然找不到了。所以需要到nginx根目录下执行。
[root@VM_0_2_centos sbin]# cd ../../
[root@VM_0_2_centos nginx-1.17.8]# ./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
重新启动[root@VM_0_2_centos nginx-1.17.8]# ./bin/sbin/nginx
查看进程:ps aux | grep nginx
[root@VM_0_2_centos nginx-1.17.8]# ps aux | grep nginx
root 7506 0.0 0.2 53380 5320 ? Ss 16:34 0:00 nginx: master process ./bin/sbin/nginx
nobody 7507 0.0 0.3 55848 6160 ? S 16:34 0:00 nginx: worker process
root 7647 0.0 0.0 112712 964 pts/0 R+ 16:35 0:00 grep --color=auto nginx
双进程守护:master process
,worker process
master process
主线程 。root
权限
worker process
工作线程。没有指定权限,默认nobody
可以看到 服务器启动成功。
ps:
检测端口是否占用:
[root@VM_0_2_centos nginx-1.17.8]# lsof -i:1935
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 7506 root 6u IPv4 1336219 0t0 TCP *:macromedia-fcs (LISTEN)
nginx 7507 nobody 6u IPv4 1336219 0t0 TCP *:macromedia-fcs (LISTEN)
[root@VM_0_2_centos nginx-1.17.8]# lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 7506 root 7u IPv4 1336220 0t0 TCP *:webcache (LISTEN)
nginx 7507 nobody 7u IPv4 1336220 0t0 TCP *:webcache (LISTEN)
可以看到端口已经被占用,因为刚刚已经启动了nginx服务。
测试服务
在windows浏览器中通过http来访问:http://xxx.xxx.xxx.xxx:8080/
如果无法连接到服务器:
- 首先检查配置文件,相关的路径是否配置正确。
- 检查服务器是否开放了
nginx中配置的端口
。
再次访问,发现 已经连接到nginx服务器,但报 404
需要检查配置文件,或者重启服务
[root@VM_0_2_centos nginx-1.17.8]# pkill -9 nginx
[root@VM_0_2_centos nginx-1.17.8]# ./bin/sbin/nginx
[root@VM_0_2_centos nginx-1.17.8]# ps aux | grep nginx
root 27738 0.0 0.2 53368 5320 ? Ss 23:04 0:00 nginx: master process ./bin/sbin/nginx
root 27739 0.0 0.3 55852 6156 ? S 23:04 0:00 nginx: worker process
root 27747 0.0 0.0 112712 964 pts/0 R+ 23:04 0:00 grep --color=auto nginx
至此在浏览器访问,发现正常访问。
PS:如报错:403 Forbidden
报错:403 Forbidden
其实前面我们查看nginx进程的时候,可以发现master process和worker process的用户不一致,一个是root而另一个是nobody。工作进程与主进程不在一个用户组,拒绝访问。
重新修改nginx.conf文件,添加root用户。
#设置为root用户
user root;
worker_processes 1;
# 错误日志 存放路径
error_log logs/error.log debug;
配置文件更改了,需要重新加载配置文件。
./bin/sbin/nginx -s reload
测试推流
-
使用录屏软件,设置参数开始推流
-
点击3 ,设置推流地址
然后点击4启动,开始推流。
-
通过浏览器
http://xxx.xxx.xxx.xxx:8080/stat
查看推流state
-
直播测试
可以使用EVPlayer_v3.4.0
播放。
也可以使用 ffmpeg自带的播放器播放:ffplay -i rtmp://111.229.245.175/myapp/
。