Nginx的安装与平滑升级(详)

简介: Nginx的安装与平滑升级(详)

一、安装Nginx


1.下载Nginx安装包

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

这里下载两个版本的Nginx安装包是为了一会儿的升级使用。


Nginx下载


2.源码安装Nginx及相关组件

[root@localhost ~]# yum -y install gcc pcre-devel openssl-devel
[root@localhost ~]# useradd -s /sbin/nologin nginx   //创建禁止登陆解释器的用户(为了安全)
[root@localhost ~]# id nginx
uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)
[root@localhost ~]# tar -xf nginx-1.14.2.tar.gz
[root@localhost ~]# cd nginx-1.14.2
[root@localhost nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
         --prefix=/usr/local/nginx                   //指定安装路径
         --user=nginx                               //指定用户
         --group=nginx                              //指定组
         --with-http_ssl_module                    //安装ssl模块,开启其中的SSL加密功能(需要什么模块就安装什么模块)
  ......
  ......
  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.14.2]# make && make install    //编译并且安装
......
    '/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
    || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
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]: 离开目录“/root/nginx-1.14.2”

3.启动Nginx

[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx 
[root@localhost nginx-1.14.2]# netstat -antulp | grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6079/nginx: master
或者[root@localhost nginx-1.14.2]# netstat -antulp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6079/nginx: master
[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx -V   //查看Nginx软件信息
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (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

4.访问Nginx初始页面

30.png

5.Nginx的基本操作命令


/usr/local/nginx/sbin/nginx            //启动服务
/usr/local/nginx/sbin/nginx -s stop    //停止服务
/usr/local/nginx/sbin/nginx -t         //验证配置文件是否有错误
/usr/local/nginx/sbin/nginx -s reload  //重新加载配置文件(平滑重启,使用前最好先-t检查一下配置文件正确性)
/usr/local/nginx/sbin/nginx -V         //查看Nginx软件信息

二、平滑升级Nginx


1.查看Nginx版本

[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (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

2.编译新版本Nginx软件

[root@localhost ~]# tar -xf nginx-1.16.1.tar.gz 
[root@localhost ~]# cd nginx-1.16.1
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
......
[root@localhost nginx-1.16.1]# make

注意:这里千万不要make install


3.备份老版Nginx程序,并使用新版本替换

[root@localhost nginx-1.16.1]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginxold
[root@localhost nginx-1.16.1]# cp objs/nginx /usr/local/nginx/sbin/
[root@localhost nginx-1.16.1]# /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

4.发送USR2信号

//向主进程(master)发送USR2信号,Nginx会启动一个新版本的master进程和对应工作进程,和旧版一起处理请求
[root@localhost nginx-1.16.1]# ps -aux | grep nginx
root       6088  0.0  0.1  46080  1928 ?        Ss   14:57   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      6091  0.0  0.1  46516  2012 ?        S    14:57   0:00 nginx: worker process
root       8693  0.0  0.0 112728   964 pts/0    R+   15:28   0:00 grep --color=auto nginx
[root@localhost nginx-1.16.1]# kill -USR2 6088    //发送USR2信号
[root@localhost nginx-1.16.1]# ps -aux | grep nginx
root       6088  0.0  0.1  46080  1928 ?        Ss   14:57   0:00   nginx: master process /usr/local/nginx/sbin/nginx
 nginx      6091  0.0  0.1  46516  2012 ?        S    14:57   0:00 nginx: worker process
root       8694  0.5  0.1  45960  3260 ?        S    15:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx
 nginx      8695  0.0  0.1  46420  1888 ?        S    15:29   0:00 nginx: worker process
 root       8697  0.0  0.0 112728   968 pts/0    S+   15:29   0:00 grep --color=auto nginx

5.发送WINCH信号

//向旧的Nginx主进程(master)发送WINCH信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理
[root@localhost nginx-1.16.1]# ps -aux | grep nginx
root       6088  0.0  0.1  46080  1928 ?        Ss   14:57   0:00 nginx: master process /usr/local/nginx/sbin/nginx
 nginx      6091  0.0  0.1  46516  2012 ?        S    14:57   0:00 nginx: worker process
root       8694  0.0  0.1  45960  3260 ?        S    15:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx
 nginx      8695  0.0  0.1  46420  1888 ?        S    15:29   0:00 nginx: worker process
 root       8700  0.0  0.0 112728   968 pts/0    R+   15:36   0:00 grep --color=auto nginx
[root@localhost nginx-1.16.1]# kill -WINCH 6088
[root@localhost nginx-1.16.1]# ps -aux | grep nginx
root       6088  0.0  0.1  46080  1928 ?        Ss   14:57   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root       8694  0.0  0.1  45960  3260 ?        S    15:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx
 nginx      8695  0.0  0.1  46420  1888 ?        S    15:29   0:00 nginx: worker process
 root       8702  0.0  0.0 112728   968 pts/0    R+   15:37   0:00 grep --color=auto nginx

注意:如果这个时候需要回退继续使用之前的Nginx版本,可以向旧版本的Nginx主程序发送HUP信号,它会重新启动工作进程, 仍使用旧版配置文件。然后可以将新版Nginx进程杀死(使用QUIT、TERM、或者KILL)

例如:
[root@localhost nginx-1.16.1]# kill -HUP 6088

6.发送QUIT信号

升级完毕,可向旧的Nginx主进程(master)发送(QUIT、TERM、或者KILL)信号,使旧的主进程退出
[root@localhost nginx-1.16.1]# kill -QUIT 6088
[root@localhost nginx-1.16.1]# ps -aux | grep nginx
root       8694  0.0  0.1  45960  3260 ?        S    15:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      8695  0.0  0.1  46420  1888 ?        S    15:29   0:00 nginx: worker process
root       8708  0.0  0.0 112728   968 pts/0    R+   15:38   0:00 grep --color=auto nginx

7.验证升级后的Nginx版本并访问

[root@localhost nginx-1.16.1]# /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) 
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_

31.png

Nginx信号补充:

QUIT          平滑关闭
TERM、INT     快速关闭
HUP           平滑重启,重新加载配置文件
USR1          重新打开日志文件
USR2          平滑升级可执行程序
WINCH         平滑关闭工作进程


相关文章
|
16天前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
1月前
|
应用服务中间件 Linux 网络安全
2022年超详细在CentOS 7上安装Nginx方法(源码安装)
这篇文章提供了在CentOS 7系统上通过源码安装Nginx的详细步骤,包括从官网下载Nginx源码包、上传至虚拟机、解压、删除压缩包、编译安装前的配置、安装PCRE库(因为Nginx使用PCRE库解析正则表达式)、安装zlib和OpenSSL库(用于支持HTTPS协议)、重新编译Nginx、安装后启动Nginx服务、关闭服务、修改默认端口、以及重启服务测试等步骤。文章还提供了相关命令和操作截图,帮助用户更好地理解和执行安装过程。
2022年超详细在CentOS 7上安装Nginx方法(源码安装)
|
1月前
|
应用服务中间件 PHP nginx
Mac安装Nginx
Mac安装Nginx
22 2
Mac安装Nginx
|
22天前
|
缓存 应用服务中间件 nginx
安装nginx-http-flv-module模块
本文介绍如何为Nginx安装`nginx-http-flv-module`模块。此模块基于`nginx-rtmp-module`二次开发,不仅具备原模块的所有功能,还支持HTTP-FLV播放、GOP缓存、虚拟主机等功能。安装步骤包括:确认Nginx版本、下载相应版本的Nginx与模块源码、重新编译Nginx并加入新模块、验证模块安装成功。特别注意,此模块已包含`nginx-rtmp-module`功能,无需重复编译安装。
63 1
|
2月前
|
应用服务中间件 Linux 网络安全
|
1月前
|
应用服务中间件 Linux nginx
Linux虚拟机磁盘扩容、Docker容器磁盘满的问题、Docker安装nginx
这篇文章讨论了Linux虚拟机磁盘扩容的方法,包括外部配置、具体扩容步骤和扩容后的效果验证。同时,文章还涉及了Docker容器磁盘满的问题及其解决方法,如删除不必要的镜像和容器,以及调整Docker的安装路径。此外,还提到了意外情况的处理,例如误删除停止的容器后的应对措施。最后,文章还提供了使用Docker安装nginx的步骤和成功访问的截图。
Linux虚拟机磁盘扩容、Docker容器磁盘满的问题、Docker安装nginx
|
1月前
|
应用服务中间件 Linux nginx
在CentOS上使用源码包安装Nginx、以及手动启动Nginx的步骤过程
这篇文章介绍了在CentOS系统上使用Nginx源码包进行安装和配置的详细步骤,包括源码包的获取、解压、配置、编译、安装、启动验证以及注意事项。
67 0
在CentOS上使用源码包安装Nginx、以及手动启动Nginx的步骤过程
|
1月前
|
应用服务中间件 网络安全 nginx
运维专题.Docker+Nginx服务器的SSL证书安装
运维专题.Docker+Nginx服务器的SSL证书安装
43 3
|
1月前
|
JavaScript 应用服务中间件 nginx
Windows安装hexo并配置nginx
Windows安装hexo并配置nginx
|
1月前
|
应用服务中间件 Shell 网络安全
nginx安装提示 libssl.so.3: cannot open shared object file: No
【8月更文挑战第1天】### 原因 未将安装的ssl中的`libssl.so.3`链接到`/usr/lib`导致缺失。 ### 解决方案 1. 检查openssl是否已安装,若为低版本则需重装。 ```sh whereis openssl
409 6