Nginx 安装--卸载--版本回滚

nginx 特点:
高并发,内存消耗少,配置文件简单,开源,gzip压缩,稳定性高,反向代理(宕机几率小),模块化设计(支持热部署),第三方支持好

安装过程:

解包解压缩:
useradd nginx -s /sbin/nologin #创建nginx用户只用来运行nginx,不登陆系统
tar zxf nginx-1.17.1.tar.gz
vim ./nginx-1.17.1/conf/nginx.conf

	user  nginx nginx; #修改执行nginx进程的是nginx用户和用户组;
	worker_processes  2; #开启2个
vim ./nginx-1.17.1/auto/cc/gcc #关闭debug信息,最小安装
	#debug
	#CFLAGS="$CFLAGS -g" #注释掉
vim ./nginx-1.17.1/src/core/nginx.h #设置是否显示版本号
	#define NGINX_VER          "nginx/" NGINX_VERSION  #加上NGINX_VERSION 显示

yum install  pcre-devel openssl-devel gcc zlib-devel -y  #编译源码需要
./configure --prefix=/usr/local/nginx --with-file-aio #编译,将服务安装到/usr/local/nginx
make && make install #只有第一次安装nginx才make install

#编译完成后,会在目录nginx-1.17.1下生成Makefile 和 objs

配置文件增加语法提示:
[root@server1 conf]# mkdir ~/.vim
[root@server1 conf]# cd ..
[root@server1 nginx-1.17.1]# cp -r contrib/vim/* ~/.vim

有颜色增加了语法提示:
在这里插入图片描述

nginx服务管理:

#/usr/local/nginx/sbin/下存放nginx代码;
nginx官网查文档

The master process supports the following signals:

TERM, INT fast shutdown
QUIT graceful shutdown
HUP changing configuration, keeping up with a changed time zone (only for FreeBSD and Linux), starting new worker processes with a new
configuration, graceful shutdown of old worker processes
USR1 re-opening log files
USR2 upgrading an executable file
WINCH graceful shutdown of worker processes

[root@server1 nginx]#/usr/local/nginx/sbin/nginx  #以脚本形式开启nginx
[root@server1 nginx]# ps aux #一个master,两个worker进程
[root@server1 nginx-1.17.1]# cd /usr/local/nginx/
[root@server1 nginx]# ./sbin/nginx -V #查看版本号等详细信息
nginx version: nginx/1.17.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-file-aio
[root@server1 nginx]# ./sbin/nginx -v #查看版本号
nginx version: nginx/1.17.1
[root@server1 nginx]# ./sbin/nginx -s stop #停止nginx 服务
[root@server1 nginx]# ./sbin/nginx -s reopen #nginx 服务

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述### 版本升级,回滚:
假设nginx要从1.17.1 升级到 1.16.0
#同样解压缩nginx-1.16.0.tar.gz,编译,安装时只make,加上make install会将之前/usr/local/nginx下的配置改掉。make完后,/nginx-1.16.0/objs/下生成 nginx文件,将其拷贝到安装目录的sbin下

ps -ef | grep nginx
kill -USR2 27397 #升级执行文件(27397是master 进程id,再生成一个master,两个worker)
ps -ef | grep nginx
kill -WINCH 27397  #关闭27397 master 进程的两个worker process
ps -ef | grep nginx
cp -f ~/nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -V #升级到1.16.0

在这里插入图片描述
在这里插入图片描述
版本升级到1.16.0。
在这里插入图片描述

回滚:
kill -HUP 27397#(将之前关闭的两个worker拉起来)nginx重新读取配置文件,若语法正确,开启新的worker,并关闭old worker直到停止all clients都被服务
kill -WINCH 29967 #29967 是之后生成的master id ,关闭它的两个worker进程
cp -f ~/nginx-1.17.1/objs/nginx /usr/local/nginx/sbin/nginx
kill -9 29967 #关闭进程
ps -ef | grep nginx
/usr/local/nginx/sbin/nginx -V 
nginx version: nginx/1.17.1
回滚完成

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
#####卸载:

[root@server1 nginx]# cd ~/nginx-1.17.1/
[root@server1 nginx-1.17.1]# make clean
rm -rf Makefile objs
[root@server1 nginx-1.17.1]# rm -fr /usr/local/nginx

每次添加模块都要重新编译,并将编译好后出现的objs/nginx拷贝到安装目录/sbin/nginx,如:
cp ./nginx-1.17.1/objs/nginx /usr/local/nginx/sbin/nginx

systemd的方式(取代脚本的方式)管理nginx:
cp /usr/lib/systemd/system/httpd.service /etc/systemd/system/nginx.service #拷贝httpd的管理文件作为模板,系统服务管理在/usr/lib/systemd/system/,自己管理的保存在/etc/systemd/system/
vim /etc/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx #打开
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecReload=/usr/local/nginx/sbin/nginx -s reload #重新加载
ExecStop=/usr/local/nginx/sbin/nginx -s stop #关闭
PrivateTmp=true
[Install]
WantedBy=multi-user.target

测试:

[root@server1 logs]# /usr/local/nginx/sbin/nginx -s stop #之前以脚本形式打开的进程,必须以脚本形式关闭
[root@server1 logs]# ps -ef | grep nginx
root      2676  2542  0 14:31 pts/0    00:00:00 grep --color=auto nginx
[root@server1 logs]# systemctl start nginx #打开nginx
[root@server1 logs]# ps -ef | grep nginx
root      2684     1  0 14:31 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     2685  2684  0 14:31 ?        00:00:00 nginx: worker process
nginx     2686  2684  0 14:31 ?        00:00:00 nginx: worker process
root      2688  2542  0 14:31 pts/0    00:00:00 grep --color=auto nginx

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值