环境准备

安装前先要确定Linux的内核版本要在2.6以上,只有2.6之后才支持epool ,在此之前使用select或pool多路复用的IO模型,无法解决高并发压力的问题。通过命令uname -a 即可查看:
image.png
另外Nginx需要GCC编译器,用来编译C语言环境,Nginx不会提供二进制可执行程序,只能下载源码进行编译;

  1. yum install gcc gcc-c

另外还需要PCRE库,是专门用来做正则表达式的,Nginx里面很多地方都用到了正则表达式,因此需要进行安装;

  1. yum install pcre pcre-devel

另外还需要zlib库,zlib库用于对HTTP包的内容做gzip格式的压缩,如果我们在nginx.conf里配置了gzip on,并指定对于某些类型(content-type)的HTTP响应使用gzip来进行压缩以减少网络传输量;
另外还需要OpenSSL开发库,如果我们的服务器不只是要支持HTTP,还需要在更安全的SSL协议上传输HTTP,那么就需要拥有OpenSSL了。另外,如果我们想使用MD5、SHA1等散列函数,那么也需要安装它;
上面几个库都是Nginx 基础功能所必需的,为简单起见我们可以通过yum 命令统一安装:

  1. yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel

安装

nginx官方地址https://nginx.org/en/download.html
Nginx 服务器三种版本的下载,分别是开发版(Mainline version)、稳定版本(Stable version)和历史版本(Legacy versions)。
image.png

“CHANGES-x.xx”链接,记录的是对应版本的功能变更日志。包括新增功能、功能的优化和功能缺陷的修复等。
紧接着“CHANGES-x.xx”链接后面的“nginx-x.x.x”链接,是 Nginx服务器的 Linux版本下载地址。
“pgp”链接,记录的是提供下载的版本使用PGP加密自由软件GnuPG计算后的签名。PGP可以理解为Pretty Good Privacy。这些数据可以用于下载文件的验证。
“nginx/Windows-x.x.x”链接,是 Nginx 服务器的Windows版本下载地址。

源代码安装-离线安装

下载源代码

  1. #wget http://nginx.org/download/nginx-1.14.2.tar.gz
  2. #tar -zxvf nginx-1.14.2.tar.gz

源码文件目录结构

  1. nginx-1.14.2 baxiang$ tree -d
  2. .
  3. ├── auto
  4. ├── cc
  5. ├── lib
  6. ├── geoip
  7. ├── google-perftools
  8. ├── libatomic
  9. ├── libgd
  10. ├── libxslt
  11. ├── openssl
  12. ├── pcre
  13. ├── perl
  14. └── zlib
  15. ├── os
  16. └── types
  17. ├── conf
  18. ├── contrib
  19. ├── unicode2nginx
  20. └── vim
  21. ├── ftdetect
  22. ├── ftplugin
  23. ├── indent
  24. └── syntax
  25. ├── html
  26. ├── man
  27. └── src
  28. ├── core
  29. ├── event
  30. └── modules
  31. ├── http
  32. ├── modules
  33. └── perl
  34. └── v2
  35. ├── mail
  36. ├── misc
  37. ├── os
  38. └── unix
  39. └── stream
  40. 37 directories

src: 源代码
man: 帮助文档
html:默认首页网站文件
contrib 其他结构或者组织共享的文档资料
conf:配置文件
configure 自动安装脚本,用于检查安装环境
auto:脚本文件 和configure程序相关
依赖库如下:

  1. yum install gcc gcc-c++ automake pcre-devel zlib-devel openssl-devel

pcre-devel :提供正则表达式
zlib-devel:提供压缩
openssl-devel 提供证书以及ssl协议

  1. # cd nginx-1.14.2/
  2. #./configure --prefix=/usr/local/nginx --with-http_ssl_module
  3. #make
  4. #make install
  5. #cd /usr/local/nginx/sbin
  6. #./nginx
  7. # ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx // 创建软连接

./configure 用于对安装的软件进行配置,检查 当前的环境是否满足安装软件 ( Nginx)的依赖关系 。—prefix选项用于设置 Nginx 的安装目录, 默认值是 /usr/local/nginx ,因此也可以省略此选项或指定到 其他位置;—with-­http_ssl_module 选项用于设置在 Nginx 中允许使用 http_ssl_module模块的相关功能。

编译报错处理

缺少c和c编译环境 yum install gcc gcc-c
./configure检查安装环境

  1. checking for OS
  2. + Linux 3.10.0-693.el7.x86_64 x86_64
  3. checking for C compiler ... not found
  4. ./configure: error: C compiler cc is not found

gcc为GNU Compiler Collection的缩写,可以编译C和C源代码等,它是GNU开发的C和C以及其他很多种语言 的编译器(最早的时候只能编译C,后来很快进化成一个编译多种语言的集合,如Fortran、Pascal、Objective-C、Java、Ada、 Go等。)
  gcc 在编译C源代码的阶段,只能编译 C 源文件,而不能自动和 C++ 程序使用的库链接(编译过程分为编译、链接两个阶段,注意不要和可执行文件这个概念搞混,相对可执行文件来说有三个重要的概念:编译(compile)、链接(link)、加载(load)。源程序文件被编译成目标文件,多个目标文件连同库被链接成一个最终的可执行文件,可执行文件被加载到内存中运行)。因此,通常使用 g++ 命令来完成 C++ 程序的编译和连接,该程序会自动调用 gcc 实现编译。
