# nginx安装配置
> [!tip] 参考文档[直播流转码](https://wxzzz.com/425.html)
> 采用`ubuntu20.04`进行安装,`centos`自行替换
> 文章的`nginx`版本为`1.8.1`,新版本的插件不支持,所以用的`1.20.2`
## 所需环境软件包
> [!tip] `nginx-flv-module`基于`nginx-rtmp-module`进行开发,所以安装`flv`模块后可以直接用
1. [nginx](http://nginx.org/)
2. [nginx-http-flv-module](https://github.com/winshining/nginx-http-flv-module)
3. 推流工具 `obs`或者`ffmpeg`
4. 播放工具 `vlc`或者`flv`在线播放工具

## 编译安装
### 前置环境配置
1. 配置编译前置环境`pcre`库,可能还需要`zip` `g++`等库来解压或者编译
<!-- tabs:start  -->
#### **ubuntu**
> 非`ubuntu`可跳过
```shell
sudo apt-get install libpcre3 libpcre3-dev 
```
你可能还需要安装,安装时没出现
```shell
sudo apt-get install openssl libssl-dev
```
#### **centos**
> 非`centos`可跳过
```shell
yum -y install unzip
yum -y install gcc-c++ 
yum -y install pcre pcre-devel  
yum -y install zlib zlib-devel 
yum -y install openssl openssl-devel
```
<!-- tabs:end -->
2. 下载对应的数据包`nginx-flv-module`以及`nginx`
> [!tip] 下载`nginx-flv-module`模块,如果下载不动可以选择下载 `zip` 然后在解压
> 可选克隆方案,或者下载zip方案
<!-- tabs:start  -->
#### **克隆方案**
```shell
git clone https://github.com/winshining/nginx-http-flv-module.git
```
#### **zip方案**
```shell
wget https://github.com/winshining/nginx-http-flv-module/archive/refs/heads/master.zip
unzip nginx-http-flv-module-master.zip
```
<!-- tabs:end -->
3. 下载编译`nginx`
> 下载并解压`nginx`,选择版本为`1.20.2`
```shell
wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar -zxvf ./nginx-1.20.2.tar.gz
cd ./nginx-1.20.2
```
> 为`nginx`添加`flv`模块, `--add-module`可以多次输入,等号后接模块路径
> 为防止权限问题,make install 用`sudo`进行获取权限
```shell
./configure --prefix=/usr/local/nginx  --add-module=../nginx-http-flv-module
make
sudo make install
```

## 启动以及配置`nginx`
> `nginx` 的二进制文件在 对应目录下`sbin`文件夹内
1. 配置`nginx`,编辑`nginx`配置文件
```shell
sudo vim /usr/local/nginx/conf/nginx.conf
```
2. 配置参考
```shell
#定义Nginx运行的用户和用户组
user nginx nginx;

#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /home/nginx/log/error.log info;


events {
    worker_connections  1024;
}
rtmp {
   server {
    listen 1988;
    chunk_size 4000;
    notify_method get;
   #直播
    application live {
        live on;
        publish_notify on;
        # 这个地址可以用来作权限验证,可以不填写,内部内容自行查找
        # on_publish http://127.0.0.1:80/on_publish.php;
    }
   #点播
   application vod {
         play /tmp/video;
      }

   }
}


http {
    # http监听地址,可以忽略
    server {
        listen 80;
        server_name  localhost;
    }
    # flv转发地址
    server {
        # 端口
        listen 19123;
        server_name localhost;
        location / {
                add_header 'Access-Control-Allow-Origin' '*';
                root html;
                index  index.html index.htm;
        }
        # 原始流地址
        location /live {
                flv_live on;
        }
        # 转换为flv后的流地址 请求时 ip:port/flv
        location /flv {
            add_header 'Access-Control-Allow-Origin' '*';
            flv_live on;
            chunked_transfer_encoding on;
        }
    }
}

```
3. 查看配置是否正确 `nginx -t`
> [!tip] 进入到`nginx`目录下的`sbin`文件夹执行目录
> 问题自行解决,无效配置会提示
```shell
sudo ./nginx -t
```
4. 启动`nginx`
```shell
sudo ./nginx
```
Tips:停止nginx服务`./nginx -s stop`
```shell
./nginx -s stop
```
重新加载配置 `./nginx -s reload`
```shell
./nginx -s reload
```
通过以上配置,即可完成服务端的直播环境配置。

5. 查看是否启动成功
> [!tip] 如果没有返回,代表没启动成功,检查配置.  浏览器访问`ip`,有显示`nginx`即可
```shell
sudo lsof -i:19123
sudo lsof -i:1988
sudo lsof -i:80
```
## 测试推流
> 使用`obs`进行推流