1、增加nginx用户和用户组
groupadd -r -g 1003 nginx
useradd -r -g nginx -u 1003 -s /bin/false nginx
2、上传并解压nginx安装包
上传nginx-1.16.1.tar.gz到/tmp目录
cd /tmp
tar -xzvf nginx-1.16.1.tar.gz -C /usr/local
3、编译、安装nginx
cd /usr/local/nginx-1.16.1
#配置准备
./configure --prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-http_v2_module
–with-http_stub_status_module
–with-http_ssl_module
–with-http_realip_module
–with-http_addition_module
–with-http_sub_module
–with-http_flv_module
–with-http_mp4_module
–with-http_auth_request_module
–with-http_secure_link_module
–with-http_slice_module
–with-stream
–with-stream=dynamic
–with-stream_ssl_module
–with-stream_realip_module
–with-stream_ssl_preread_module
–with-pcre
#安装nginx
make install
4、配置nginx配置文件
#备份nginx的配置文件
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bakyyyymmdd
#编辑nginx.conf配置文件
vi /usr/local/nginx/conf/nginx.conf
nginx.conf的相关配置示例如下:
user nginx;
server {
listen 80;
server_name localhost;
#设置nginx默认服务的侦听端口80
location / {
root html/dist;
index index.html index.htm;
}
#设置默认服务的根目录和默认文件(前端文件存放位置)
location /dev-api/ {
proxy_set_header Host
h
o
s
t
:
host:
host:server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://后端服务器ip:8080/;
#请求后端接口服务
}
#修改完成后保存
:wq
#建议增加的nginx安全配置(http模块)
add_header Referrer-Policy “no-referrer-when-downgrade”;
add_header X-XSS-Protection 1;
add_header X-Content-Type-Options nosniff;
add_header X-Permitted-Cross-Domain-Policies “1; mode=block”;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Download-Options noopen;
#测试nginx.conf的语法是否正确
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 表示测试通过。
5、启动nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6、将nginx设置为systemd启动、管理方式
#生成nginx.service文件
vi /usr/lib/systemd/system/nginx.service
#将以下内容输入nginx.service文件:
[Unit]
Description=nginx a high performance web server
After=network.target remote-fs.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#给nginx.service文件赋予执行权限
chmod +x /usr/lib/systemd/system/nginx.service
#重新加载systemd
systemctl daemon-reload
#启动nginx
systemctl start nginx
#查看nginx状态,查看nginx日志
systemctl status nginx
journalctl -xe
7、nginx安装rtmp模块
将nginx-rtmp-module-master.tar.gz压缩包上传到服务器/tmp目录
cd /tmp
tar -xzvf nginx-rtmp-module-master.tar.gz -C /usr/local/nginx-1.16.1/
cd /usr/local/nginx-1.16.1/
#重新编译nginx
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_v2_module --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_auth_request_module --with-http_secure_link_module --with-http_slice_module --with-stream --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-pcre --add-module=./nginx-rtmp-module-master
#使用新的nginx可执行程序替换原来的nginx程序
mv …/nginx/sbin/nginx …/nginx/sbin/nginx.old
cp objs/nginx …/nginx/sbin/
#创建流视频文件,向nginx用户授权:
mkdir /mnt/hls
chown -R nginx.nginx /mnt/hls
#修改nginx.conf配置文件,在http相同的层次加入:
#rtmp服务
rtmp {
server {
listen 19305; #监听的端口
chunk_size 4000;
application live {
live on; #开启实时
hls on; #开启hls
hls_path /mnt/hls; #rtmp推流请求路径,文件存放路径
hls_fragment 5s; #每个TS文件包含5秒的视频内容
}
}
}
#重新加载nginx使服务生效
systemctl restart nginx
了解更多内容 关注公众号