1. 下载nginx
- 安装wget
yum install wget
- 下载nginx
从www.nginx.org上下载最新版本的nginx,并解压,如无特别说明,本示例操作均在root根目录下操作。wget http://nginx.org/download/nginx-1.16.1.tar.gz
- 解压nginx压缩文件
tar -zvxf nginx-1.16.1.tar.gz
得到nginx安装文件目录
2. 安装依赖库
- 安装nginx所需的依赖库
yum -y install gcc gcc-c++ autoconf automake make
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel
3. 下载rtmp模块
如果不需要安装rtmp模块,可忽略这一步。
- 安装git
yum -y install git
- 从github下载rtmp模块
git clone https://github.com/arut/nginx-rtmp-module.git
4. 安装nginx
-
编译安装nginx
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
- prefix:安装目录
- add-module: 添加外部模块(如果不需要安装rtmp模块,则去掉此参数)
- with-http_ssl_module:启用添加HTTPS协议支持到HTTP服务器的模块,该模块默认不启用。构建和运行该模块需要OpenSSL库。
make & make install
cd ~
ll /usr/local/nginx
5. 配置nginx
默认nginx配置文件为/usr/local/nginx/conf/nginx.conf nano /usr/local/nginx/conf/nginx.conf
在配置文件中添加如下配置
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /root/nginx-rtmp-module/;
}
location /live {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /data/video/hls;
expires -1;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
# 如果不需要rtmp可取消此处配置,以及http中的/stat, /stat.xls, /live配置
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
hls on;
hls_path /data/video/hls;
hls_fragment 2s;
hls_playlist_length 10m;
hls_continuous on;
hls_cleanup on;
hls_nested on;
}
}
}
6. 启动关闭nginx
- 开放80和1935端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=1935/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
查看打开端口是否包括80和1935。 - 启动、关闭nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
打开nginx后,在浏览器中打开http://ip,如果能看到欢迎界面,则表示安装成功 - 关闭nginx
/usr/local/nginx/sbin/nginx -s stop
7. 配置nginx自动启动服务
- 添加并便捷系统服务文件 /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.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
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 修改服务文件权限
chmod 755 /usr/lib/systemd/system/nginx.service
- 设置开机自启动
systemctl enable nginx