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/
。