0
点赞
收藏
分享

微信扫一扫

基础服务篇之Nginx搭建与平滑升级(一)

A邱凌 2022-04-24 阅读 71
nginx

文章目录


Nginx的安装部署

以下有yum源安装和二进制包两种部署方式安装

1.yum源安装

  1. 阿里yum源链接:https://developer.aliyun.com/mirror/epel?spm=a2c6h.13651102.0.0.3e221b1139q8E9

选择自己的系统版本根据其提示增加源添加完成后直接执行命令

yum install -y nginx
  1. nginx官方源:https://nginx.org/en/linux_packages.html
    选择相应的系统版本
    创建一个yum源仓库文件 /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
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

配置好仓库后直接执行以下安装命令

yum install -y nginx

2.二进制编译安装

  1. 下载nginx二进制安装包
[root@node01 ~]# cd /usr/src/
[root@node01 src]# wget https://nginx.org/download/nginx-1.18.0.tar.gz
--2022-04-20 10:16:51--  https://nginx.org/download/nginx-1.18.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 2a05:d014:edb:5704::6, 2a05:d014:edb:5702::6
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1039530 (1015K) [application/octet-stream]
Saving to: ‘nginx-1.18.0.tar.gz’

100%[=======================================================================================================>] 1,039,530    683KB/s   in 1.5s   

2022-04-20 10:16:53 (683 KB/s) - ‘nginx-1.18.0.tar.gz’ saved [1039530/1039530]

  1. 解压
[root@node01 src]# ll
total 1016
drwxr-xr-x. 2 root root       6 Nov  5  2016 debug
drwxr-xr-x. 3 root root      35 Dec 18 07:37 kernels
-rw-r--r--  1 root root 1039530 Apr 21  2020 nginx-1.18.0.tar.gz
[root@node01 src]# tar -xf nginx-1.18.0.tar.gz 
[root@node01 src]# ll
total 1016
drwxr-xr-x. 2 root root       6 Nov  5  2016 debug
drwxr-xr-x. 3 root root      35 Dec 18 07:37 kernels
drwxr-xr-x  8 1001 1001     158 Apr 21  2020 nginx-1.18.0
-rw-r--r--  1 root root 1039530 Apr 21  2020 nginx-1.18.0.tar.gz
[root@node01 src]#
  1. 进入解压后的目录进行预编译
[root@node01 src]# cd nginx-1.18.0/
[root@node01 nginx-1.18.0]# ll
total 760
drwxr-xr-x 6 1001 1001    326 Apr 20 10:19 auto
-rw-r--r-- 1 1001 1001 302863 Apr 21  2020 CHANGES
-rw-r--r-- 1 1001 1001 462213 Apr 21  2020 CHANGES.ru
drwxr-xr-x 2 1001 1001    168 Apr 20 10:19 conf
-rwxr-xr-x 1 1001 1001   2502 Apr 21  2020 configure
drwxr-xr-x 4 1001 1001     72 Apr 20 10:19 contrib
drwxr-xr-x 2 1001 1001     40 Apr 20 10:19 html
-rw-r--r-- 1 1001 1001   1397 Apr 21  2020 LICENSE
drwxr-xr-x 2 1001 1001     21 Apr 20 10:19 man
-rw-r--r-- 1 1001 1001     49 Apr 21  2020 README
drwxr-xr-x 9 1001 1001     91 Apr 20 10:19 src
[root@node01 nginx-1.18.0]#

  1. 查看预编译帮助根据自己需求编译nginx
[root@node01 nginx-1.18.0]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix #安装路径
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes
 ...
 ...(中间编译模块自己可以慢慢看)

