安装所需依赖
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
下载nginx程序包
cd /home
wget http://nginx.org/download/nginx-1.19.5.tar.gz
# 如果没有wget命令执行如下命令安装
yum install -y wget
解压压缩包
tar -zxvf nginx-1.19.5.tar.gz
编译并安装nginx
cd nginx-1.19.5
# 编译程序并指定程序安装参数
./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-stream
# 利用make进行安装
make && make install
# 参数说明
# –prefix:指定安装目录
# –with-http_dav_module:启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启;
# –with-http_stub_status_module:启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态);
# –with-http_addition_module:启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求);
# –with-http_sub_module:启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本);
# –with-http_flv_module:启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件);
# –with-http_mp4_module:启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件)
配置nginx.service和开机自启
# 进入服务目录
cd /usr/lib/systemd/system/
# 创建 nginx.service 文件
vi nginx.service
# 键入以下内容:
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx
PrivateTmp=true
[Install]
WantedBy=multi-user.target
授权nginx.service启动权限和启动命令
# 授权nginx.service文件
chmod 755 nginx.service
# 启动
systemctl start nginx
# 关闭
systemctl stop nginx
# 重启
systemctl restart nginx
# 开机自启
systemctl enable nginx
# 关闭开机自启
systemctl disable nginx