Nginx 安装和使用

43 阅读3分钟

nginx-01.png

介绍

常见用法

  1. web服务器软件 httpd http协议
  2. 代理服务器 反向代理
  3. 邮箱代理服务器 IMAP POP3 SMTP
  4. 负载均衡功能 LB loadblance

Nginx架构的特点:

高可靠:稳定性 master进程 管理调度请求分发到哪一个worker=> worker进程 响应请求 一master多worker

热部署 :(1)平滑升级 (2)可以快速重载配置

高并发:可以同时响应更多的请求 事件 epoll模型 几万

响应快:尤其在处理静态文件上,响应速度很快 sendfile 机制

低消耗:cpu和内存 1w个请求 内存2-3MB

分布式支持 :反向代理 七层负载均衡

安装

首先我们进入官网:nginx.org/,选择 download,可以 stable version(稳定版本下载)

1702691025008.png

上传到 linux 上

Snipaste_2023-12-16_09-52-36.png

# 解压
tar -zxvf nginx-1.14.2.tar.gz
# 进入目录
cd nginx-1.14.2
# 源码编译(查看缺少的依赖,具体可以看下面的图片)
./configure
# 安装 PCRE
yum install pcre-devel -y
# 源码编译并设置安装到哪个目录
./configure --prefix=/usr/local/nginx
# 安装 zlib
yum install -y zlib-devel
# 再次检测
./configure --prefix=/usr/local/nginx
# 安装 openssl
yum install -y openssl-devel
# 再次检测
./configure --prefix=/usr/local/nginx --with-http_ssl_module
# 创建用户
useradd -s /sbin/nologin -M www
# 编辑
make && make install
# 启动 ngxin
cd /usr/local/nginx/sbin
./nginx

1702691913178.png

1702692161147.png

1702692460970.png

验证安装是否成功

Snipaste_2023-12-16_11-23-17.png

编译参数说明

参数作用
--prefix编译安装到的软件目录
--userworker进程运行用户
--groupworker进程运行用户组
--with-http_ssl_module支持https 需要==pcel-devel==依赖
--with-http_stub_status_module基本状态信息显示 查看请求数、连接数等
--with-http_realip_module定义客户端地址和端口为header头信息 常用于反向代理后的真实IP获取

目录介绍

查看安装目录/usr/local/nginx

目录作用
conf配置文件
html网站默认目录
logs日志
sbin可执行文件 [软件的启动 停止 重启等]

软件操作参数

查看nginx的二进制可执行文件的相关参数

cd /usr/local/nginx/sbin
./nginx -h

执行后显示

nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
#查看帮助
  -?,-h         : this help
#查看版本并退出
  -v            : show version and exit
#查看版本和配置选项并退出
  -V            : show version and configure options then exit
#检测配置文件语法并退出
  -t            : test configuration and exit
#检测配置文件语法打印它并退出
  -T            : test configuration, dump it and exit
#在配置测试期间禁止显示非错误信息
  -q            : suppress non-error messages during configuration testing
#发送信号给主进程  stop强制退出  quit优雅的退出  reopen重开日志   reload重载配置
  -s signal     : send signal to a master process: stop, quit, reopen, reload
#设置nginx目录  $prefix路径
  -p prefix     : set prefix path (default: /usr/local/nginx/)
#指定启动使用的配置文件
  -c filename   : set configuration file (default: conf/nginx.conf)
#在配置文件之外设置全局指令
  -g directives : set global directives out of configuration file

一般主要使用:

-s参数控制管理nginx服务

-V参数查看nginx开启的模块和编译参数

-t参数检测配置文件是否有错误

服务脚本配置

使用社区的服务配置文件

nginx编译包里默认没有服务启动脚本模板,可以通过社区获得

www.nginx.com/resources/w…

上传脚本到/etc/init.d目录下

vim /etc/init.d/nginx
#增加执行权限
chmod +x nginx

修改软件和配置路径

#执行文件路径  第22行
nginx="/usr/local/nginx/sbin/nginx"
#配置文件路径  第25行
NGINIX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

添加自启动

chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

注意在服务脚本中,有chkconfig配置开启模式、开启顺序、关闭顺序设置

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#              开启模式(0-6)   开启顺序  关闭顺序      
# chkconfig:   - 85 15

配置完后命令

#启动nginx
server nginx start
#查看状态
server nginx status
#关闭nginx
server nginx stop