--with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging
[root@node01 nginx-1.18.0]# useradd nginx
[root@node01 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
checking for OS
 1. Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 2. using GNU C compiler
 3. gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
checking for gcc -pipe switch ... found
...
./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.##这里提示有错误,rewrite模块需要PCRE lib库的支持,所以需要安装依赖
  1. 安装以上所缺 PCRE依赖包解决报错,继续编译
[root@node01 nginx-1.18.0]# yum install -y pcre-devel.x86_64

[root@node01 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
checking for gcc -pipe switch ... found
...
checking for zlib library ... not found

./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.##同理gzip模块缺少zlib依赖,安装即可
  1. 安装zlib依赖继续编译
[root@node01 nginx-1.18.0]# yum install -y zlib-devel.x86_64

[root@node01 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
...
creating objs/Makefile

Configuration summary
 1. using system PCRE library
 2. OpenSSL library is not used
 3. using system zlib library

  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"
###此时预编译无报错
  1. 开始编译
[root@node01 nginx-1.18.0]# 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@node01 nginx-1.18.0]# make && make install
...
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
	|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
	|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
	|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
	|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/usr/src/nginx-1.18.0'##编译完成
  1. 添加环境变量以便服务的启动
[root@node01 nginx-1.18.0]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@node01 nginx-1.18.0]# export PATH=$PATH:/usr/local/nginx/sbin
[root@node01 nginx-1.18.0]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin
[root@node01 nginx-1.18.0]# echo “export PATH=$PATH:/usr/local/nginx/sbin” >> /etc/profile    ##写入etc文件下,重启后环境变量依旧可以加载
  1. 启动nginx服务
[root@node01 nginx-1.18.0]# nginx
[root@node01 nginx-1.18.0]# ps -ef | grep nginx
root      20688      1  0 11:07 ?        00:00:00 nginx: master process nginx
nginx     20689  20688  0 11:07 ?        00:00:00 nginx: worker process
root      20691   1462  0 11:07 pts/0    00:00:00 grep --color=auto nginx
[root@node01 nginx-1.18.0]# 

在这里插入图片描述

3.Nginx版本更新

  1. 查看当前版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.18.0
  1. 下载1.20.2版本升级
[root@localhost src]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@localhost src]# tar -xzvf nginx-1.20.2.tar.gz
[root@localhost src]# ll
total 2056
drwxr-xr-x. 2 root root       6 Apr 11  2018 debug
drwxr-xr-x. 3 root root      35 Nov 25 23:05 kernels
drwxr-xr-x  9 1001 1001     186 Apr 24 20:10 nginx-1.18.0
-rw-r--r--  1 root root 1039530 Apr 21  2020 nginx-1.18.0.tar.gz
drwxr-xr-x  9 1001 1001     186 Apr 20 16:51 nginx-1.20.2
-rw-r--r--  1 root root 1062124 Nov 16 22:51 nginx-1.20.2.tar.gz
[root@localhost src]# cd nginx-1.20.2/
  1. 查看老版本编译参数,查看进程PID后重新编译新版本
[root@localhost nginx-1.20.2]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx 
[root@localhost nginx-1.20.2]# ps -ef | grep nginx
root      15117      1  0 22:19 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     15118  15117  0 22:19 ?        00:00:00 nginx: worker process
root      15120   7258  0 22:19 pts/0    00:00:00 grep --color=auto nginx
[root@localhost nginx-1.20.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module##重新编译时不能缺少旧版本的模块以防nginx服务出现问题(可以增加编译模块)
[root@localhost nginx-1.20.2]# make && make install##开始编译
  1. 更新版本
[root@localhost nginx-1.20.2]# cat Makefile 

default:	build

clean:
	rm -rf Makefile objs

.PHONY:	default clean

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`
	####这里我们先看下upgrade下面的命令,这是更新时执行的几条命令,因此我们也可以手动敲命令升级更新。这里我们直接使用make upgrade升级
[root@localhost nginx-1.20.2]# make upgrade
/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
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`
  1. 查看nginx进程pid
[root@localhost nginx-1.20.2]# ps -ef | grep nginx
root      17712      1  0 22:21 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     17714  17712  0 22:21 ?        00:00:00 nginx: worker process
root      17720   7258  0 22:23 pts/0    00:00:00 grep --color=auto nginx

与之前相比pid变了,证明平滑升级成功!

备注:如果在执行make upgrade命令出现以下类似报错,则检查你的nginx启动是否为绝对路径启动,我的路径为/usr/local/nginx/sbin/nginx ,不能以环境变量等方式启动nginx,否则需要重启nginx服务才能升级成功。

举报

相关推荐

0 条评论