【Linux基础服务教程】Nginx的安装和介绍

Nginx的官网:https://nginx.org

一、Nginx的作用

  • 配置web服务
    • 高并发、高性能
  • 反向代理服务器

1.Nginx的特点

  • 开源、跨平台
  • 高度模块化
  • 高并发、高性能
  • 支持虚拟主机
  • 支持https
  • 支持url重写

2.Nginx高效的原因

  • 基于异步非阻塞模型或者叫异步IO模型
  • 异步和同步的介绍:
    • 异步速度快
    • 同步速度慢,但是可靠性最高
  • 非阻塞和阻塞的介绍:
    • 阻塞
      • 进程必须等待磁盘IO完成
    • 非阻塞
      • 进程在等待磁盘IO的同时,可以处理其他事务
  • 基于epoll事件驱动模型设计的
    • select
      • 周期性询问, 限制最大文件数1024
    • poll
      • 周期性询问,取消最大文件数的限制
    • epoll
      • 通知机制

二、Nginx的安装部署

1.下载Nginx安装包

[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz

2.安装必要的依赖

[root@localhost ~]# yum install -y gcc openssl-devel pcre-devel zlib-devel 

3.创建Nginx临时文件目录(可选的)

如果不指定临时文件存放目录,默认会和安装目录在一起

[root@localhost ~]# mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}

4.创建Nginx用户(可选的)

默认使用nobody用户

[root@localhost ~]# useradd -s /sbin/nologin nginx 

5.安装Nginx

A.解压安装包

[root@localhost ~]# tar xf nginx-1.20.2.tar.gz 

B.进入目录

[root@localhost ~]# cd nginx-1.20.2/

C.编译安装

[root@localhost nginx-1.20.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-stream --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-file-aio --with-http_secure_link_module --with-threads --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ --http-scgi-temp-path=/var/tmp/nginx/scgi/
[root@localhost nginx-1.20.2]# make && make install

三、Nginx相关文件

  • nginx安装目录/conf
    • 配置文件nginx.conf主配置文件
  • nginx安装目录/logs
    • 存放日志
  • nginx安装目录/html
    • 默认网页目录
  • nginx安装目录/sbin
    • 二进制文件

四、Nginx启动管理

1.启动Nginx

[root@localhost ~]# /usr/local/nginx/sbin/nginx 

[root@localhost ~]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9726/nginx: master  

[root@localhost ~]# ps -elf | grep nginx 
1 S root       9726      1  0  80   0 - 11499 sigsus 15:56 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nginx      9727   9726  0  80   0 - 11612 ep_pol 15:56 ?        00:00:00 nginx: worker process
  • master process主进程
    • 派生子进程、记录日志、重新加载配置文件
  • worker process工作进程
    • 接收、处理客户端访问请求

2.设置Nginx开机自启动

[root@localhost ~]# sed -ri '$a \/usr/local/nginx/sbin/nginx' /etc/rc.d/rc.local
[root@localhost ~]# chmod a+x /etc/rc.d/rc.local

3.停止Nginx服务

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop

4.重新加载Nginx配置文件

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

5.检测Nginx配置文件语法

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

6.查看Nginx版本

[root@localhost ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.20.2

7.查看Nginx配置参数

[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-file-aio --with-http_secure_link_module --with-threads --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ --http-scgi-temp-path=/var/tmp/nginx/scgi/