缺少PCRE库 yum install pcre pcre-devel

  1. ./configure: error: the HTTP rewrite module requires the PCRE library.
  2. You can either disable the module by using --without-http_rewrite_module
  3. option, or install the PCRE library into the system, or build the PCRE library
  4. statically from the source with nginx by using --with-pcre=<path> option.

pcre pcre-devel:在Nginx编译需要 PCRE(Perl Compatible Regular Expression),因为Nginx 的Rewrite模块和HTTP 核心模块会使用到PCRE正则表达式语法。
缺少zip 压缩库

  1. ./configure: error: the HTTP gzip module requires the zlib library.
  2. You can either disable the module by using --without-http_gzip_module
  3. option, or install the zlib library into the system, or build the zlib library
  4. statically from the source with nginx by using --with-zlib=<path> option.

zlip zlib-devel:nginx启用压缩功能的时候,需要此模块的支持

CentOS

vim /etc/yum.repos.d/nginx.repo

  1. [nginx-stable]
  2. name=nginx stable repo
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  4. gpgcheck=1
  5. enabled=1
  6. gpgkey=https://nginx.org/keys/nginx_signing.key
  7. [nginx-mainline]
  8. name=nginx mainline repo
  9. baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
  10. gpgcheck=1
  11. enabled=0
  12. gpgkey=https://nginx.org/keys/nginx_signing.key

yum安装nginx

  1. yum install -y nginx

分别设置启动Nginx和开机自启动

  1. systemctl start nginx.service
  2. systemctl enable nginx.service

Ubuntu

  1. $ sudo apt-get update
  2. $ sudo apt-get install -y nginx

启动

  1. $ sudo service nginx start [23:04:36]
  2. * Starting nginx nginx

MacOS

  1. $ brew install nginx
  2. ....
  3. To have launchd start nginx now and restart at login:
  4. brew services start nginx
  5. Or, if you don't want/need a background service you can just run:
  6. nginx

Docker

方式一:通过docker后台启动nginx,宿主机端口是80

  1. $ docker run -d --name nginx -p 80:80 nginx

方式二:通过docke-compose搭建一个开发调试

  1. version: "3.7"
  2. services:
  3. web:
  4. image: nginx
  5. container_name: nginx
  6. ports:
  7. - "80:80"
  8. volumes:
  9. - ./nginx.conf:/etc/nginx/nginx.conf
  10. command: [nginx-debug, '-g', 'daemon off;']

启动docke-compose

  1. $ docker-compose up
  2. Starting nginx ... done
  3. Attaching to nginx

Nginx组成

二进制可执行文件 由各模块源码编译出的一个文件
Nginx.conf 配置文件控制Nginx的行为
access.log 访问日志 记录每一条http请求信息
error.log 错误日志

Nginx执行文件目录结构

  1. tree
  2. .
  3. ├── conf
  4. ├── fastcgi.conf
  5. ├── fastcgi.conf.default
  6. ├── fastcgi_params
  7. ├── fastcgi_params.default
  8. ├── koi-utf
  9. ├── koi-win
  10. ├── mime.types
  11. ├── mime.types.default
  12. ├── nginx.conf
  13. ├── nginx.conf.default
  14. ├── scgi_params
  15. ├── scgi_params.default
  16. ├── uwsgi_params
  17. ├── uwsgi_params.default
  18. └── win-utf
  19. ├── html
  20. ├── 50x.html
  21. └── index.html
  22. ├── logs
  23. └── sbin
  24. └── nginx
  25. 4 directories, 18 files

conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。
html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

Nginx 命令

启动Nginx

  1. ./usr/local/nginx/sbin/nginx

立即停止Nginx

  1. ./usr/local/nginx/sbin/nginx -s stop

优雅的停止服务 quit,是在完成当前工作任务后再停止 。

  1. ./usr/local/nginx/sbin/nginx -s quit

平滑重启,在 Nginx 已经启动的情况下重新加载配置文件

  1. nginx -s reload

检查和重新启动配置

  1. nginx -t -c /etc/nginx/nginx.conf

检测特定目录下的 显示版本信息 显示版本信息和编译选项

  1. [root@aliyun sbin]# ./nginx -V
  2. nginx version: nginx/1.14.2
  3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
  4. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

使用指定的配置文件 -c
指定配置指令 -g
指定运行目录 -p
发送信号 -s
测试配置文件是否有语法错误 - t
查看当前端口占用

  1. # netstat -tlnp

开机启动Nginx服务

vim /lib/systemd/system/nginx.service

  1. [Unit]
  2. Description=nginx service
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. ExecStart=/usr/local/nginx/sbin/nginx
  7. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  8. ExecStop=/usr/local/nginx/sbin/nginx -s quit
  9. PrivateTmp=true
  10. [Install]
  11. WantedBy=multi-user.target

增加开启启动Nginx

  1. # systemctl enable nginx.service
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.