环境准备
安装前先要确定Linux的内核版本要在2.6以上,只有2.6之后才支持epool ,在此之前使用select或pool多路复用的IO模型,无法解决高并发压力的问题。通过命令uname -a 即可查看:
另外Nginx需要GCC编译器,用来编译C语言环境,Nginx不会提供二进制可执行程序,只能下载源码进行编译;
yum install gcc gcc-c
另外还需要PCRE库,是专门用来做正则表达式的,Nginx里面很多地方都用到了正则表达式,因此需要进行安装;
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 命令统一安装:
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)。
“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版本下载地址。
源代码安装-离线安装
下载源代码
#wget http://nginx.org/download/nginx-1.14.2.tar.gz
#tar -zxvf nginx-1.14.2.tar.gz
源码文件目录结构
nginx-1.14.2 baxiang$ tree -d
.
├── auto
│ ├── cc
│ ├── lib
│ │ ├── geoip
│ │ ├── google-perftools
│ │ ├── libatomic
│ │ ├── libgd
│ │ ├── libxslt
│ │ ├── openssl
│ │ ├── pcre
│ │ ├── perl
│ │ └── zlib
│ ├── os
│ └── types
├── conf
├── contrib
│ ├── unicode2nginx
│ └── vim
│ ├── ftdetect
│ ├── ftplugin
│ ├── indent
│ └── syntax
├── html
├── man
└── src
├── core
├── event
│ └── modules
├── http
│ ├── modules
│ │ └── perl
│ └── v2
├── misc
├── os
│ └── unix
└── stream
37 directories
src: 源代码
man: 帮助文档
html:默认首页网站文件
contrib 其他结构或者组织共享的文档资料
conf:配置文件
configure 自动安装脚本,用于检查安装环境
auto:脚本文件 和configure程序相关
依赖库如下:
yum install gcc gcc-c++ automake pcre-devel zlib-devel openssl-devel
pcre-devel :提供正则表达式
zlib-devel:提供压缩
openssl-devel 提供证书以及ssl协议
# cd nginx-1.14.2/
#./configure --prefix=/usr/local/nginx --with-http_ssl_module
#make
#make install
#cd /usr/local/nginx/sbin
#./nginx
# 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检查安装环境
checking for OS
+ Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found
./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
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
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 压缩库
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
zlip zlib-devel:nginx启用压缩功能的时候,需要此模块的支持
CentOS
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
yum安装nginx
yum install -y nginx
分别设置启动Nginx和开机自启动
systemctl start nginx.service
systemctl enable nginx.service
Ubuntu
$ sudo apt-get update
$ sudo apt-get install -y nginx
启动
$ sudo service nginx start [23:04:36]
* Starting nginx nginx
MacOS
$ brew install nginx
....
To have launchd start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
nginx
Docker
方式一:通过docker后台启动nginx,宿主机端口是80
$ docker run -d --name nginx -p 80:80 nginx
方式二:通过docke-compose搭建一个开发调试
version: "3.7"
services:
web:
image: nginx
container_name: nginx
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
command: [nginx-debug, '-g', 'daemon off;']
启动docke-compose
$ docker-compose up
Starting nginx ... done
Attaching to nginx
Nginx组成
二进制可执行文件 由各模块源码编译出的一个文件
Nginx.conf 配置文件控制Nginx的行为
access.log 访问日志 记录每一条http请求信息
error.log 错误日志
Nginx执行文件目录结构
tree
.
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
└── sbin
└── nginx
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
./usr/local/nginx/sbin/nginx
立即停止Nginx
./usr/local/nginx/sbin/nginx -s stop
优雅的停止服务 quit,是在完成当前工作任务后再停止 。
./usr/local/nginx/sbin/nginx -s quit
平滑重启,在 Nginx 已经启动的情况下重新加载配置文件
nginx -s reload
检查和重新启动配置
nginx -t -c /etc/nginx/nginx.conf
检测特定目录下的 显示版本信息 显示版本信息和编译选项
[root@aliyun sbin]# ./nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
使用指定的配置文件 -c
指定配置指令 -g
指定运行目录 -p
发送信号 -s
测试配置文件是否有语法错误 - t
查看当前端口占用
# netstat -tlnp
开机启动Nginx服务
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
增加开启启动Nginx
# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.