Nginx yum安装和源码安装

Nginx简介


Nginx是一款开源的、免费的WEB服务器软件,2019.3.12被F5硬件负载均衡厂家以6.7亿美金收购,Nginx主要是用于发布网站代码、提供网页信息服务,用户通过浏览器可以实现页面的访问。

Nginx WEB软件默认只能处理静态网页,不能直接处理动态网页,动态网页交于第三方的程序去解析,Nginx官方宣称其处理静态网页的并发可以达到5w/s,相当于Apache WEB整体性能的5-10倍。

nginx 的发展:

第一个公开版本0.1.0发布于2004年10月4日。

Nginx 的1.4.0稳定版已经于2013年4月24日。

Nginx目前最新的版本是1.16.1,于 2019年8月13号发布。

来到Blog, case studies and resources | Netcraft

可以看到nginx是有多受欢迎

nginx的安装方式


YUM安装部署

源码安装部署

yum安装nginx


(1)首先centos必须配置yum仓库才能下载相应的软件包进行安装,开到nginx的官方网站nginx news再来到

http://nginx.org/en/linux_packages.html#RHEL-CentOS

将红色方框的部分配置到.repo文件里面

[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo --这样yum源就配置好了(nginx-stable enable为0代表yum安装nginx稳定的1.16版本。如果要安装最新版本1.17将nginx-mainline enable改为1)

[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

module_hotfixes=true

[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

module_hotfixes=true

[root@localhost ~]# yum makecache  --生成一下缓存

[root@localhost ~]# yum -C list nginx  --从生成的缓存里面查找nginx包,可以看到到包是稳定版提供的

Available Packages

nginx.x86_64  1:1.16.1-1.el7.ngx   nginx-stable

[root@localhost ~]# yum-config-manager --enable nginx-mainline  --如果要下载nginx的最新版本需要将nginx-mainline开放,那么就不会下载稳定版的nginx了。

Mainline version:主线版本:也叫开发版本,目前最新但是还没有经过大量测试的版本

Stable version:稳定版通常是经过大量测试的,相对比较稳定的版本,企业中我们也会使用稳定版。

Legacy versions:通常是往期的稳定版本。

[root@localhost ~]# yum install nginx -y  --安装稳定版本

[root@localhost ~]# rpm -q nginx

nginx-1.16.1-1.el7.ngx.x86_64

[root@localhost ~]# nginx -v --至此,yum安装nginx就完成了

nginx version: nginx/1.16.1

yum部署方式相对比较简单、快捷、高效,可以自动校验软件包的正确性,可以自动解决软件包之间的依赖关系,可以自动安装软件服务并且设置为系统服务,不能自定义软件服务特定功能和模块,安装之后的文件和目录相对比较分散,不便于后期的维护和管理。

yum安装nginx有个大问题,nginx的二进制文件是会把模块编译进来的,nginx的每个模块并不是默认都会开启的,如果你想添加第三方的nginx模块,那么必须通过编译nginx这种方式,才能将第三方生态圈里面的功能添加到nginx中。所以就要使用接下来的源码安装。

源码安装nginx


Make源码部署方式相对比较复杂、繁琐、低效,不能自动校验软件包的正确性,不能自动解决软件包之间的依赖关系,不能自动安装软件服务并且设置为系统服务,可以自定义软件服务特定功能和模块,安装之后的文件和目录相对比较统一,便于后期的维护和管理。

yum源下载所需的依赖包

yum -y install gcc make pcre-devel openssl-devel

useradd -s /sbin/nologin nginx

useradd -s /sbin/mologin nginx

用源码方式安装软件,通常是分为以下三步:

(1)./configure:预编译

用来在安装前对系统进行检查,确认系统是否具备了编译软件所需的各种条件(相当于rpm 的test)。以C语言为例,configure脚本会确认系统是否有编译软件所需的编译器,以及所需的各种头文件和库等产生makefile文件,同时可以看到./configure执行后产生了objs这个目录以及.c文件

[root@www nginx-1.16.1]# cat Makefile 

default:    build

clean:
    rm -rf Makefile objs

build:
    $(MAKE) -f objs/Makefile

install:
    $(MAKE) -f objs/Makefile install

modules:
    $(MAKE) -f objs/Makefile modules

upgrade:
    /usr/local/nginx//sbin/nginx -t

    kill -USR2 `cat /usr/local/nginx//logs/nginx.pid`
    sleep 1
    test -f /usr/local/nginx//logs/nginx.pid.oldbin

    kill -QUIT `cat /usr/local/nginx//logs/nginx.pid.oldbin`

[root@www nginx-1.16.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  letsencrypt  LICENSE  Makefile  man  objs  README  src
[root@www nginx-1.16.1]# ls objs/   --此时预编译src目录下面是没有文件的
autoconf.err  Makefile  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  src

[root@www objs]# pwd
/usr/src/nginx-1.16.1/objs
[root@www objs]# tree src
src
├── core
├── event
│   └── modules
├── http
│   ├── modules
│   │   └── perl
│   └── v2
├── mail
├── misc
├── os
│   ├── unix
│   └── win32
└── stream

(2)make:编译

不加参数的make的作用就是从当前目录下的Makefile文件中读取指令,对源代码进行编译。在每次使用make时,都会重新生成objects,新产生的object会覆盖旧的objects。同时产生了新的nginx二进制文件

[root@www nginx-1.16.1]# tree objs/src/
objs/src/
├── core
│   ├── nginx.o
│   ├── ngx_array.o
│   ├── ngx_buf.o
│   ├── ngx_conf_file.o
│   ├── ngx_connection.o
│   ├── ngx_cpuinfo.o
│   ├── ngx_crc32.o
│   ├── ngx_crypt.o
│   ├── ngx_cycle.o
│   ├── ngx_file.o
│   ├── ngx_hash.o
│   ├── ngx_inet.o

上面这些文件叫做.o文件,c语言都会先生成目标文件,最后生成可执行文件,由这些.o文件最后编译出来了nginx二进制文件

如下标红所示

[root@www nginx-1.16.1]# ls objs/
autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src

[root@www nginx-1.16.1]# file  objs/nginx
objs/nginx: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=a58f8f59b977239a65fb4376531b511fe8d15286, not stripped

(3)make install :安装,如果原始代码编译无误,且执行结果正确,便可以把程序安装至系统预设的可执行文件存放路径,最后可以运行make clean删除临时文件。(都是查看目录文件是否存在,是否需要拷贝)

[root@www nginx-1.16.1]# make install
make -f objs/Makefile install
make[1]: Entering directory `/usr/src/nginx-1.16.1'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
    || mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
    || mv '/usr/local/nginx/sbin/nginx' \
        '/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'

(4)卸载源码方式安装的软件,直接删除安装目录即可!

下载nginx源码包

[root@localhost ~]# cd /usr/src  --下载源码包先到src目录,这个地方一般是用来存放源码包的

[root@localhost src]# pwd

/usr/src

[root@localhost src]# yum -C info  nginx   --缓存里面查看nginx是由什么源提供的

Name        : nginx

Arch        : x86_64

Epoch       : 1

Version     : 1.16.1

Release     : 1.el7.ngx

Size        : 766 k

Repo        : nginx-stable/7/x86_64

Summary     : High performance web server

URL         : http://nginx.org/

License     : 2-clause BSD-like license

Description : nginx [engine x] is an HTTP and reverse proxy server, as well as

            : a mail proxy server.

来到downloader页面nginx: download下载包,选择想要下载的版本,直接单击右键复制地址下载。

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

[root@localhost src]# ls

debug  kernels  nginx-1.16.1.tar.gz

[root@localhost src]# tar -zxvf nginx-1.16.1.tar.gz --解压源码包

[root@localhost src]# ls

debug  kernels  nginx-1.16.1  nginx-1.16.1.tar.gz

[root@localhost src]# cd nginx-1.16.1

[root@localhost nginx-1.16.1]# ls  --可以看到这些就是nginx的源码文件

auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

源码包各个目录的作用

[root@localhost nginx-1.16.1]# cd auto/  --auto目录里存放着一些工具包

[root@localhost auto]# ls

cc          feature       headers  install  module   options  stubs    types

define      have          include  lib      modules  os       summary  unix

endianness  have_headers  init     make     nohave   sources  threads

[root@localhost nginx-1.16.1]# more CHANGES  --描述了nginx修复的一些bug

    *) Bugfix: a segmentation fault might occur in a worker process if

       variables were used in the "ssl_certificate" or "ssl_certificate_key"

       directives and OCSP stapling was enabled.

       Changes with nginx 1.15.11                                       09 Apr 2019

[root@localhost ~]# cat /usr/src/nginx-1.16.0/objs/ngx_modules.c | grep stub_status  --objs目录下的ngx_modules.c 是来查看已经编译到Nginx二进制文件里面的模块
extern ngx_module_t  ngx_http_stub_status_module;
    &ngx_http_stub_status_module,
    "ngx_http_stub_status_module",

[root@localhost nginx-1.16.1]# ls conf  --存放了nginx的配置文件

fastcgi.conf(整合php)    koi-utf  mime.types  scgi_params   win-utf

fastcgi_params  koi-win  nginx.conf  uwsgi_params(整合python)

[root@localhost nginx-1.16.1]# ll configure  --预编译工具,是个脚本,shell脚本,来检查当前环境能否正常装nginx

-rwxr-xr-x 1 1001 1001 2502 Aug 13  2019 configure

contrib目录:能够提供Nginx语法高亮的问题

[root@www nginx-1.16.1]# cp -r contrib/vim/* /usr/share/vim//vimfiles/  --如果在vim编辑nginx的配置文件想让其语法高亮可以使用上面命令

html是发布目录:你能够访问到welcome to nginx页面也是该目录index.html提供的

[root@localhost nginx-1.16.1]# ls src/  --src目录里面包含了各个应用,基础应用,核心应用和第三方应用。每个应用又是由许多的模块构成

core(核心应用)  (event  http  mail  misc  os  stream基础应用)

Nginx一般安装在/usr/local或者/opt下面

预编译

预编译主要是检测Linux系统安装该软件所需的依赖环境、库文件,检测Linux系统是否存在GCC编译环境(C编译器),指定软件服务部署的路径,自定义软件服务特定的功能、模块,最终会产生Makefile文件。

./configure -h 可以查看预编译参数

--prefix 指定nginx编译安装的目录(一般只需要指定安装路径即可,如果指定太多路径,源码卸载会十分麻烦,文件存储在各个位置)

--user=*** 指定nginx的属主

--group=*** 指定nginx的属主与属组

--with-*** 指定编译某模块

--without-** 指定不编译某模块

--add-module 编译第三方模块,可以看到反向代理模块,rewrite模块和防盗链模块已经被编译进去了

 --without-http_referer_module      disable ngx_http_referer_module
 --without-http_rewrite_module      disable ngx_http_rewrite_module
 --without-http_proxy_module        disable ngx_http_proxy_module

[root@localhost modules]# cd /usr/src/nginx-1.16.0/src/http/modules/  --模块存放的位置
[root@localhost modules]# vim ngx_http_gzip_filter_module.c 

[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx

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

[root@localhost nginx-1.16.1]# yum install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y  --将依赖的包全部安装好

[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --再次重新预编译一下,可以看到所有nginx配置的特性,以及运行时候的目录都会列在最下方

creating objs/Makefile

  nginx path prefix: "/usr/local/nginx"

  nginx binary file: "/usr/local/nginx/sbin/nginx"

  nginx modules path: "/usr/local/nginx/modules"

  nginx configuration prefix: "/usr/local/nginx/conf"

  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

  nginx pid file: "/usr/local/nginx/logs/nginx.pid"

  nginx error log file: "/usr/local/nginx/logs/error.log"

  nginx http access log file: "/usr/local/nginx/logs/access.log"

  nginx http client request body temporary files: "client_body_temp"

  nginx http proxy temporary files: "proxy_temp"

  nginx http fastcgi temporary files: "fastcgi_temp"

  nginx http uwsgi temporary files: "uwsgi_temp"

  nginx http scgi temporary files: "scgi_temp"

[root@localhost nginx-1.16.1]# ls  --可以看到预编译生成了Makefile和objs目录

auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src

[root@www nginx-1.16.1]# cd objs/  --这里最重要的是会生成ngx_modules.c,它决定了执行编译的时候有哪些模块会被编译进nginx

[root@www objs]# ls

autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src

Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o

[root@www objs]# head -10 ngx_modules.c  --可以看到有哪些模块被编译进去了

extern ngx_module_t  ngx_core_module;

extern ngx_module_t  ngx_errlog_module;

extern ngx_module_t  ngx_conf_module;

extern ngx_module_t  ngx_regex_module;

[root@localhost nginx-1.16.1]# make clean  --如果发现预编译自己少加了一些东西,可以使用make clean删除预编译的文件,重新编译

rm -rf Makefile objs

[root@localhost nginx-1.16.1]# ls

auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

[root@localhost nginx-1.16.1]# cat Makefile --make clean是在Makefile文件里面定义了clean参数

default: build  make的默认参数

clean:

rm -rf Makefile objs

build:通过makefile文件构造包

$(MAKE) -f objs/Makefile

install:根据makefile文件进行安装

$(MAKE) -f objs/Makefile install

modules:

$(MAKE) -f objs/Makefile modules

upgrade:热部署升级

/usr/local/nginx/sbin/nginx -t

kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

sleep 1

test -f /usr/local/nginx/logs/nginx.pid.oldbin

kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

编译

主要是通过make编译工具,读取Makefile文件,调用Linux操作系统下GCC编译环境(C编译器),将软件包中的源代码文件编译生成二进制文件。Makefile文件用途,告知make编译工具在编译源代码文件时,从哪个源码文件开始编译至哪个源码文件结束编译。

[root@localhost nginx-1.16.1]# make build

[root@www nginx-1.16.1]# ls objs/  --可以看到生成了nginx二进制文件,假如你需要做版本升级,还需要将这个文件拷贝到安装目录当中

autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src

Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o

安装,make install

主要是将第二步make编译产生的二进制文件,拷贝或者安装至Linux操作系统指定的安装目录:--prefix=/usr/local/nginx/

测试以及nginx基本命令

[root@localhost objs]# cd /usr/local/nginx/  --来到nginx的安装目录下面

[root@localhost nginx]# ls

conf(配置文件)  html(发布目录)  logs(存放nginx日志)  sbin(存放二进制文件)

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -V  --查看版本信息

nginx version: nginx/1.16.1

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)

configure arguments: --prefix=/usr/local/nginx

[root@localhost sbin]# /usr/local/nginx/sbin/nginx  --启动nginx

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

这里显示端口被占用了,可能是apache占用端口,或者是nginx已经就启动了

[root@localhost sbin]# netstat -tpln | grep 80  --可以看到nginx被占用了端口

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10243/nginx: master  

停止Nginx的三种方式

/usr/local/nginx/sbin/nginx -s stop   --立即停止Nginx服务

/usr/local/nginx/sbin/nginx -s quit  --完成当前任务后停止

pkill nginx  --杀死Nginx进程

使用quite来关闭nginx:关闭监听句柄,要保证worker进程不会再去处理新的连接了。然后接着去看连接池,nginx为了保证资源是最大化的,经常会保存资源但是没有断开,这个时候就会关闭空闲连接,当处理完所有请求之后退出worker进程,还有一种情况当达到定时器时间时候会强制退出,即使还有请求没有处理完。

[root@localhost sbin]# ps -ef | grep nginx  --nginx启动起来了两个进程,一个是master进程,一个是worker进程。master进程来管理worker进程的,用户请求由worker进程来处理

root      10255      1  0 16:37 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

nobody    10256  10255  0 16:37 ?        00:00:00 nginx: worker process

如果你用yum安装了nginx和源码安装了nginx都在一台主机上,你不知道起来的服务是yum启动的还是源码启动的可以使用ps -ef | grep nginx 查看命令路径或者查看用户。

[root@localhost objs]# cd /usr/local/nginx/  --来到nginx的安装目录下面

[root@localhost nginx]# ls

conf(配置文件)  html(发布目录)  logs(存放nginx日志)  sbin(存放二进制文件)

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -V  --查看版本信息

nginx version: nginx/1.16.1

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)

configure arguments: --prefix=/usr/local/nginx

[root@localhost sbin]# /usr/local/nginx/sbin/nginx  --启动nginx

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

这里显示端口被占用了,可能是apache占用端口,或者是nginx已经就启动了

[root@localhost sbin]# netstat -tpln | grep 80  --可以看到nginx被占用了端口

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10243/nginx: master  

[root@localhost sbin]# pkill nginx  --杀掉进程重启

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

[root@localhost sbin]# ps -ef | grep nginx  --nginx启动起来了两个进程,一个是master进程,一个是worker进程。master进程来管理worker进程的,用户请求由worker进程来处理

root      10255      1  0 16:37 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

nobody    10256  10255  0 16:37 ?        00:00:00 nginx: worker process

如果你用yum安装了nginx和源码安装了nginx都在一台主机上,你不知道起来的服务是yum启动的还是源码启动的可以使用ps -ef | grep nginx 查看命令路径或者查看用户。

在最后来访问一下,nginx WEB服务已经搭建成功!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值